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