Skip to content

Various sniffs: add tests for properties with asymmetric visibility #1125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ function moreParamSecond(LogicException $bar, Exception $foo) {
// phpcs:set Generic.CodeAnalysis.UnusedFunctionParameter ignoreTypeHints[]

class ConstructorPropertyPromotionNoContentInMethod {
public function __construct(protected int $id) {}
public function __construct(protected int $id, private(set) $foo) {}
}

class ConstructorPropertyPromotionWithContentInMethod {
public function __construct(protected int $id, $toggle = true) {
public function __construct(protected int $id, private(set) $foo, $toggle = true) {
if ($toggle === true) {
doSomething();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TypedProperties
}

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

class ConstructorPropertyPromotionAndNormalParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TypedProperties
}

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

class ConstructorPropertyPromotionAndNormalParams {
Expand Down
8 changes: 8 additions & 0 deletions src/Standards/Generic/Tests/PHP/UpperCaseConstantUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,11 @@ class SkipOverPHP84FinalProperties {
final MyType|false $propA;
private static final null|MyClass $propB;
}

// PHP 8.4 asymmetric visibility
class WithAsym {
private(set) null|true $asym1 = TRUE;
public private(set) ?bool $asym2 = false;
protected(set) false|string|null $asym3 = null;
public protected(set) Type|null|bool $asym4 = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,11 @@ class SkipOverPHP84FinalProperties {
final MyType|false $propA;
private static final null|MyClass $propB;
}

// PHP 8.4 asymmetric visibility
class WithAsym {
private(set) null|true $asym1 = TRUE;
public private(set) ?bool $asym2 = FALSE;
protected(set) false|string|null $asym3 = NULL;
public protected(set) Type|null|bool $asym4 = TRUE;
}
45 changes: 24 additions & 21 deletions src/Standards/Generic/Tests/PHP/UpperCaseConstantUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,30 @@ final class UpperCaseConstantUnitTest extends AbstractSniffUnitTest
public function getErrorList()
{
return [
7 => 1,
10 => 1,
15 => 1,
16 => 1,
23 => 1,
26 => 1,
31 => 1,
32 => 1,
39 => 1,
42 => 1,
47 => 1,
48 => 1,
70 => 1,
71 => 1,
85 => 1,
87 => 1,
88 => 1,
90 => 2,
92 => 2,
93 => 1,
98 => 2,
7 => 1,
10 => 1,
15 => 1,
16 => 1,
23 => 1,
26 => 1,
31 => 1,
32 => 1,
39 => 1,
42 => 1,
47 => 1,
48 => 1,
70 => 1,
71 => 1,
85 => 1,
87 => 1,
88 => 1,
90 => 2,
92 => 2,
93 => 1,
98 => 2,
112 => 1,
113 => 1,
114 => 1,
];

}//end getErrorList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class OnlyConstructorPropertyPromotion {

class ConstructorPropertyPromotionMixedWithNormalParams {
public function __construct(
public string $name = '',
public(set) string $name = '',
?int $optionalParam = 0,
mixed $requiredParam,
) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,17 @@ enum SomeEnum
$bar_foo = 3;
}
}

class AsymVisibility {
// The read scope is public, but not specified. Enforce the naming conventions anyway.
private(set) $asymPublicImplied = 'hello';
private(set) $_asymPublicImplied = 'hello';

// The read scope is private, so these properties should be handled as private properties.
private private(set) $asymPrivate = 'hello';
private(set) private $_asymPrivate = 'hello';

// The read scope is public/protected, so these properties should be handled as public properties.
public private(set) $asymPublicPrivate = 'hello';
private(set) protected $_asymPrivateProtected = 'hello';
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public function getErrorList()
146 => 1,
152 => 1,
155 => 1,
162 => 1,
165 => 1,
170 => 1,
];

return $errors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,17 @@ enum SomeEnum
$bar_foo = 3;
}
}

class AsymVisibility {
// The read scope is public, but not specified. Enforce the naming conventions anyway.
private(set) $asymPublicImplied = 'hello';
private(set) $_asymPublicImplied = 'hello';

// The read scope is private, so these properties should be handled as private properties.
private private(set) $asymPrivate = 'hello';
private(set) private $_asymPrivate = 'hello';

// The read scope is public/protected, so these properties should be handled as public properties.
protected private(set) $asymProtectedPrivate = 'hello';
private(set) public $_asymPrivatePublic = 'hello';
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function getErrorList()
121 => 1,
126 => 1,
129 => 1,
136 => 1,
139 => 1,
143 => 1,
144 => 1,
];

}//end getErrorList()
Expand Down