Skip to content

Commit e09be25

Browse files
PHP 8.4 | Add tokenizer support for asymmetric visibility (#871)
1 parent 4667299 commit e09be25

25 files changed

+684
-43
lines changed

src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.1.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,15 @@ class SkipOverPHP84FinalProperties {
160160
final MyType|FALSE $propA;
161161
private static final NULL|MyClass $propB;
162162
}
163+
164+
// PHP 8.4 asymmetric visibility
165+
class WithAsym {
166+
167+
private(set) NULL|TRUE $asym1 = TRUE;
168+
169+
public private(set) ?bool $asym2 = FALSE;
170+
171+
protected(set) FALSE|string|null $asym3 = NULL;
172+
173+
public protected(set) Type|NULL|bool $asym4 = TRUE;
174+
}

src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.1.inc.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,15 @@ class SkipOverPHP84FinalProperties {
160160
final MyType|FALSE $propA;
161161
private static final NULL|MyClass $propB;
162162
}
163+
164+
// PHP 8.4 asymmetric visibility
165+
class WithAsym {
166+
167+
private(set) NULL|TRUE $asym1 = true;
168+
169+
public private(set) ?bool $asym2 = false;
170+
171+
protected(set) FALSE|string|null $asym3 = null;
172+
173+
public protected(set) Type|NULL|bool $asym4 = true;
174+
}

src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public function getErrorList($testFile='')
6666
129 => 1,
6767
149 => 1,
6868
153 => 1,
69+
167 => 1,
70+
169 => 1,
71+
171 => 1,
72+
173 => 1,
6973
];
7074

7175
case 'LowerCaseConstantUnitTest.js':

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: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,28 @@ public function getErrorList()
4141
* The key of the array should represent the line number and the value
4242
* should represent the number of warnings that should occur on that line.
4343
*
44+
* @param string $testFile The name of the file being tested.
45+
*
4446
* @return array<int, int>
4547
*/
46-
public function getWarningList()
48+
public function getWarningList($testFile='')
4749
{
48-
return [
49-
4 => 1,
50-
12 => 1,
51-
21 => 1,
52-
];
50+
switch ($testFile) {
51+
case 'ConstantVisibilityUnitTest.1.inc':
52+
return [
53+
4 => 1,
54+
12 => 1,
55+
21 => 1,
56+
];
57+
58+
case 'ConstantVisibilityUnitTest.2.inc':
59+
return [
60+
6 => 1,
61+
];
62+
63+
default:
64+
return [];
65+
}
5366

5467
}//end getWarningList()
5568

src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,17 @@ class FinalProperties {
9696
public FINAL ?int $wrongOrder1;
9797
static protected final ?string $wrongOrder2;
9898
}
99+
100+
class AsymmetricVisibility {
101+
private(set) int $foo,
102+
$bar,
103+
$var = 5;
104+
105+
public private(set) readonly ?string $spaces;
106+
107+
protected(set) array $unfixed;
108+
109+
protected(set) public int $wrongOrder1;
110+
111+
private(set) protected ?string $wrongOrder2;
112+
}

src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,17 @@ class FinalProperties {
9393
FINAL public ?int $wrongOrder1;
9494
final protected static ?string $wrongOrder2;
9595
}
96+
97+
class AsymmetricVisibility {
98+
private(set) int $foo,
99+
$bar,
100+
$var = 5;
101+
102+
public private(set) readonly ?string $spaces;
103+
104+
protected(set) array $unfixed;
105+
106+
protected(set) public int $wrongOrder1;
107+
108+
private(set) protected ?string $wrongOrder2;
109+
}

src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,41 @@ final class PropertyDeclarationUnitTest extends AbstractSniffUnitTest
3131
public function getErrorList()
3232
{
3333
return [
34-
7 => 1,
35-
9 => 2,
36-
10 => 1,
37-
11 => 1,
38-
17 => 1,
39-
18 => 1,
40-
23 => 1,
41-
38 => 1,
42-
41 => 1,
43-
42 => 1,
44-
50 => 2,
45-
51 => 1,
46-
55 => 1,
47-
56 => 1,
48-
61 => 1,
49-
62 => 1,
50-
68 => 1,
51-
69 => 1,
52-
71 => 1,
53-
72 => 1,
54-
76 => 1,
55-
80 => 1,
56-
82 => 1,
57-
84 => 1,
58-
86 => 1,
59-
90 => 1,
60-
94 => 1,
61-
95 => 1,
62-
96 => 1,
63-
97 => 2,
34+
7 => 1,
35+
9 => 2,
36+
10 => 1,
37+
11 => 1,
38+
17 => 1,
39+
18 => 1,
40+
23 => 1,
41+
38 => 1,
42+
41 => 1,
43+
42 => 1,
44+
50 => 2,
45+
51 => 1,
46+
55 => 1,
47+
56 => 1,
48+
61 => 1,
49+
62 => 1,
50+
68 => 1,
51+
69 => 1,
52+
71 => 1,
53+
72 => 1,
54+
76 => 1,
55+
80 => 1,
56+
82 => 1,
57+
84 => 1,
58+
86 => 1,
59+
90 => 1,
60+
94 => 1,
61+
95 => 1,
62+
96 => 1,
63+
97 => 2,
64+
101 => 2,
65+
105 => 1,
66+
107 => 1,
67+
109 => 1,
68+
111 => 1,
6469
];
6570

6671
}//end getErrorList()

0 commit comments

Comments
 (0)