|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\DeadCode\Rector\Property; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr; |
| 9 | +use PhpParser\Node\Stmt\Class_; |
| 10 | +use PhpParser\Node\Stmt\ClassMethod; |
| 11 | +use Rector\Rector\AbstractRector; |
| 12 | +use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector; |
| 13 | +use Rector\ValueObject\MethodName; |
| 14 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 16 | + |
| 17 | +/** |
| 18 | + * @see \Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\RemoveDefaultValueFromAssignedPropertyRectorTest |
| 19 | + */ |
| 20 | +final class RemoveDefaultValueFromAssignedPropertyRector extends AbstractRector |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + private readonly ConstructorAssignDetector $constructorAssignDetector |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + public function getRuleDefinition(): RuleDefinition |
| 28 | + { |
| 29 | + return new RuleDefinition( |
| 30 | + 'Remove redundant default value from property that is always assigned in the constructor', |
| 31 | + [ |
| 32 | + new CodeSample( |
| 33 | + <<<'CODE_SAMPLE' |
| 34 | +class SomeClass |
| 35 | +{ |
| 36 | + private ?SomeType $someType = null; |
| 37 | +
|
| 38 | + public function __construct(SomeType $someType) |
| 39 | + { |
| 40 | + $this->someType = $someType; |
| 41 | + } |
| 42 | +} |
| 43 | +CODE_SAMPLE |
| 44 | + , |
| 45 | + <<<'CODE_SAMPLE' |
| 46 | +class SomeClass |
| 47 | +{ |
| 48 | + private ?SomeType $someType; |
| 49 | +
|
| 50 | + public function __construct(SomeType $someType) |
| 51 | + { |
| 52 | + $this->someType = $someType; |
| 53 | + } |
| 54 | +} |
| 55 | +CODE_SAMPLE |
| 56 | + ), |
| 57 | + ] |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return array<class-string<Node>> |
| 63 | + */ |
| 64 | + public function getNodeTypes(): array |
| 65 | + { |
| 66 | + return [Class_::class]; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param Class_ $node |
| 71 | + */ |
| 72 | + public function refactor(Node $node): ?Node |
| 73 | + { |
| 74 | + if (! $node->getMethod(MethodName::CONSTRUCT) instanceof ClassMethod) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + $hasChanged = false; |
| 79 | + |
| 80 | + foreach ($node->getProperties() as $property) { |
| 81 | + // untyped properties are handled by RemoveNullPropertyInitializationRector |
| 82 | + if (! $property->type instanceof Node) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + if ($property->hooks !== []) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + foreach ($property->props as $propertyProperty) { |
| 91 | + if (! $propertyProperty->default instanceof Expr) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + $propertyName = $this->getName($propertyProperty); |
| 96 | + if (! $this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + $propertyProperty->default = null; |
| 101 | + $hasChanged = true; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if ($hasChanged) { |
| 106 | + return $node; |
| 107 | + } |
| 108 | + |
| 109 | + return null; |
| 110 | + } |
| 111 | +} |
0 commit comments