8000 [Php82] Handle implicit "public" on construtor promotion on ReadOnlyC… · rectorphp/rector-src@c229e8e · GitHub
[go: up one dir, main page]

Skip to content

Commit c229e8e

Browse files
authored
[Php82] Handle implicit "public" on construtor promotion on ReadOnlyClassRector (#6895)
* [Php82] Handle implicit "public" on ReadOnlyClassRector * clean * clean up only used once private method
1 parent ac3a941 commit c229e8e

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;
4+
5+
final class ImplicitPublicReadonlyPropertyPromotion
6+
{
7+
public function __construct(readonly string $name)
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;
17+
18+
final readonly class ImplicitPublicReadonlyPropertyPromotion
19+
{
20+
public function __construct(public string $name)
21+
{
22+
}
23+
}
24+
25+
?>

rules/Privatization/NodeManipulator/VisibilityManipulator.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function changeNodeVisibility(ClassMethod | Property | ClassConst $node,
8989
$this->replaceVisibilityFlag($node, $visibility);
9090
}
9191

92-
public function makePublic(ClassMethod | Property | ClassConst $node): void
92+
public function makePublic(ClassMethod | Property | ClassConst | Param $node): void
9393
{
9494
$this->replaceVisibilityFlag($node, Visibility::PUBLIC);
9595
}
@@ -130,7 +130,15 @@ public function isReadonly(Class_ | Property | Param $node): bool
130130

131131
public function removeReadonly(Class_ | Property | Param $node): void
132132
{
133-
$this->removeVisibilityFlag($node, Visibility::READONLY);
133+
$isConstructorPromotionBefore = $node instanceof Param && $node->isPromoted();
134+
135+
$node->flags &= ~Modifiers::READONLY;
136+
137+
$isConstructorPromotionAfter = $node instanceof Param && $node->isPromoted();
138+
139+
if ($node instanceof Param && $isConstructorPromotionBefore && ! $isConstructorPromotionAfter) {
140+
$this->makePublic($node);
141+
}
134142
}
135143

136144
public function publicize(ClassConst|ClassMethod $node): ClassConst|ClassMethod|null
@@ -188,13 +196,6 @@ private function addVisibilityFlag(
188196
$node->flags |= $visibility;
189197
}
190198

191-
private function removeVisibilityFlag(
192-
Class_ | ClassMethod | Property | ClassConst | Param $node,
193-
int $visibility
194-
): void {
195-
$node->flags &= ~$visibility;
196-
}
197-
198199
private function replaceVisibilityFlag(ClassMethod | Property | ClassConst | Param $node, int $visibility): void
199200
{
200201
$isStatic = $node instanceof ClassMethod && $node->isStatic();

0 commit comments

Comments
 (0)
0