|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Php85\Rector\Property; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Attribute; |
| 9 | +use PhpParser\Node\AttributeGroup; |
| 10 | +use PhpParser\Node\Name\FullyQualified; |
| 11 | +use PhpParser\Node\Stmt\Class_; |
| 12 | +use PhpParser\Node\Stmt\Property; |
| 13 | +use PHPStan\Reflection\ClassReflection; |
| 14 | +use PHPStan\Reflection\ReflectionProvider; |
| 15 | +use Rector\NodeAnalyzer\ClassAnalyzer; |
| 16 | +use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer; |
| 17 | +use Rector\Rector\AbstractRector; |
| 18 | +use Rector\ValueObject\PhpVersionFeature; |
| 19 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 21 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 22 | + |
| 23 | +/** |
| 24 | + * @see https://wiki.php.net/rfc/override_properties |
| 25 | + * |
| 26 | + * @see \Rector\Tests\Php85\Rector\Property\AddOverrideAttributeToOverriddenPropertiesRector\AddOverrideAttributeToOverriddenPropertiesRectorTest |
| 27 | + */ |
| 28 | +final class AddOverrideAttributeToOverriddenPropertiesRector extends AbstractRector implements MinPhpVersionInterface |
| 29 | +{ |
| 30 | + private const string OVERRIDE_CLASS = 'Override'; |
| 31 | + |
| 32 | + private bool $hasChanged = false; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + private readonly ReflectionProvider $reflectionProvider, |
| 36 | + private readonly ClassAnalyzer $classAnalyzer, |
| 37 | + private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer, |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + public function getRuleDefinition(): RuleDefinition |
| 42 | + { |
| 43 | + return new RuleDefinition( |
| 44 | + 'Add override attribute to overridden properties', |
| 45 | + [ |
| 46 | + new CodeSample( |
| 47 | + <<<'CODE_SAMPLE' |
| 48 | +class ParentClass |
| 49 | +{ |
| 50 | + public string $name; |
| 51 | +} |
| 52 | +
|
| 53 | +final class ChildClass extends ParentClass |
| 54 | +{ |
| 55 | + public string $name; |
| 56 | +} |
| 57 | +CODE_SAMPLE |
| 58 | + , |
| 59 | + <<<'CODE_SAMPLE' |
| 60 | +class ParentClass |
| 61 | +{ |
| 62 | + public string $name; |
| 63 | +} |
| 64 | +
|
| 65 | +final class ChildClass extends ParentClass |
| 66 | +{ |
| 67 | + #[\Override] |
| 68 | + public string $name; |
| 69 | +} |
| 70 | +CODE_SAMPLE |
| 71 | + ), |
| 72 | + ] |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @return array<class-string<Node>> |
| 78 | + */ |
| 79 | + public function getNodeTypes(): array |
| 80 | + { |
| 81 | + return [Class_::class]; |
| 82 | + } |
| 83 | + |
| 84 | + public function provideMinPhpVersion(): int |
| 85 | + { |
| 86 | + return PhpVersionFeature::OVERRIDE_ATTRIBUTE_ON_PROPERTIES; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param Class_ $node |
| 91 | + */ |
| 92 | + public function refactor(Node $node): ?Node |
| 93 | + { |
| 94 | + if ($this->classAnalyzer->isAnonymousClass($node)) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + $className = (string) $this->getName($node); |
| 99 | + if (! $this->reflectionProvider->hasClass($className)) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + $classReflection = $this->reflectionProvider->getClass($className); |
| 104 | + $parentClassReflections = $classReflection->getParents(); |
| 105 | + |
| 106 | + if ($parentClassReflections === []) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + $this->hasChanged = false; |
| 111 | + |
| 112 | + foreach ($node->getProperties() as $property) { |
| 113 | + $this->processProperty($property, $parentClassReflections); |
| 114 | + } |
| 115 | + |
| 116 | + if ($this->hasChanged) { |
| 117 | + return $node; |
| 118 | + } |
| 119 | + |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @param ClassReflection[] $parentClassReflections |
| 125 | + */ |
| 126 | + private function processProperty(Property $property, array $parentClassReflections): void |
| 127 | + { |
| 128 | + if ($this->shouldSkipProperty($property)) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + foreach ($property->props as $propertyProperty) { |
| 133 | + $propertyName = $this->getName($propertyProperty); |
| 134 | + if ($propertyName === null) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + if ($this->isPropertyOverridden($propertyName, $parentClassReflections)) { |
| 139 | + $property->attrGroups[] = new AttributeGroup([new Attribute(new FullyQualified(self::OVERRIDE_CLASS))]); |
| 140 | + $this->hasChanged = true; |
| 141 | + return; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private function shouldSkipProperty(Property $property): bool |
| 147 | + { |
| 148 | + if ($property->isPrivate()) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + return $this->phpAttributeAnalyzer->hasPhpAttribute($property, self::OVERRIDE_CLASS); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @param ClassReflection[] $parentClassReflections |
| 157 | + */ |
| 158 | + private function isPropertyOverridden(string $propertyName, array $parentClassReflections): bool |
| 159 | + { |
| 160 | + foreach ($parentClassReflections as $parentClassReflection) { |
| 161 | + if (! $parentClassReflection->hasNativeProperty($propertyName)) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + $parentProperty = $parentClassReflection->getNativeProperty($propertyName); |
| 166 | + if ($parentProperty->isPrivate()) { |
| 167 | + continue; |
| 168 | + } |
| 169 | + |
| 170 | + return true; |
| 171 | + } |
| 172 | + |
| 173 | + return false; |
| 174 | + } |
| 175 | +} |
0 commit comments