Skip to content

Commit b79927e

Browse files
authored
[Privatization] Fix invalid "private protected(set)" on asymmetric visibility property on PrivatizeFinalClassPropertyRector (#8935)
1 parent 8125fe6 commit b79927e

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector\Fixture;
4+
5+
final class AsymmetricVisibility
6+
{
7+
protected protected(set) string $name = 'a';
8+
9+
protected private(set) string $other = 'b';
10+
11+
public function __construct(
12+
protected protected(set) int $promoted,
13+
) {
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector\Fixture;
22+
23+
final class AsymmetricVisibility
24+
{
25+
private string $name = 'a';
26+
27+
private string $other = 'b';
28+
29+
public function __construct(
30+
private int $promoted,
31+
) {
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector\Fixture;
4+
5+
final class SkipPublicAsymmetricVisibility
6+
{
7+
public protected(set) string $name = 'a';
8+
9+
protected(set) string $other = 'b';
10+
11+
public function __construct(
12+
public protected(set) int $promoted,
13+
) {
14+
}
15+
}

‎rules/Privatization/NodeManipulator/VisibilityManipulator.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ public function makePrivate(ClassMethod|Property|ClassConst|Param $node): void
116116
{
117117
$this->replaceVisibilityFlag($node, Visibility::PRIVATE);
118118

119+
// private property cannot have wider set visibility, and private(set) is redundant
120+
if ($node instanceof Property || $node instanceof Param) {
121+
$node->flags &= ~Modifiers::VISIBILITY_SET_MASK;
122+
}
123+
119124
// only constructor can be both private and final
120125
if ($node instanceof ClassMethod && $this->nodeNameResolver->isName($node, MethodName::CONSTRUCT)) {
121126
return;

0 commit comments

Comments
 (0)