Skip to content

Commit b010b6a

Browse files
committed
PSR12/ConstantVisibility: prevent false negatives for asym visibility
Class constants do not support asymmetric visibility, so if this "typo" would be encountered, the sniff should still flag the constant as not having visibility. Includes test.
1 parent bd1b0d6 commit b010b6a

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/Standards/PSR12/Sniffs/Properties/ConstantVisibilitySniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,14 @@ public function process(File $phpcsFile, $stackPtr)
5050
$ignore = Tokens::$emptyTokens;
5151
$ignore[] = T_FINAL;
5252

53+
$validVisibility = [
54+
T_PRIVATE => T_PRIVATE,
55+
T_PUBLIC => T_PUBLIC,
56+
T_PROTECTED => T_PROTECTED,
57+
];
58+
5359
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
54-
if (isset(Tokens::$scopeModifiers[$tokens[$prev]['code']]) === true) {
60+
if (isset($validVisibility[$tokens[$prev]['code']]) === true) {
5561
return;
5662
}
5763

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error - unsupported asym visibility used on class constant.
4+
// Testing that the sniff will flags this as the constant doesn't have a valid visibility.
5+
class Foo {
6+
public(set) const BAR = 'bar';
7+
}

src/Standards/PSR12/Tests/Properties/ConstantVisibilityUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public function getWarningList($testFile='')
5555
21 => 1,
5656
];
5757

58+
case 'ConstantVisibilityUnitTest.2.inc':
59+
return [
60+
6 => 1,
61+
];
62+
5863
default:
5964
return [];
6065
}

0 commit comments

Comments
 (0)