Skip to content

Commit c5b890e

Browse files
authored
[DeadCode] Skip protected property on non-final class on RemoveDefaultValueFromAssignedPropertyRector (#8214)
1 parent e729052 commit c5b890e

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;
4+
5+
class SkipProtectedPropertyOnNonFinalClass
6+
{
7+
protected int $value = 5;
8+
9+
public function __construct(int $value)
10+
{
11+
$this->value = $value;
12+
}
13+
}

‎rules/DeadCode/Rector/Property/RemoveDefaultValueFromAssignedPropertyRector.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Stmt\Class_;
1212
use PhpParser\Node\Stmt\ClassMethod;
1313
use PhpParser\Node\Stmt\Return_;
14+
use Rector\Configuration\Parameter\FeatureFlags;
1415
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
1516
use Rector\PhpParser\Node\BetterNodeFinder;
1617
use Rector\Rector\AbstractRector;
@@ -89,6 +90,7 @@ public function refactor(Node $node): ?Node
8990
}
9091

9192
$hasChanged = false;
93+
$isFinal = $node->isFinal() || FeatureFlags::treatClassesAsFinal($node);
9294

9395
foreach ($node->getProperties() as $property) {
9496
// untyped properties are handled by RemoveNullPropertyInitializationRector
@@ -100,6 +102,10 @@ public function refactor(Node $node): ?Node
100102
continue;
101103
}
102104

105+
if (! $property->isPrivate() && ! $isFinal) {
106+
continue;
107+
}
108+
103109
foreach ($property->props as $propertyProperty) {
104110
if (! $propertyProperty->default instanceof Expr) {
105111
continue;

0 commit comments

Comments
 (0)