|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\CodeQuality\Rector\Class_; |
| 6 | + |
| 7 | +use PhpParser\Modifiers; |
| 8 | +use PhpParser\Node; |
| 9 | +use PhpParser\Node\Expr\FuncCall; |
| 10 | +use PhpParser\Node\Expr\MethodCall; |
| 11 | +use PhpParser\Node\Expr\StaticCall; |
| 12 | +use PhpParser\Node\Expr\Variable; |
| 13 | +use PhpParser\Node\Name; |
| 14 | +use PhpParser\Node\Stmt\Class_; |
| 15 | +use PhpParser\Node\Stmt\ClassMethod; |
| 16 | +use PhpParser\Node\Stmt\Function_; |
| 17 | +use Rector\Rector\AbstractRector; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 20 | + |
| 21 | +/** |
| 22 | + * @see \Rector\Tests\CodeQuality\Rector\Class_\InnerFunctionToPrivateMethodRector\InnerFunctionToPrivateMethodRectorTest |
| 23 | + */ |
| 24 | +final class InnerFunctionToPrivateMethodRector extends AbstractRector |
| 25 | +{ |
| 26 | + public function getRuleDefinition(): RuleDefinition |
| 27 | + { |
| 28 | + return new RuleDefinition( |
| 29 | + 'Turn an inner named function declared inside a class method into a private method, as inner named functions are not supported by PHPStan', |
| 30 | + [ |
| 31 | + new CodeSample( |
| 32 | + <<<'CODE_SAMPLE' |
| 33 | +class SomeClass |
| 34 | +{ |
| 35 | + public function run(): void |
| 36 | + { |
| 37 | + function inner(): void |
| 38 | + { |
| 39 | + echo 'hello'; |
| 40 | + } |
| 41 | +
|
| 42 | + inner(); |
| 43 | + } |
| 44 | +} |
| 45 | +CODE_SAMPLE |
| 46 | + , |
| 47 | + <<<'CODE_SAMPLE' |
| 48 | +class SomeClass |
| 49 | +{ |
| 50 | + public function run(): void |
| 51 | + { |
| 52 | + $this->inner(); |
| 53 | + } |
| 54 | +
|
| 55 | + private function inner(): void |
| 56 | + { |
| 57 | + echo 'hello'; |
| 58 | + } |
| 59 | +} |
| 60 | +CODE_SAMPLE |
| 61 | + ), |
| 62 | + ] |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @return array<class-string<Node>> |
| 68 | + */ |
| 69 | + public function getNodeTypes(): array |
| 70 | + { |
| 71 | + return [Class_::class]; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param Class_ $node |
| 76 | + */ |
| 77 | + public function refactor(Node $node): ?Node |
| 78 | + { |
| 79 | + $existingMethodNames = array_map( |
| 80 | + fn (ClassMethod $classMethod): string => $classMethod->name->toString(), |
| 81 | + $node->getMethods() |
| 82 | + ); |
| 83 | + |
| 84 | + $newClassMethods = []; |
| 85 | + |
| 86 | + foreach ($node->getMethods() as $classMethod) { |
| 87 | + $innerFunctions = $this->extractInnerFunctions($classMethod, $existingMethodNames); |
| 88 | + if ($innerFunctions === []) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + $isStatic = $classMethod->isStatic(); |
| 93 | + $innerFunctionNames = array_map( |
| 94 | + fn (Function_ $innerFunction): string => $innerFunction->name->toString(), |
| 95 | + $innerFunctions |
| 96 | + ); |
| 97 | + |
| 98 | + foreach ($innerFunctions as $innerFunction) { |
| 99 | + $newClassMethod = $this->createPrivateMethod($innerFunction, $isStatic); |
| 100 | + $this->rewriteCallsToMethod($newClassMethod, $innerFunctionNames, $isStatic); |
| 101 | + $newClassMethods[] = $newClassMethod; |
| 102 | + } |
| 103 | + |
| 104 | + $this->rewriteCallsToMethod($classMethod, $innerFunctionNames, $isStatic); |
| 105 | + } |
| 106 | + |
| 107 | + if ($newClassMethods === []) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + $node->stmts = array_merge($node->stmts, $newClassMethods); |
| 112 | + |
| 113 | + return $node; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param string[] $existingMethodNames |
| 118 | + * @return Function_[] |
| 119 | + */ |
| 120 | + private function extractInnerFunctions(ClassMethod $classMethod, array &$existingMethodNames): array |
| 121 | + { |
| 122 | + $innerFunctions = []; |
| 123 | + |
| 124 | + foreach ($classMethod->stmts ?? [] as $key => $stmt) { |
| 125 | + if (! $stmt instanceof Function_) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + $functionName = $stmt->name->toString(); |
| 130 | + |
| 131 | + // avoid collision with an already existing method |
| 132 | + if (in_array($functionName, $existingMethodNames, true)) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + $existingMethodNames[] = $functionName; |
| 137 | + $innerFunctions[] = $stmt; |
| 138 | + unset($classMethod->stmts[$key]); |
| 139 | + } |
| 140 | + |
| 141 | + if ($innerFunctions !== []) { |
| 142 | + $classMethod->stmts = array_values((array) $classMethod->stmts); |
| 143 | + } |
| 144 | + |
| 145 | + return $innerFunctions; |
| 146 | + } |
| 147 | + |
| 148 | + private function createPrivateMethod(Function_ $innerFunction, bool $isStatic): ClassMethod |
| 149 | + { |
| 150 | + $flags = Modifiers::PRIVATE; |
| 151 | + if ($isStatic) { |
| 152 | + $flags |= Modifiers::STATIC; |
| 153 | + } |
| 154 | + |
| 155 | + $classMethod = new ClassMethod($innerFunction->name, [ |
| 156 | + 'flags' => $flags, |
| 157 | + 'byRef' => $innerFunction->byRef, |
| 158 | + 'params' => $innerFunction->params, |
| 159 | + 'returnType' => $innerFunction->returnType, |
| 160 | + 'stmts' => $innerFunction->stmts, |
| 161 | + 'attrGroups' => $innerFunction->attrGroups, |
| 162 | + ]); |
| 163 | + |
| 164 | + $this->mirrorComments($classMethod, $innerFunction); |
| 165 | + |
| 166 | + return $classMethod; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @param string[] $innerFunctionNames |
| 171 | + */ |
| 172 | + private function rewriteCallsToMethod(ClassMethod $classMethod, array $innerFunctionNames, bool $isStatic): void |
| 173 | + { |
| 174 | + $this->traverseNodesWithCallable($classMethod->stmts ?? [], function (Node $node) use ( |
| 175 | + $innerFunctionNames, |
| 176 | + $isStatic |
| 177 | + ): MethodCall|StaticCall|null { |
| 178 | + if (! $node instanceof FuncCall) { |
| 179 | + return null; |
| 180 | + } |
| 181 | + |
| 182 | + if (! $node->name instanceof Name) { |
| 183 | + return null; |
| 184 | + } |
| 185 | + |
| 186 | + $functionName = $node->name->toString(); |
| 187 | + if (! in_array($functionName, $innerFunctionNames, true)) { |
| 188 | + return null; |
| 189 | + } |
| 190 | + |
| 191 | + if ($isStatic) { |
| 192 | + return new StaticCall(new Name('self'), $functionName, $node->args); |
| 193 | + } |
| 194 | + |
| 195 | + return new MethodCall(new Variable('this'), $functionName, $node->args); |
| 196 | + }); |
| 197 | + } |
| 198 | +} |
0 commit comments