Skip to content

Commit e482cdc

Browse files
committed
[Privatization] Remove final flag on PrivatizeFinalClassPropertyRector
1 parent 72d6525 commit e482cdc

5 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\Privatization\Rector\ClassConst\PrivatizeFinalClassConstantRector\Fixture;
4+
5+
final class RemoveFinalModifier
6+
{
7+
final protected const VALUE = 'value';
8+
}
9+
10+
?>
11+
-----
12+
<?php
13+
14+
namespace Rector\Tests\Privatization\Rector\ClassConst\PrivatizeFinalClassConstantRector\Fixture;
15+
16+
final class RemoveFinalModifier
17+
{
18+
private const VALUE = 'value';
19+
}
20+
21+
?>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\Fixture;
4+
5+
final class RemoveFinalModifier
6+
{
7+
final protected function run()
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\Fixture;
17+
18+
final class RemoveFinalModifier
19+
{
20+
private function run()
21+
{
22+
}
23+
}
24+
25+
?>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector\Fixture;
4+
5+
final class RemoveFinalModifier
6+
{
7+
protected final $x = true;
8+
}
9+
10+
?>
11+
-----
12+
<?php
13+
14+
namespace Rector\Tests\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector\Fixture;
15+
16+
final class RemoveFinalModifier
17+
{
18+
private $x = true;
19+
}
20+
21+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector\Fixture;
4+
5+
final class RemoveFinalModifierPromotedProperty
6+
{
7+
public function __construct(protected final $value)
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector\Fixture;
17+
18+
final class RemoveFinalModifierPromotedProperty
19+
{
20+
public function __construct(private $value)
21+
{
22+
}
23+
}
24+
25+
?>

‎rules/Privatization/NodeManipulator/VisibilityManipulator.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Stmt\ClassConst;
1111
use PhpParser\Node\Stmt\ClassMethod;
1212
use PhpParser\Node\Stmt\Property;
13+
use Rector\ValueObject\MethodName;
1314
use Rector\ValueObject\Visibility;
1415
use Webmozart\Assert\Assert;
1516

@@ -105,6 +106,13 @@ public function makeProtected(ClassMethod|Property|ClassConst $node): void
105106
public function makePrivate(ClassMethod|Property|ClassConst|Param $node): void
106107
{
107108
$this->replaceVisibilityFlag($node, Visibility::PRIVATE);
109+
110+
// only constructor can be both private and final
111+
if ($node instanceof ClassMethod && $node->name->toString() === MethodName::CONSTRUCT) {
112+
return;
113+
}
114+
115+
$node->flags &= ~Modifiers::FINAL;
108116
}
109117

110118
/**

0 commit comments

Comments
 (0)