|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tempest\Upgrade\Tempest34; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\PropertyFetch; |
| 7 | +use PhpParser\Node\Identifier; |
| 8 | +use Rector\Rector\AbstractRector; |
| 9 | + |
| 10 | +final class UpdateKernelDiscoveryPropertiesRector extends AbstractRector |
| 11 | +{ |
| 12 | + private const array PROPERTY_RENAMES = [ |
| 13 | + 'discoveryLocations' => 'locations', |
| 14 | + 'discoveryClasses' => 'classes', |
| 15 | + ]; |
| 16 | + |
| 17 | + public function getNodeTypes(): array |
| 18 | + { |
| 19 | + return [ |
| 20 | + PropertyFetch::class, |
| 21 | + ]; |
| 22 | + } |
| 23 | + |
| 24 | + public function refactor(Node $node): ?Node |
| 25 | + { |
| 26 | + if (! $node instanceof PropertyFetch) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + if (! $node->name instanceof Identifier) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + $propertyName = $node->name->toString(); |
| 35 | + |
| 36 | + if (! isset(self::PROPERTY_RENAMES[$propertyName])) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + if (! $this->isKernelType($node->var)) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + // Transform $kernel->discoveryLocations to $kernel->registry->locations |
| 45 | + return new PropertyFetch( |
| 46 | + new PropertyFetch($node->var, 'registry'), |
| 47 | + self::PROPERTY_RENAMES[$propertyName], |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + private function isKernelType(Node\Expr $expr): bool |
| 52 | + { |
| 53 | + $type = $this->nodeTypeResolver->getType($expr); |
| 54 | + |
| 55 | + foreach ($type->getObjectClassNames() as $className) { |
| 56 | + if ($className === 'Tempest\Core\Kernel' || is_subclass_of($className, 'Tempest\Core\Kernel')) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return false; |
| 62 | + } |
| 63 | +} |
0 commit comments