diff --git a/SlevomatCodingStandard/Sniffs/Classes/ClassConstantVisibilitySniff.php b/SlevomatCodingStandard/Sniffs/Classes/ClassConstantVisibilitySniff.php index 0f375d9f7..19ee9f1bb 100644 --- a/SlevomatCodingStandard/Sniffs/Classes/ClassConstantVisibilitySniff.php +++ b/SlevomatCodingStandard/Sniffs/Classes/ClassConstantVisibilitySniff.php @@ -12,6 +12,7 @@ use function in_array; use function sprintf; use const T_CONST; +use const T_EQUAL; use const T_FINAL; use const T_PRIVATE; use const T_PROTECTED; @@ -62,10 +63,13 @@ public function process(File $phpcsFile, $constantPointer): void return; } + $equalSignPointer = TokenHelper::findNext($phpcsFile, T_EQUAL, $constantPointer + 1); + $namePointer = TokenHelper::findPreviousEffective($phpcsFile, $equalSignPointer - 1); + $message = sprintf( 'Constant %s::%s visibility missing.', ClassHelper::getFullyQualifiedName($phpcsFile, $classPointer), - $tokens[TokenHelper::findNextEffective($phpcsFile, $constantPointer + 1)]['content'] + $tokens[$namePointer]['content'] ); if ($this->fixable) { diff --git a/tests/Sniffs/Classes/ClassConstantVisibilitySniffTest.php b/tests/Sniffs/Classes/ClassConstantVisibilitySniffTest.php index e890b3460..ab3f85982 100644 --- a/tests/Sniffs/Classes/ClassConstantVisibilitySniffTest.php +++ b/tests/Sniffs/Classes/ClassConstantVisibilitySniffTest.php @@ -11,7 +11,7 @@ public function testErrors(): void { $report = self::checkFile(__DIR__ . '/data/classWithConstants.php'); - self::assertSame(3, $report->getErrorCount()); + self::assertSame(4, $report->getErrorCount()); self::assertNoSniffError($report, 7); self::assertNoSniffError($report, 9); @@ -26,20 +26,27 @@ public function testErrors(): void self::assertSniffError( $report, - 23, + 13, ClassConstantVisibilitySniff::CODE_MISSING_CONSTANT_VISIBILITY, - 'Constant class@anonymous::PUBLIC_FOO visibility missing.' + 'Constant \ClassWithConstants::PUBLIC_INT_CONST visibility missing.' ); self::assertSniffError( $report, 25, ClassConstantVisibilitySniff::CODE_MISSING_CONSTANT_VISIBILITY, + 'Constant class@anonymous::PUBLIC_FOO visibility missing.' + ); + + self::assertSniffError( + $report, + 27, + ClassConstantVisibilitySniff::CODE_MISSING_CONSTANT_VISIBILITY, 'Constant class@anonymous::FINAL_WITHOUT_VISIBILITY visibility missing.' ); - self::assertNoSniffError($report, 26); - self::assertNoSniffError($report, 27); + self::assertNoSniffError($report, 28); + self::assertNoSniffError($report, 29); } public function testNoClassConstants(): void diff --git a/tests/Sniffs/Classes/data/classWithConstants.php b/tests/Sniffs/Classes/data/classWithConstants.php index 431678104..e85a7a241 100644 --- a/tests/Sniffs/Classes/data/classWithConstants.php +++ b/tests/Sniffs/Classes/data/classWithConstants.php @@ -1,4 +1,4 @@ -= 8.1 += 8.3 class ClassWithConstants { @@ -10,6 +10,8 @@ class ClassWithConstants private const PRIVATE_FOO = null; private const PRIVATE_BAR = null; + const int PUBLIC_INT_CONST = 1; + public function __construct() { print_r(self::PRIVATE_BAR);