Skip to content

Commit

Permalink
Handle even attributes with array inside
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Apr 17, 2024
1 parent d426617 commit 21da5d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 16 additions & 8 deletions src/UsedSymbolExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use const T_USE;
use const T_WHITESPACE;

// phpcs:disable Squiz.PHP.CommentedOutCode.Found

class UsedSymbolExtractor
{

Expand Down Expand Up @@ -68,9 +70,10 @@ public function parseUsedSymbols(): array
$useStatements = [];
$useStatementKinds = [];

$level = 0;
$level = 0; // {, }, {$, ${
$squareLevel = 0; // [, ], #[
$inClassLevel = null;
$inAttribute = false;
$inAttributeSquareLevel = null;

$numTokens = $this->numTokens;
$tokens = $this->tokens;
Expand Down Expand Up @@ -98,7 +101,7 @@ public function parseUsedSymbols(): array
break;

case PHP_VERSION_ID > 80000 ? T_ATTRIBUTE : -1:
$inAttribute = true;
$inAttributeSquareLevel = ++$squareLevel;
break;

case PHP_VERSION_ID >= 80000 ? T_NAMESPACE : -1:
Expand All @@ -107,7 +110,7 @@ public function parseUsedSymbols(): array

case PHP_VERSION_ID >= 80000 ? T_NAME_FULLY_QUALIFIED : -1:
$symbolName = $this->normalizeBackslash($token[1]);
$kind = $this->getFqnSymbolKind($this->pointer - 2, $this->pointer, $inAttribute);
$kind = $this->getFqnSymbolKind($this->pointer - 2, $this->pointer, $inAttributeSquareLevel !== null);
$usedSymbols[$kind][$symbolName][] = $token[2];
break;

Expand All @@ -116,7 +119,7 @@ public function parseUsedSymbols(): array

if (isset($useStatements[$neededAlias])) {
$symbolName = $useStatements[$neededAlias] . substr($token[1], strlen($neededAlias));
$kind = $this->getFqnSymbolKind($this->pointer - 2, $this->pointer, $inAttribute);
$kind = $this->getFqnSymbolKind($this->pointer - 2, $this->pointer, $inAttributeSquareLevel !== null);
$usedSymbols[$kind][$symbolName][] = $token[2];
}

Expand Down Expand Up @@ -188,8 +191,14 @@ public function parseUsedSymbols(): array
}

$level--;
} elseif ($token === ']' && $inAttribute) {
$inAttribute = false;
} elseif ($token === '[') {
$squareLevel++;
} elseif ($token === ']') {
if ($squareLevel === $inAttributeSquareLevel) {
$inAttributeSquareLevel = null;
}

$squareLevel--;
}
}

Expand Down Expand Up @@ -353,7 +362,6 @@ private function getFqnSymbolKind(
break;
} while ($pointerAfterName < $this->numTokens);

// phpcs:disable Squiz.PHP.CommentedOutCode.Found
if (
$tokenAfterName === '('
&& $tokenBeforeName[0] !== T_NEW // eliminate new \ClassName(
Expand Down
2 changes: 1 addition & 1 deletion tests/data/not-autoloaded/used-symbols/attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class ClassWithAttribute {}

#[
\Assert\NotNull(),
\Assert\NotNull(foo: []),
\Assert\NotBlank(),
]
class ClassWithMultipleAttributes {}

0 comments on commit 21da5d6

Please sign in to comment.