|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\DeadCode\Rector\ClassMethod; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\Expr\StaticCall; |
| 10 | +use PhpParser\Node\Expr\Variable; |
| 11 | +use PhpParser\Node\Stmt; |
| 12 | +use PhpParser\Node\Stmt\ClassMethod; |
| 13 | +use PhpParser\Node\Stmt\Expression; |
| 14 | +use PhpParser\NodeVisitor; |
| 15 | +use PHPStan\Reflection\ClassReflection; |
| 16 | +use PHPStan\Reflection\ExtendedMethodReflection; |
| 17 | +use Rector\Enum\ObjectReference; |
| 18 | +use Rector\PHPStan\ScopeFetcher; |
| 19 | +use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; |
| 20 | +use Rector\Rector\AbstractRector; |
| 21 | +use Rector\StaticTypeMapper\StaticTypeMapper; |
| 22 | +use Rector\ValueObject\MethodName; |
| 23 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 24 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 25 | + |
| 26 | +/** |
| 27 | + * @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\RemoveParentDelegatingConstructorRectorTest |
| 28 | + */ |
| 29 | +final class RemoveParentDelegatingConstructorRector extends AbstractRector |
| 30 | +{ |
| 31 | + public function __construct( |
| 32 | + private readonly StaticTypeMapper $staticTypeMapper, |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + public function getRuleDefinition(): RuleDefinition |
| 37 | + { |
| 38 | + return new RuleDefinition( |
| 39 | + 'Remove constructor that only delegates call to parent class with same values', |
| 40 | + [ |
| 41 | + new CodeSample( |
| 42 | + <<<'CODE_SAMPLE' |
| 43 | +class Node |
| 44 | +{ |
| 45 | + public function __construct(array $attributes) |
| 46 | + { |
| 47 | + } |
| 48 | +} |
| 49 | +
|
| 50 | +class SomeParent extends Node |
| 51 | +{ |
| 52 | + public function __construct(array $attributes) |
| 53 | + { |
| 54 | + parent::__construct($attributes); |
| 55 | + } |
| 56 | +} |
| 57 | +CODE_SAMPLE |
| 58 | + , |
| 59 | + <<<'CODE_SAMPLE' |
| 60 | +class Node |
| 61 | +{ |
| 62 | + public function __construct(array $attributes) |
| 63 | + { |
| 64 | + } |
| 65 | +} |
| 66 | +
|
| 67 | +class SomeParent extends Node |
| 68 | +{ |
| 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 [ClassMethod::class]; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param ClassMethod $node |
| 86 | + */ |
| 87 | + public function refactor(Node $node): ?int |
| 88 | + { |
| 89 | + if (! $this->isName($node, MethodName::CONSTRUCT)) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + if ($node->stmts === null || count($node->stmts) !== 1) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + $parentMethodReflection = $this->matchParentConstructorReflection($node); |
| 98 | + if (! $parentMethodReflection instanceof ExtendedMethodReflection) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + $soleStmt = $node->stmts[0]; |
| 103 | + $parentCallArgs = $this->matchParentConstructorCallArgs($soleStmt); |
| 104 | + if ($parentCallArgs === null) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + // match count and order |
| 109 | + if (! $this->isParameterAndArgCountAndOrderIdentical($node)) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + // match parameter types and parent constructor types |
| 114 | + if (! $this->areConstructorAndParentParameterTypesMatching($node, $parentMethodReflection)) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + return NodeVisitor::REMOVE_NODE; |
| 119 | + } |
| 120 | + |
| 121 | + private function matchParentConstructorReflection(ClassMethod $classMethod): ?ExtendedMethodReflection |
| 122 | + { |
| 123 | + $scope = ScopeFetcher::fetch($classMethod); |
| 124 | + |
| 125 | + $classReflection = $scope->getClassReflection(); |
| 126 | + if (! $classReflection instanceof ClassReflection) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + $parentClassReflection = $classReflection->getParentClass(); |
| 131 | + if (! $parentClassReflection instanceof ClassReflection) { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + if (! $parentClassReflection->hasConstructor()) { |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + return $parentClassReflection->getConstructor(); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Looking for parent::__construct() |
| 144 | + * |
| 145 | + * @return Arg[]|null |
| 146 | + */ |
| 147 | + private function matchParentConstructorCallArgs(Stmt $stmt): ?array |
| 148 | + { |
| 149 | + if (! $stmt instanceof Expression) { |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + if (! $stmt->expr instanceof StaticCall) { |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + $staticCall = $stmt->expr; |
| 158 | + if ($staticCall->isFirstClassCallable()) { |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + if (! $this->isName($staticCall->class, ObjectReference::PARENT)) { |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + if (! $this->isName($staticCall->name, MethodName::CONSTRUCT)) { |
| 167 | + return null; |
| 168 | + } |
| 169 | + |
| 170 | + return $staticCall->getArgs(); |
| 171 | + } |
| 172 | + |
| 173 | + private function isParameterAndArgCountAndOrderIdentical(ClassMethod $classMethod): bool |
| 174 | + { |
| 175 | + $soleStmt = $classMethod->stmts[0]; |
| 176 | + |
| 177 | + $parentCallArgs = $this->matchParentConstructorCallArgs($soleStmt); |
| 178 | + if ($parentCallArgs === null) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + $constructorParams = $classMethod->getParams(); |
| 183 | + if (count($constructorParams) !== count($parentCallArgs)) { |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + // match passed names in the same order |
| 188 | + $paramNames = []; |
| 189 | + foreach ($constructorParams as $constructorParam) { |
| 190 | + $paramNames[] = $this->getName($constructorParam->var); |
| 191 | + } |
| 192 | + |
| 193 | + $argNames = []; |
| 194 | + foreach ($parentCallArgs as $parentCallArg) { |
| 195 | + $argValue = $parentCallArg->value; |
| 196 | + if (! $argValue instanceof Variable) { |
| 197 | + return false; |
| 198 | + } |
| 199 | + |
| 200 | + $argNames[] = $this->getName($argValue); |
| 201 | + } |
| 202 | + |
| 203 | + return $paramNames === $argNames; |
| 204 | + } |
| 205 | + |
| 206 | + private function areConstructorAndParentParameterTypesMatching( |
| 207 | + ClassMethod $classMethod, |
| 208 | + ExtendedMethodReflection $extendedMethodReflection |
| 209 | + ): bool { |
| 210 | + foreach ($classMethod->getParams() as $position => $param) { |
| 211 | + $parameterType = $param->type; |
| 212 | + |
| 213 | + // no type override |
| 214 | + if ($parameterType === null) { |
| 215 | + continue; |
| 216 | + } |
| 217 | + |
| 218 | + $parametersSelector = $extendedMethodReflection->getOnlyVariant(); |
| 219 | + |
| 220 | + foreach ($parametersSelector->getParameters() as $index => $parameterReflection) { |
| 221 | + if ($index !== $position) { |
| 222 | + continue; |
| 223 | + } |
| 224 | + |
| 225 | + $parentParameterType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode( |
| 226 | + $parameterReflection->getType(), |
| 227 | + TypeKind::PARAM |
| 228 | + ); |
| 229 | + |
| 230 | + if (! $this->nodeComparator->areNodesEqual($parameterType, $parentParameterType)) { |
| 231 | + return false; |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + return true; |
| 237 | + } |
| 238 | +} |
0 commit comments