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