Skip to content

Commit 15f95f8

Browse files
committed
SlevomatCodingStandard.Classes.ClassConstantVisibility: Fixed error message for typed constants
1 parent 74b296b commit 15f95f8

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

SlevomatCodingStandard/Sniffs/Classes/ClassConstantVisibilitySniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function in_array;
1313
use function sprintf;
1414
use const T_CONST;
15+
use const T_EQUAL;
1516
use const T_FINAL;
1617
use const T_PRIVATE;
1718
use const T_PROTECTED;
@@ -62,10 +63,13 @@ public function process(File $phpcsFile, $constantPointer): void
6263
return;
6364
}
6465

66+
$equalSignPointer = TokenHelper::findNext($phpcsFile, T_EQUAL, $constantPointer + 1);
67+
$namePointer = TokenHelper::findPreviousEffective($phpcsFile, $equalSignPointer - 1);
68+
6569
$message = sprintf(
6670
'Constant %s::%s visibility missing.',
6771
ClassHelper::getFullyQualifiedName($phpcsFile, $classPointer),
68-
$tokens[TokenHelper::findNextEffective($phpcsFile, $constantPointer + 1)]['content']
72+
$tokens[$namePointer]['content']
6973
);
7074

7175
if ($this->fixable) {

tests/Sniffs/Classes/ClassConstantVisibilitySniffTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function testErrors(): void
1111
{
1212
$report = self::checkFile(__DIR__ . '/data/classWithConstants.php');
1313

14-
self::assertSame(3, $report->getErrorCount());
14+
self::assertSame(4, $report->getErrorCount());
1515

1616
self::assertNoSniffError($report, 7);
1717
self::assertNoSniffError($report, 9);
@@ -26,20 +26,27 @@ public function testErrors(): void
2626

2727
self::assertSniffError(
2828
$report,
29-
23,
29+
13,
3030
ClassConstantVisibilitySniff::CODE_MISSING_CONSTANT_VISIBILITY,
31-
'Constant class@anonymous::PUBLIC_FOO visibility missing.'
31+
'Constant \ClassWithConstants::PUBLIC_INT_CONST visibility missing.'
3232
);
3333

3434
self::assertSniffError(
3535
$report,
3636
25,
3737
ClassConstantVisibilitySniff::CODE_MISSING_CONSTANT_VISIBILITY,
38+
'Constant class@anonymous::PUBLIC_FOO visibility missing.'
39+
);
40+
41+
self::assertSniffError(
42+
$report,
43+
27,
44+
ClassConstantVisibilitySniff::CODE_MISSING_CONSTANT_VISIBILITY,
3845
'Constant class@anonymous::FINAL_WITHOUT_VISIBILITY visibility missing.'
3946
);
4047

41-
self::assertNoSniffError($report, 26);
42-
self::assertNoSniffError($report, 27);
48+
self::assertNoSniffError($report, 28);
49+
self::assertNoSniffError($report, 29);
4350
}
4451

4552
public function testNoClassConstants(): void

tests/Sniffs/Classes/data/classWithConstants.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php // lint >= 8.1
1+
<?php // lint >= 8.3
22

33
class ClassWithConstants
44
{
@@ -10,6 +10,8 @@ class ClassWithConstants
1010
private const PRIVATE_FOO = null;
1111
private const PRIVATE_BAR = null;
1212

13+
const int PUBLIC_INT_CONST = 1;
14+
1315
public function __construct()
1416
{
1517
print_r(self::PRIVATE_BAR);

0 commit comments

Comments
 (0)