From 6e798e8658daa862d7bb5600b5cdbdcb8c25059b Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 23 Mar 2021 01:32:13 +0100 Subject: [PATCH] cleanup --- .../NodeCollector/NodeRepository.php | 53 ------ .../ParsedClassConstFetchNodeCollector.php | 160 ------------------ .../NodeVisitor/NodeCollectorNodeVisitor.php | 9 - .../Fixture/skip_used_constant.php.inc | 13 ++ .../FixtureOpenSource/skip_public.php.inc | 8 - .../OpenSourceRectorTest.php | 33 ---- .../config/project_open_source.php | 16 -- ...RemoveUnusedPrivateClassConstantRector.php | 17 +- 8 files changed, 27 insertions(+), 282 deletions(-) delete mode 100644 packages/NodeCollector/NodeCollector/ParsedClassConstFetchNodeCollector.php create mode 100644 rules-tests/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector/Fixture/skip_used_constant.php.inc delete mode 100644 rules-tests/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector/FixtureOpenSource/skip_public.php.inc delete mode 100644 rules-tests/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector/OpenSourceRectorTest.php delete mode 100644 rules-tests/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector/config/project_open_source.php diff --git a/packages/NodeCollector/NodeCollector/NodeRepository.php b/packages/NodeCollector/NodeCollector/NodeRepository.php index 7163edee19d6..e5cc61fb2f53 100644 --- a/packages/NodeCollector/NodeCollector/NodeRepository.php +++ b/packages/NodeCollector/NodeCollector/NodeRepository.php @@ -98,11 +98,6 @@ final class NodeRepository */ private $parsedPropertyFetchNodeCollector; - /** - * @var ParsedClassConstFetchNodeCollector - */ - private $parsedClassConstFetchNodeCollector; - /** * @var ParsedNodeCollector */ @@ -132,7 +127,6 @@ public function __construct( ArrayCallableMethodReferenceAnalyzer $arrayCallableMethodReferenceAnalyzer, ParsedPropertyFetchNodeCollector $parsedPropertyFetchNodeCollector, NodeNameResolver $nodeNameResolver, - ParsedClassConstFetchNodeCollector $parsedClassConstFetchNodeCollector, ParsedNodeCollector $parsedNodeCollector, TypeUnwrapper $typeUnwrapper, ReflectionProvider $reflectionProvider, @@ -141,7 +135,6 @@ public function __construct( $this->nodeNameResolver = $nodeNameResolver; $this->arrayCallableMethodReferenceAnalyzer = $arrayCallableMethodReferenceAnalyzer; $this->parsedPropertyFetchNodeCollector = $parsedPropertyFetchNodeCollector; - $this->parsedClassConstFetchNodeCollector = $parsedClassConstFetchNodeCollector; $this->parsedNodeCollector = $parsedNodeCollector; $this->typeUnwrapper = $typeUnwrapper; $this->reflectionProvider = $reflectionProvider; @@ -360,52 +353,6 @@ public function findCallsByClassMethod(ClassMethod $classMethod): array return $this->findCallsByClassAndMethod($class, $method); } - /** - * @return ClassReflection[] - */ - public function findDirectClassConstantFetches(ClassReflection $classReflection, string $desiredConstantName): array - { - $classConstantFetchByClassAndName = $this->parsedClassConstFetchNodeCollector->getClassConstantFetchByClassAndName(); - $classTypes = $classConstantFetchByClassAndName[$classReflection->getName()][$desiredConstantName] ?? []; - - return $this->resolveClassReflectionsFromClassTypes($classTypes); - } - - /** - * @return ClassReflection[] - */ - public function findIndirectClassConstantFetches( - ClassReflection $classReflection, - string $desiredConstantName - ): array { - $classConstantFetchByClassAndName = $this->parsedClassConstFetchNodeCollector->getClassConstantFetchByClassAndName(); - - foreach ($classConstantFetchByClassAndName as $className => $classesByConstantName) { - if (! $this->reflectionProvider->hasClass($className)) { - return []; - } - - $currentClassReflection = $this->reflectionProvider->getClass($className); - if (! isset($classesByConstantName[$desiredConstantName])) { - continue; - } - - if (! $classReflection->isSubclassOf($currentClassReflection->getName()) && - ! $currentClassReflection->isSubclassOf($classReflection->getName())) { - continue; - } - - // include child usages and parent usages - if ($currentClassReflection->getName() === $classReflection->getName()) { - continue; - } - - return $this->resolveClassReflectionsFromClassTypes($classesByConstantName[$desiredConstantName]); - } - - return []; - } - public function hasClassChildren(Class_ $desiredClass): bool { $desiredClassName = $desiredClass->getAttribute(AttributeKey::CLASS_NAME); diff --git a/packages/NodeCollector/NodeCollector/ParsedClassConstFetchNodeCollector.php b/packages/NodeCollector/NodeCollector/ParsedClassConstFetchNodeCollector.php deleted file mode 100644 index ad2a9cd77221..000000000000 --- a/packages/NodeCollector/NodeCollector/ParsedClassConstFetchNodeCollector.php +++ /dev/null @@ -1,160 +0,0 @@ -> - */ - private $classConstantFetchByClassAndName = []; - - /** - * @var NodeNameResolver - */ - private $nodeNameResolver; - - /** - * @var NodeTypeResolver - */ - private $nodeTypeResolver; - - /** - * @var ReflectionProvider - */ - private $reflectionProvider; - - public function __construct( - NodeNameResolver $nodeNameResolver, ReflectionProvider $reflectionProvider, - NodeTypeResolver $nodeTypeResolver - ) { - $this->nodeNameResolver = $nodeNameResolver; - $this->reflectionProvider = $reflectionProvider; - $this->nodeTypeResolver = $nodeTypeResolver; - } - - public function collect(Node $node): void - { - if (! $node instanceof ClassConstFetch) { - return; - } - - $constantName = $this->nodeNameResolver->getName($node->name); - if ($constantName === 'class') { - // this is not a manual constant - return; - } - if ($constantName === null) { - // this is not a manual constant - return; - } - - $resolvedClassType = $this->nodeTypeResolver->resolve($node->class); - - $className = $this->resolveClassTypeThatContainsConstantOrFirstUnioned($resolvedClassType, $constantName); - if ($className === null) { - return; - } - - // current class - $classOfUse = $node->getAttribute(AttributeKey::CLASS_NAME); - if ($classOfUse === null) { - return; - } - - $this->classConstantFetchByClassAndName[$className][$constantName][] = $classOfUse; - - $this->classConstantFetchByClassAndName[$className][$constantName] = array_unique( - $this->classConstantFetchByClassAndName[$className][$constantName] - ); - } - - /** - * @return array> - */ - public function getClassConstantFetchByClassAndName(): array - { - return $this->classConstantFetchByClassAndName; - } - - private function resolveClassTypeThatContainsConstantOrFirstUnioned( - Type $resolvedClassType, - string $constantName - ): ?string { - $className = $this->matchClassTypeThatContainsConstant($resolvedClassType, $constantName); - if ($className !== null) { - return $className; - } - - // we need at least one original user class - if (! $resolvedClassType instanceof UnionType) { - return null; - } - - foreach ($resolvedClassType->getTypes() as $unionedType) { - if (! $unionedType instanceof ObjectType) { - continue; - } - - return $unionedType->getClassName(); - } - - return null; - } - - private function matchClassTypeThatContainsConstant(Type $type, string $constant): ?string - { - if ($type instanceof ObjectType) { - return $type->getClassName(); - } - - $classNames = TypeUtils::getDirectClassNames($type); - - foreach ($classNames as $className) { - $currentClassConstants = $this->getConstantsDefinedInClass($className); - if (! in_array($constant, $currentClassConstants, true)) { - continue; - } - - return $className; - } - - return null; - } - - /** - * @return string[] - */ - private function getConstantsDefinedInClass(string $className): array - { - if (! $this->reflectionProvider->hasClass($className)) { - return []; - } - - $classReflection = $this->reflectionProvider->getClass($className); - $reflectionClass = $classReflection->getNativeReflection(); - - $constants = $reflectionClass->getConstants(); - - $currentClassConstants = array_keys($constants); - if ($classReflection->getParentClass() !== false) { - return $currentClassConstants; - } - - $parentClassConstants = array_keys($constants); - return array_diff($currentClassConstants, $parentClassConstants); - } -} diff --git a/packages/NodeCollector/NodeVisitor/NodeCollectorNodeVisitor.php b/packages/NodeCollector/NodeVisitor/NodeCollectorNodeVisitor.php index d0aba3bb40a9..77079938e005 100644 --- a/packages/NodeCollector/NodeVisitor/NodeCollectorNodeVisitor.php +++ b/packages/NodeCollector/NodeVisitor/NodeCollectorNodeVisitor.php @@ -7,7 +7,6 @@ use PhpParser\Node; use PhpParser\NodeVisitorAbstract; use Rector\NodeCollector\NodeCollector\NodeRepository; -use Rector\NodeCollector\NodeCollector\ParsedClassConstFetchNodeCollector; use Rector\NodeCollector\NodeCollector\ParsedNodeCollector; use Rector\NodeCollector\NodeCollector\ParsedPropertyFetchNodeCollector; @@ -28,13 +27,7 @@ final class NodeCollectorNodeVisitor extends NodeVisitorAbstract */ private $parsedPropertyFetchNodeCollector; - /** - * @var ParsedClassConstFetchNodeCollector - */ - private $parsedClassConstFetchNodeCollector; - public function __construct( - ParsedClassConstFetchNodeCollector $parsedClassConstFetchNodeCollector, NodeRepository $nodeRepository, ParsedNodeCollector $parsedNodeCollector, ParsedPropertyFetchNodeCollector $parsedPropertyFetchNodeCollector @@ -42,7 +35,6 @@ public function __construct( $this->nodeRepository = $nodeRepository; $this->parsedNodeCollector = $parsedNodeCollector; $this->parsedPropertyFetchNodeCollector = $parsedPropertyFetchNodeCollector; - $this->parsedClassConstFetchNodeCollector = $parsedClassConstFetchNodeCollector; } public function enterNode(Node $node) @@ -53,7 +45,6 @@ public function enterNode(Node $node) $this->nodeRepository->collect($node); $this->parsedPropertyFetchNodeCollector->collect($node); - $this->parsedClassConstFetchNodeCollector->collect($node); return null; } diff --git a/rules-tests/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector/Fixture/skip_used_constant.php.inc b/rules-tests/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector/Fixture/skip_used_constant.php.inc new file mode 100644 index 000000000000..ddffa9fc3cfa --- /dev/null +++ b/rules-tests/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector/Fixture/skip_used_constant.php.inc @@ -0,0 +1,13 @@ +doTestFileInfo($fileInfo); - } - - /** - * @return Iterator - */ - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureOpenSource'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/project_open_source.php'; - } -} diff --git a/rules-tests/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector/config/project_open_source.php b/rules-tests/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector/config/project_open_source.php deleted file mode 100644 index 3c2d20eb0511..000000000000 --- a/rules-tests/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector/config/project_open_source.php +++ /dev/null @@ -1,16 +0,0 @@ -parameters(); - $parameters->set(Option::PROJECT_TYPE, ProjectType::OPEN_SOURCE); - - $services = $containerConfigurator->services(); - $services->set(RemoveUnusedPrivateClassConstantRector::class); -}; diff --git a/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php b/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php index faa80e0ee912..60481d0018d9 100644 --- a/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php +++ b/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php @@ -10,6 +10,7 @@ use PhpParser\Node\Stmt\ClassLike; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; +use PHPStan\Type\ObjectType; use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\ApiPhpDocTagNode; use Rector\Core\NodeManipulator\ClassConstManipulator; use Rector\Core\Rector\AbstractRector; @@ -91,13 +92,23 @@ public function refactor(Node $node): ?Node return null; } + $classObjectType = new ObjectType($classReflection->getName()); + + /** @var ClassConstFetch[] $classConstFetches */ $classConstFetches = $this->betterNodeFinder->findInstanceOf($classLike->stmts, ClassConstFetch::class); foreach ($classConstFetches as $classConstFetch) { - dump($classConstFetch); - dump($node); + if (! $this->nodeNameResolver->areNamesEqual($classConstFetch->name, $node->consts[0]->name)) { + continue; + } + + $constFetchClassType = $this->nodeTypeResolver->resolve($classConstFetch->class); + + // constant is used! + if ($constFetchClassType->isSuperTypeOf($classObjectType)->yes()) { + return null; + } } -// // $directUsingClassReflections = $this->nodeRepository->findDirectClassConstantFetches( // $classReflection, // $constant