TokenNavigatorTrait.php
- Pfad:
src/Infrastructure/CodeAnalysis/TokenNavigatorTrait.php - Namespace: Infrastructure\CodeAnalysis
- Zeilen: 112 | Größe: 2,577 Bytes
- Geändert: 2025-12-25 01:44:45 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 100
- Dependencies: 100 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Klassen 1
-
TokenNavigatorTraittrait Zeile 15
Funktionen 4
-
extractTypeName()private Zeile 22 -
findNextString()private Zeile 53 -
findPrevNonWhitespace()private Zeile 83 -
findNextNonWhitespace()private Zeile 99
Verwendet von 1
- PhpFileParser.php trait
Code
<?php
declare(strict_types=1);
namespace Infrastructure\CodeAnalysis;
// @responsibility: Token-Navigation für PHP-Tokenizer
/**
* Trait for navigating PHP token arrays.
*
* Provides helper methods to find and extract tokens from
* token arrays generated by token_get_all().
*/
trait TokenNavigatorTrait
{
/**
* Extract a type name starting from a position.
*
* @param array<mixed> $tokens
*/
private function extractTypeName(array $tokens, int $startIndex): ?string
{
$count = count($tokens);
for ($i = $startIndex; $i < $count; $i++) {
$token = $tokens[$i];
if (!is_array($token)) {
continue;
}
if ($token[0] === T_WHITESPACE) {
continue;
}
if (in_array($token[0], [T_STRING, T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED], true)) {
return $token[1];
}
// Stop at unexpected token
break;
}
return null;
}
/**
* Find next T_STRING token and return its value.
*
* @param array<mixed> $tokens
*/
private function findNextString(array $tokens, int $startIndex): ?string
{
$count = count($tokens);
for ($i = $startIndex + 1; $i < $count; $i++) {
if (!is_array($tokens[$i])) {
if ($tokens[$i] === '(') {
return null; // Anonymous function
}
continue;
}
if ($tokens[$i][0] === T_STRING) {
return $tokens[$i][1];
}
if ($tokens[$i][0] !== T_WHITESPACE) {
break;
}
}
return null;
}
/**
* Find previous non-whitespace token index.
*
* @param array<mixed> $tokens
*/
private function findPrevNonWhitespace(array $tokens, int $index): ?int
{
for ($i = $index - 1; $i >= 0; $i--) {
if (!is_array($tokens[$i]) || $tokens[$i][0] !== T_WHITESPACE) {
return $i;
}
}
return null;
}
/**
* Find next non-whitespace token index.
*
* @param array<mixed> $tokens
*/
private function findNextNonWhitespace(array $tokens, int $index): ?int
{
$count = count($tokens);
for ($i = $index + 1; $i < $count; $i++) {
if (!is_array($tokens[$i]) || $tokens[$i][0] !== T_WHITESPACE) {
return $i;
}
}
return null;
}
}