Skip to content

Commit

Permalink
SlevomatCodingStandard.Classes.ClassConstantVisibility: Fixed error m…
Browse files Browse the repository at this point in the history
…essage for typed constants
  • Loading branch information
kukulich committed Mar 9, 2024
1 parent 74b296b commit 15f95f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 12 additions & 5 deletions tests/Sniffs/Classes/ClassConstantVisibilitySniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/Sniffs/Classes/data/classWithConstants.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php // lint >= 8.1
<?php // lint >= 8.3

class ClassWithConstants
{
Expand All @@ -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);
Expand Down

0 comments on commit 15f95f8

Please sign in to comment.