Skip to content

Commit

Permalink
Fix potential PHP warnings due to undefined token elements
Browse files Browse the repository at this point in the history
Ensure token elements are defined before accessing them to prevent potential PHP warnings. Added checks using `isset` to verify element existence and included `@phpstan-ignore-line` comments for static analysis tools.
  • Loading branch information
koriym committed Nov 2, 2024
1 parent ad48c3a commit 4dab18d
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/ClassesInDirectories.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ private static function extractNamespace(array $tokens): string|null
}

for ($j = $index + 1, $count = count($tokens); $j < $count; $j++) {
if ($tokens[$j][0] === T_NAME_QUALIFIED) {
if (isset($tokens[$j][0]) && $tokens[$j][0] === T_NAME_QUALIFIED) { // @phpstan-ignore-line
assert(isset($tokens[$j][1])); // @phpstan-ignore-line
$string = $tokens[$j][1];
assert(is_string($string));

Expand All @@ -113,8 +114,9 @@ private static function extractNamespace(array $tokens): string|null
/** @param array<int, mixed> $tokens */
private static function extractClassName(array $tokens): string|null
{
/** @psalm-suppress MixedAssignment */
foreach ($tokens as $index => $token) {
if ($token[0] !== T_CLASS && $token[0] !== T_INTERFACE) {
if (isset($token[0]) && $token[0] !== T_CLASS && $token[0] !== T_INTERFACE) { // @phpstan-ignore-line
continue;
}

Expand Down

0 comments on commit 4dab18d

Please sign in to comment.