Skip to content

Commit 72ca17f

Browse files
committed
ReferencedNameHelper: Global constant used in class constant definition was not detected
1 parent 3957603 commit 72ca17f

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

SlevomatCodingStandard/Helpers/ReferencedNameHelper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,13 @@ private static function isReferencedName(\PHP_CodeSniffer\Files\File $phpcsFile,
195195
return false;
196196
} elseif ($previousToken['code'] === T_OPEN_PARENTHESIS && isset($previousToken['parenthesis_owner']) && $tokens[$previousToken['parenthesis_owner']]['code'] === T_DECLARE) {
197197
return false;
198-
} elseif ($previousToken['code'] === T_COMMA && TokenHelper::findPreviousLocal($phpcsFile, [T_CONST, T_DECLARE], $previousPointer - 1) !== null) {
198+
} elseif ($previousToken['code'] === T_COMMA && TokenHelper::findPreviousLocal($phpcsFile, T_DECLARE, $previousPointer - 1) !== null) {
199199
return false;
200+
} elseif ($previousToken['code'] === T_COMMA) {
201+
$constPointer = TokenHelper::findPreviousLocal($phpcsFile, T_CONST, $previousPointer - 1);
202+
if ($constPointer !== null && TokenHelper::findNext($phpcsFile, [T_OPEN_SHORT_ARRAY, T_ARRAY], $constPointer + 1, $startPointer) === null) {
203+
return false;
204+
}
200205
} elseif ($previousToken['code'] === T_BITWISE_AND && TokenHelper::findPreviousLocal($phpcsFile, [T_FUNCTION], $previousPointer - 1) !== null) {
201206
return false;
202207
}

tests/Helpers/ReferencedNameHelperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function testGetAllReferencedNames(): void
4444
['\SecondExtendedInterface', false, false],
4545
['\ThirdExtendedInterface', false, false],
4646
['SomeTrait', false, false],
47+
['OPENSSL_ALGO_SHA256', false, true],
48+
['OPENSSL_ALGO_SHA512', false, true],
4749
];
4850

4951
$names = ReferencedNameHelper::getAllReferencedNames($codeSnifferFile, 0);

tests/Helpers/data/lotsOfReferencedNames.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,11 @@ public function work() : string
9494
}
9595

9696
}
97+
98+
class OpenSsl
99+
{
100+
public const LENGTH = [
101+
OPENSSL_ALGO_SHA256 => 64,
102+
OPENSSL_ALGO_SHA512 => 132,
103+
];
104+
}

0 commit comments

Comments
 (0)