Skip to content

Commit 15ce011

Browse files
authored
Merge pull request #1125 from PHPCSStandards/php-8.4/feature/various-sniffs-add-tests-asym-visibility
Various sniffs: add tests for properties with asymmetric visibility
2 parents 572c70a + 596a761 commit 15ce011

11 files changed

+80
-26
lines changed

src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.1.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ function moreParamSecond(LogicException $bar, Exception $foo) {
140140
// phpcs:set Generic.CodeAnalysis.UnusedFunctionParameter ignoreTypeHints[]
141141

142142
class ConstructorPropertyPromotionNoContentInMethod {
143-
public function __construct(protected int $id) {}
143+
public function __construct(protected int $id, private(set) $foo) {}
144144
}
145145

146146
class ConstructorPropertyPromotionWithContentInMethod {
147-
public function __construct(protected int $id, $toggle = true) {
147+
public function __construct(protected int $id, private(set) $foo, $toggle = true) {
148148
if ($toggle === true) {
149149
doSomething();
150150
}

src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class TypedProperties
7575
}
7676

7777
class ConstructorPropertyPromotionWithTypes {
78-
public function __construct(protected Float|Int $x, public ?STRING &$y = 'test', private mixed $z) {}
78+
public function __construct(protected Float|Int $x, public(set) ?STRING &$y = 'test', private mixed $z) {}
7979
}
8080

8181
class ConstructorPropertyPromotionAndNormalParams {

src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class TypedProperties
7575
}
7676

7777
class ConstructorPropertyPromotionWithTypes {
78-
public function __construct(protected float|int $x, public ?string &$y = 'test', private mixed $z) {}
78+
public function __construct(protected float|int $x, public(set) ?string &$y = 'test', private mixed $z) {}
7979
}
8080

8181
class ConstructorPropertyPromotionAndNormalParams {

src/Standards/Generic/Tests/PHP/UpperCaseConstantUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,11 @@ class SkipOverPHP84FinalProperties {
105105
final MyType|false $propA;
106106
private static final null|MyClass $propB;
107107
}
108+
109+
// PHP 8.4 asymmetric visibility
110+
class WithAsym {
111+
private(set) null|true $asym1 = TRUE;
112+
public private(set) ?bool $asym2 = false;
113+
protected(set) false|string|null $asym3 = null;
114+
public protected(set) Type|null|bool $asym4 = true;
115+
}

src/Standards/Generic/Tests/PHP/UpperCaseConstantUnitTest.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,11 @@ class SkipOverPHP84FinalProperties {
105105
final MyType|false $propA;
106106
private static final null|MyClass $propB;
107107
}
108+
109+
// PHP 8.4 asymmetric visibility
110+
class WithAsym {
111+
private(set) null|true $asym1 = TRUE;
112+
public private(set) ?bool $asym2 = FALSE;
113+
protected(set) false|string|null $asym3 = NULL;
114+
public protected(set) Type|null|bool $asym4 = TRUE;
115+
}

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,30 @@ final class UpperCaseConstantUnitTest extends AbstractSniffUnitTest
3131
public function getErrorList()
3232
{
3333
return [
34-
7 => 1,
35-
10 => 1,
36-
15 => 1,
37-
16 => 1,
38-
23 => 1,
39-
26 => 1,
40-
31 => 1,
41-
32 => 1,
42-
39 => 1,
43-
42 => 1,
44-
47 => 1,
45-
48 => 1,
46-
70 => 1,
47-
71 => 1,
48-
85 => 1,
49-
87 => 1,
50-
88 => 1,
51-
90 => 2,
52-
92 => 2,
53-
93 => 1,
54-
98 => 2,
34+
7 => 1,
35+
10 => 1,
36+
15 => 1,
37+
16 => 1,
38+
23 => 1,
39+
26 => 1,
40+
31 => 1,
41+
32 => 1,
42+
39 => 1,
43+
42 => 1,
44+
47 => 1,
45+
48 => 1,
46+
70 => 1,
47+
71 => 1,
48+
85 => 1,
49+
87 => 1,
50+
88 => 1,
51+
90 => 2,
52+
92 => 2,
53+
93 => 1,
54+
98 => 2,
55+
112 => 1,
56+
113 => 1,
57+
114 => 1,
5558
];
5659

5760
}//end getErrorList()

src/Standards/PEAR/Tests/Functions/ValidDefaultValueUnitTest.1.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class OnlyConstructorPropertyPromotion {
109109

110110
class ConstructorPropertyPromotionMixedWithNormalParams {
111111
public function __construct(
112-
public string $name = '',
112+
public(set) string $name = '',
113113
?int $optionalParam = 0,
114114
mixed $requiredParam,
115115
) {}

src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,17 @@ enum SomeEnum
155155
$bar_foo = 3;
156156
}
157157
}
158+
159+
class AsymVisibility {
160+
// The read scope is public, but not specified. Enforce the naming conventions anyway.
161+
private(set) $asymPublicImplied = 'hello';
162+
private(set) $_asymPublicImplied = 'hello';
163+
164+
// The read scope is private, so these properties should be handled as private properties.
165+
private private(set) $asymPrivate = 'hello';
166+
private(set) private $_asymPrivate = 'hello';
167+
168+
// The read scope is public/protected, so these properties should be handled as public properties.
169+
public private(set) $asymPublicPrivate = 'hello';
170+
private(set) protected $_asymPrivateProtected = 'hello';
171+
}

src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public function getErrorList()
6868
146 => 1,
6969
152 => 1,
7070
155 => 1,
71+
162 => 1,
72+
165 => 1,
73+
170 => 1,
7174
];
7275

7376
return $errors;

src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,17 @@ enum SomeEnum
129129
$bar_foo = 3;
130130
}
131131
}
132+
133+
class AsymVisibility {
134+
// The read scope is public, but not specified. Enforce the naming conventions anyway.
135+
private(set) $asymPublicImplied = 'hello';
136+
private(set) $_asymPublicImplied = 'hello';
137+
138+
// The read scope is private, so these properties should be handled as private properties.
139+
private private(set) $asymPrivate = 'hello';
140+
private(set) private $_asymPrivate = 'hello';
141+
142+
// The read scope is public/protected, so these properties should be handled as public properties.
143+
protected private(set) $asymProtectedPrivate = 'hello';
144+
private(set) public $_asymPrivatePublic = 'hello';
145+
}

src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public function getErrorList()
6161
121 => 1,
6262
126 => 1,
6363
129 => 1,
64+
136 => 1,
65+
139 => 1,
66+
143 => 1,
67+
144 => 1,
6468
];
6569

6670
}//end getErrorList()

0 commit comments

Comments
 (0)