|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\DeadCode\Rector\Class_; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr; |
| 8 | +use PhpParser\Node\Expr\BooleanNot; |
| 9 | +use PhpParser\Node\Expr\Instanceof_; |
| 10 | +use PhpParser\Node\Stmt\Class_; |
| 11 | +use PhpParser\Node\Stmt\ClassMethod; |
| 12 | +use PhpParser\Node\Stmt\If_; |
| 13 | +use PhpParser\Node\Stmt\Return_; |
| 14 | +use PHPStan\Reflection\ClassReflection; |
| 15 | +use PHPStan\Type\ObjectType; |
| 16 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 17 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 18 | +use Rector\PHPStan\ScopeFetcher; |
| 19 | +use Rector\Rector\AbstractRector; |
| 20 | +use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType; |
| 21 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 22 | +use RectorPrefix202606\Webmozart\Assert\Assert; |
| 23 | +/** |
| 24 | + * @see \Rector\Tests\DeadCode\Rector\Class_\RemoveRefactorDuplicatedNodeInstanceCheckRector\RemoveRefactorDuplicatedNodeInstanceCheckRectorTest |
| 25 | + */ |
| 26 | +final class RemoveRefactorDuplicatedNodeInstanceCheckRector extends AbstractRector |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @readonly |
| 30 | + */ |
| 31 | + private PhpDocInfoFactory $phpDocInfoFactory; |
| 32 | + /** |
| 33 | + * @readonly |
| 34 | + */ |
| 35 | + private ValueResolver $valueResolver; |
| 36 | + public function __construct(PhpDocInfoFactory $phpDocInfoFactory, ValueResolver $valueResolver) |
| 37 | + { |
| 38 | + $this->phpDocInfoFactory = $phpDocInfoFactory; |
| 39 | + $this->valueResolver = $valueResolver; |
| 40 | + } |
| 41 | + public function getRuleDefinition(): RuleDefinition |
| 42 | + { |
| 43 | + return new RuleDefinition('Remove refactor() method of Rector rule double check of $classMethod instance, if already defined in @param type', []); |
| 44 | + } |
| 45 | + public function getNodeTypes(): array |
| 46 | + { |
| 47 | + return [Class_::class]; |
| 48 | + } |
| 49 | + /** |
| 50 | + * @param Class_ $node |
| 51 | + */ |
| 52 | + public function refactor(Node $node): ?Node |
| 53 | + { |
| 54 | + $scope = ScopeFetcher::fetch($node); |
| 55 | + $classReflection = $scope->getClassReflection(); |
| 56 | + if (!$classReflection instanceof ClassReflection) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + if (!$classReflection->is('Rector\Rector\AbstractRector')) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + $refactorClassMethod = $node->getMethod('refactor'); |
| 63 | + if (!$refactorClassMethod instanceof ClassMethod) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + $firstStmt = $refactorClassMethod->stmts[0] ?? null; |
| 67 | + if (!$firstStmt instanceof If_) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + $instanceofNodeClass = $this->matchBooleanNotInstanceOfNodeClass($firstStmt->cond); |
| 71 | + if (!is_string($instanceofNodeClass)) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + $nodeParamTypeClass = $this->matchNodeParamType($refactorClassMethod); |
| 75 | + $getNodeTypesClassMethod = $node->getMethod('getNodeTypes'); |
| 76 | + if (!$getNodeTypesClassMethod instanceof ClassMethod) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + $soleReturn = $getNodeTypesClassMethod->stmts[0] ?? null; |
| 80 | + $nodeTypeClass = null; |
| 81 | + if ($soleReturn instanceof Return_) { |
| 82 | + Assert::isInstanceOf($soleReturn->expr, Expr::class); |
| 83 | + $nodeTypes = $this->valueResolver->getValue($soleReturn->expr); |
| 84 | + if (count($nodeTypes) === 1) { |
| 85 | + $nodeTypeClass = $nodeTypes[0]; |
| 86 | + } |
| 87 | + } |
| 88 | + if ($nodeParamTypeClass !== null) { |
| 89 | + if ($nodeParamTypeClass !== $instanceofNodeClass) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + } elseif ($nodeTypeClass !== null) { |
| 93 | + if ($nodeTypeClass !== $instanceofNodeClass) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + } else { |
| 97 | + return null; |
| 98 | + } |
| 99 | + unset($refactorClassMethod->stmts[0]); |
| 100 | + return $node; |
| 101 | + } |
| 102 | + private function matchBooleanNotInstanceOfNodeClass(Expr $expr): ?string |
| 103 | + { |
| 104 | + if (!$expr instanceof BooleanNot) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + $booleanNot = $expr; |
| 108 | + if (!$booleanNot->expr instanceof Instanceof_) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + return $this->getInstanceofNodeClass($booleanNot->expr); |
| 112 | + } |
| 113 | + /** |
| 114 | + * @return class-string<Node>|null |
| 115 | + */ |
| 116 | + private function getInstanceofNodeClass(Instanceof_ $instanceof): ?string |
| 117 | + { |
| 118 | + $checkedClassType = $this->getType($instanceof->class); |
| 119 | + if (!$checkedClassType instanceof ObjectType) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + /** @var ClassReflection $classReflection */ |
| 123 | + $classReflection = $checkedClassType->getClassReflection(); |
| 124 | + if (!$classReflection->is(Node::class)) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + return $classReflection->getName(); |
| 128 | + } |
| 129 | + private function matchNodeParamType(ClassMethod $classMethod): ?string |
| 130 | + { |
| 131 | + $classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod); |
| 132 | + $paramType = $classMethodPhpDocInfo->getParamType('$node'); |
| 133 | + if (!$paramType instanceof ObjectType) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + if ($paramType instanceof ShortenedObjectType) { |
| 137 | + return $paramType->getFullyQualifiedName(); |
| 138 | + } |
| 139 | + return $paramType->getClassName(); |
| 140 | + } |
| 141 | +} |
0 commit comments