|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tempest\Upgrade\Tempest312; |
| 4 | + |
| 5 | +use PhpParser\Modifiers; |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr; |
| 8 | +use PhpParser\Node\Identifier; |
| 9 | +use PhpParser\Node\Name; |
| 10 | +use PhpParser\Node\Stmt\Class_; |
| 11 | +use PhpParser\Node\Stmt\ClassMethod; |
| 12 | +use PhpParser\Node\Stmt\Expression; |
| 13 | +use PhpParser\Node\Stmt\Return_; |
| 14 | +use PhpParser\NodeTraverser; |
| 15 | +use PhpParser\NodeVisitorAbstract; |
| 16 | +use Rector\Rector\AbstractRector; |
| 17 | +use Tempest\Core\Kernel; |
| 18 | + |
| 19 | +final class UpdateKernelImplementationsRector extends AbstractRector |
| 20 | +{ |
| 21 | + public function getNodeTypes(): array |
| 22 | + { |
| 23 | + return [ |
| 24 | + Class_::class, |
| 25 | + ]; |
| 26 | + } |
| 27 | + |
| 28 | + public function refactor(Node $node): ?Node |
| 29 | + { |
| 30 | + if (! $node instanceof Class_) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + if (! $this->implementsKernel($node)) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + $hasChanged = false; |
| 39 | + $shutdown = $node->getMethod('shutdown'); |
| 40 | + |
| 41 | + if ($shutdown instanceof ClassMethod && ! $this->isVoidReturnType($shutdown)) { |
| 42 | + $shutdown->returnType = new Identifier('void'); |
| 43 | + $this->removeReturnValues($shutdown); |
| 44 | + $hasChanged = true; |
| 45 | + } |
| 46 | + |
| 47 | + if (!$node->getMethod('reset') instanceof ClassMethod) { |
| 48 | + $node->stmts[] = new ClassMethod('reset', [ |
| 49 | + 'flags' => Modifiers::PUBLIC, |
| 50 | + 'returnType' => new Identifier('void'), |
| 51 | + 'stmts' => [], |
| 52 | + ]); |
| 53 | + |
| 54 | + $hasChanged = true; |
| 55 | + } |
| 56 | + |
| 57 | + return $hasChanged ? $node : null; |
| 58 | + } |
| 59 | + |
| 60 | + private function implementsKernel(Class_ $class): bool |
| 61 | + { |
| 62 | + return array_any( |
| 63 | + $class->implements, |
| 64 | + fn (Name $name) => $this->isInterfaceName($name, Kernel::class, 'Kernel'), |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + private function isInterfaceName(Name $name, string $interfaceName, string $shortName): bool |
| 69 | + { |
| 70 | + $names = [ |
| 71 | + ltrim($name->toString(), '\\'), |
| 72 | + ]; |
| 73 | + |
| 74 | + $resolvedName = $name->getAttribute('resolvedName'); |
| 75 | + |
| 76 | + if ($resolvedName instanceof Name) { |
| 77 | + $names[] = ltrim($resolvedName->toString(), '\\'); |
| 78 | + } |
| 79 | + |
| 80 | + return array_any( |
| 81 | + $names, |
| 82 | + static fn (string $name) => in_array($name, [$interfaceName, $shortName], strict: true), |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + private function isVoidReturnType(ClassMethod $method): bool |
| 87 | + { |
| 88 | + return $method->returnType instanceof Identifier && $method->returnType->toString() === 'void'; |
| 89 | + } |
| 90 | + |
| 91 | + private function removeReturnValues(ClassMethod $method): void |
| 92 | + { |
| 93 | + if ($method->stmts === null) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + $traverser = new NodeTraverser(); |
| 98 | + $traverser->addVisitor(new class extends NodeVisitorAbstract { |
| 99 | + public function leaveNode(Node $node): ?array |
| 100 | + { |
| 101 | + if (! $node instanceof Return_ || ! $node->expr instanceof Expr) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + return [ |
| 106 | + new Expression($node->expr), |
| 107 | + new Return_(), |
| 108 | + ]; |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + $method->stmts = $traverser->traverse($method->stmts); |
| 113 | + } |
| 114 | +} |
0 commit comments