From b8877839ded65338d9253961cc31349c99660548 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 23 Mar 2021 01:32:13 +0100 Subject: [PATCH] cleanup --- .../NodeCollector/NodeRepository.php | 90 +++++++++---------- .../ParsedClassConstFetchNodeCollector.php | 86 +++++++++--------- .../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 +++- 7 files changed, 115 insertions(+), 148 deletions(-) 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..581177c750c5 100644 --- a/packages/NodeCollector/NodeCollector/NodeRepository.php +++ b/packages/NodeCollector/NodeCollector/NodeRepository.php @@ -360,51 +360,51 @@ 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 []; - } +// /** +// * @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 { diff --git a/packages/NodeCollector/NodeCollector/ParsedClassConstFetchNodeCollector.php b/packages/NodeCollector/NodeCollector/ParsedClassConstFetchNodeCollector.php index ad2a9cd77221..5ec7590ddd48 100644 --- a/packages/NodeCollector/NodeCollector/ParsedClassConstFetchNodeCollector.php +++ b/packages/NodeCollector/NodeCollector/ParsedClassConstFetchNodeCollector.php @@ -46,49 +46,49 @@ public function __construct( $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; - } +// 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, 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