From 49aa15dbfb1932401ae68310c4064c273259ea4e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 5 Nov 2021 15:19:53 +0100 Subject: [PATCH] [CodingStyle] Deprecate RemoveUnusedAliasRector, job rather for coding standard tool and opinonated (#1157) --- .../docs/rector_rules_overview.md | 42 +-- config/set/coding-style.php | 2 - .../UseAnalyzer/Fixture/some_uses.php.inc | 15 -- .../Fixture/union_alias_imports.php.inc | 16 -- .../UseAnalyzer/Fixture/use_import.php.inc | 16 -- .../UseAnalyzer/Source/FirstUsage.php | 10 - .../UseAnalyzer/UseAnalyzerTest.php | 124 --------- .../NodeAnalyzer/UseAnalyzer.php | 119 --------- .../ValueObject/NameAndParent.php | 37 --- .../Fixture/fixture.php.inc | 35 --- .../Fixture/handle_static_cal.php.inc | 31 --- .../Fixture/interace_extending.php.inc | 23 -- .../Fixture/keep_nette_symfony_finder.php.inc | 19 -- .../Fixture/keep_used_doc_param.php.inc | 17 -- .../Fixture/keep_used_spl_file_info.php.inc | 17 -- .../Fixture/nullable_alias.php.inc | 31 --- .../skip_alias_as_used_namespace.php.inc | 17 -- .../Fixture/skip_class_name.php.inc | 9 - ...kip_different_namespaces_same_name.php.inc | 15 -- ...fferent_namespaces_same_name_const.php.inc | 11 - ..._same_name_const_without_namespace.php.inc | 9 - .../Fixture/skip_doc_block.php.inc | 13 - .../Fixture/skip_doc_block_uniontype.php.inc | 19 -- .../skip_has_conflict_class_contant.php.inc | 23 -- ...kip_nette_symfony_filesystem_alias.php.inc | 26 -- .../skip_no_namespace_class_name.php.inc | 7 - ...me_class_in_use_statement_not_used.php.inc | 14 - .../Fixture/skip_trait_name.php.inc | 10 - .../Fixture/skip_used.php.inc | 15 -- .../Fixture/skip_vich_annotation.php.inc | 26 -- .../Fixture/under_intanceof.php.inc | 33 --- .../Fixture/unneeded_trait_name.php.inc | 27 -- .../Fixture/used_in_class_constant.php.inc | 45 ---- .../Fixture/used_union_type.php.inc | 31 --- .../RemoveUnusedAliasRectorTest.php | 33 --- .../Source/AbstractInterface.php | 10 - .../Source/AbstractKernel.php | 10 - .../Source/AliasedStaticCall.php | 12 - .../Source/Another/AbstractKernel.php | 10 - .../Source/ClassicTrait.php | 10 - .../DifferentNamespace/StandaloneClass.php | 10 - .../Source/Mapping.php | 10 - .../Source/StandaloneClass.php | 10 - .../Source/StandaloneTrait.php | 10 - .../config/configured_rule.php | 11 - rules/CodingStyle/Naming/NameRenamer.php | 215 --------------- rules/CodingStyle/Node/DocAliasResolver.php | 93 ------- .../Node/UseNameAliasToNameResolver.php | 42 --- .../Rector/Use_/RemoveUnusedAliasRector.php | 244 ------------------ .../FullyQualifiedFromUseFinder.php | 39 --- 50 files changed, 22 insertions(+), 1651 deletions(-) delete mode 100644 packages-tests/NameImporting/NodeAnalyzer/UseAnalyzer/Fixture/some_uses.php.inc delete mode 100644 packages-tests/NameImporting/NodeAnalyzer/UseAnalyzer/Fixture/union_alias_imports.php.inc delete mode 100644 packages-tests/NameImporting/NodeAnalyzer/UseAnalyzer/Fixture/use_import.php.inc delete mode 100644 packages-tests/NameImporting/NodeAnalyzer/UseAnalyzer/Source/FirstUsage.php delete mode 100644 packages-tests/NameImporting/NodeAnalyzer/UseAnalyzer/UseAnalyzerTest.php delete mode 100644 packages/NameImporting/NodeAnalyzer/UseAnalyzer.php delete mode 100644 packages/NameImporting/ValueObject/NameAndParent.php delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/handle_static_cal.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/interace_extending.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/keep_nette_symfony_finder.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/keep_used_doc_param.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/keep_used_spl_file_info.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/nullable_alias.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_alias_as_used_namespace.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_class_name.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_different_namespaces_same_name.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_different_namespaces_same_name_const.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_different_namespaces_same_name_const_without_namespace.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_doc_block.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_doc_block_uniontype.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_has_conflict_class_contant.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_nette_symfony_filesystem_alias.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_no_namespace_class_name.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_same_class_in_use_statement_not_used.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_trait_name.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_used.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_vich_annotation.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/under_intanceof.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/unneeded_trait_name.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/used_in_class_constant.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/used_union_type.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/RemoveUnusedAliasRectorTest.php delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/AbstractInterface.php delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/AbstractKernel.php delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/AliasedStaticCall.php delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/Another/AbstractKernel.php delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/ClassicTrait.php delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/DifferentNamespace/StandaloneClass.php delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/Mapping.php delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/StandaloneClass.php delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/StandaloneTrait.php delete mode 100644 rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/config/configured_rule.php delete mode 100644 rules/CodingStyle/Naming/NameRenamer.php delete mode 100644 rules/CodingStyle/Node/DocAliasResolver.php delete mode 100644 rules/CodingStyle/Node/UseNameAliasToNameResolver.php delete mode 100644 rules/CodingStyle/Rector/Use_/RemoveUnusedAliasRector.php delete mode 100644 src/PhpParser/NodeFinder/FullyQualifiedFromUseFinder.php diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index 29896715e1c..1980c12a3d5 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -12,7 +12,7 @@ - [CodeQuality](#codequality) (69) -- [CodingStyle](#codingstyle) (39) +- [CodingStyle](#codingstyle) (38) - [Composer](#composer) (6) @@ -76,7 +76,7 @@ - [Php80](#php80) (17) -- [Php81](#php81) (5) +- [Php81](#php81) (6) - [PhpSpecToPHPUnit](#phpspectophpunit) (7) @@ -2352,24 +2352,6 @@ Non-magic PHP object methods cannot start with "__"
-### RemoveUnusedAliasRector - -Removes unused use aliases. Keep annotation aliases like "Doctrine\ORM\Mapping as ORM" to keep convention format - -- class: [`Rector\CodingStyle\Rector\Use_\RemoveUnusedAliasRector`](../rules/CodingStyle/Rector/Use_/RemoveUnusedAliasRector.php) - -```diff --use Symfony\Kernel as BaseKernel; -+use Symfony\Kernel; - --class SomeClass extends BaseKernel -+class SomeClass extends Kernel - { - } -``` - -
- ### ReturnArrayClassMethodToYieldRector Turns array return to yield return in specific type and method @@ -8025,6 +8007,26 @@ Refactor MyCLabs enum fetch to Enum const
+### Php81ResourceReturnToObjectRector + +Change `is_resource()` to instanceof Object + +- class: [`Rector\Php81\Rector\FuncCall\Php81ResourceReturnToObjectRector`](../rules/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector.php) + +```diff + class SomeClass + { + public function run() + { + $f = finfo_open(); +- is_resource($f); ++ $f instanceof \finfo; + } + } +``` + +
+ ### ReadOnlyPropertyRector Decorate read-only property with `readonly` attribute diff --git a/config/set/coding-style.php b/config/set/coding-style.php index 986cd11d604..0b65b87f349 100644 --- a/config/set/coding-style.php +++ b/config/set/coding-style.php @@ -30,7 +30,6 @@ use Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector; use Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector; use Rector\CodingStyle\Rector\Ternary\TernaryConditionVariableAssignmentRector; -use Rector\CodingStyle\Rector\Use_\RemoveUnusedAliasRector; use Rector\CodingStyle\Rector\Use_\SeparateMultiUseImportsRector; use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector; use Rector\Transform\Rector\FuncCall\FuncCallToConstFetchRector; @@ -43,7 +42,6 @@ $services->set(BinarySwitchToIfElseRector::class); $services->set(ConsistentImplodeRector::class); $services->set(TernaryConditionVariableAssignmentRector::class); - $services->set(RemoveUnusedAliasRector::class); $services->set(SymplifyQuoteEscapeRector::class); $services->set(SplitGroupedConstantsAndPropertiesRector::class); $services->set(SplitStringClassConstantToClassConstFetchRector::class); diff --git a/packages-tests/NameImporting/NodeAnalyzer/UseAnalyzer/Fixture/some_uses.php.inc b/packages-tests/NameImporting/NodeAnalyzer/UseAnalyzer/Fixture/some_uses.php.inc deleted file mode 100644 index f7483e5a476..00000000000 --- a/packages-tests/NameImporting/NodeAnalyzer/UseAnalyzer/Fixture/some_uses.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -boot(); - - $this->useAnalyzer = $this->getService(UseAnalyzer::class); - $this->testingParser = $this->getService(TestingParser::class); - } - - /** - * @dataProvider provideData() - * - * @param class-string $parentNodeType - * @param Identifier|FullyQualified $expectedNameNode - */ - public function test( - string $filePath, - string $expectedShortName, - int $position, - Name|Identifier $expectedNameNode, - string $parentNodeType - ): void { - $file = $this->testingParser->parseFilePathToFile($filePath); - $firstStmt = $file->getOldStmts()[1]; - - $namesAndParents = $this->useAnalyzer->resolveUsedNameNodes($firstStmt); - - $this->assertArrayHasKey($position, $namesAndParents); - $nameAndParent = $namesAndParents[$position]; - $this->assertInstanceOf(NameAndParent::class, $nameAndParent); - - $this->assertTrue($nameAndParent->matchShortName($expectedShortName)); - - // remove attributes for compare - $nameNode = $nameAndParent->getNameNode(); - $nameNode->setAttributes([]); - - $this->assertEquals($expectedNameNode, $nameNode); - $this->assertInstanceOf($parentNodeType, $nameAndParent->getParentNode()); - } - - public function provideData(): Iterator - { - yield [ - __DIR__ . '/Fixture/some_uses.php.inc', - 'FirstUsage', - 0, - new FullyQualified(FirstUsage::class), - New_::class, - ]; - - yield [__DIR__ . '/Fixture/some_uses.php.inc', 'SomeUses', 1, new Identifier('SomeUses'), Class_::class]; - - yield [ - __DIR__ . '/Fixture/use_import.php.inc', - 'BaseKernel', - 4, - new FullyQualified(AbstractKernel::class), - New_::class, - ]; - - yield [ - __DIR__ . '/Fixture/use_import.php.inc', - 'BaseInterface', - 6, - new Identifier('BaseInterface'), - UseUse::class, - ]; - - yield [ - __DIR__ . '/Fixture/use_import.php.inc', - 'BaseKernel', - 7, - new Identifier('BaseKernel'), - UseUse::class, - ]; - - yield [__DIR__ . '/Fixture/use_import.php.inc', 'SomeClass', 5, new Identifier('SomeClass'), Class_::class]; - - yield [ - __DIR__ . '/Fixture/union_alias_imports.php.inc', - 'String_', - 0, - new FullyQualified(String_::class), - UnionType::class, - ]; - - yield [ - __DIR__ . '/Fixture/union_alias_imports.php.inc', - 'PhpParserUnionType', - 3, - new Identifier('PhpParserUnionType'), - UseUse::class, - ]; - } -} diff --git a/packages/NameImporting/NodeAnalyzer/UseAnalyzer.php b/packages/NameImporting/NodeAnalyzer/UseAnalyzer.php deleted file mode 100644 index 7932ae144a8..00000000000 --- a/packages/NameImporting/NodeAnalyzer/UseAnalyzer.php +++ /dev/null @@ -1,119 +0,0 @@ -resolveUsedNames($node); - $usedClassNames = $this->resolveUsedClassNames($node); - $usedTraitNames = $this->resolveTraitUseNames($node); - - return array_merge($usedNames, $usedClassNames, $usedTraitNames); - } - - /** - * @return NameAndParent[] - */ - private function resolveUsedNames(Node $node): array - { - $namesAndParents = []; - - /** @var Name[] $names */ - $names = $this->betterNodeFinder->findInstanceOf($node, Name::class); - - foreach ($names as $name) { - /** node name before becoming FQN - attribute from @see NameResolver */ - $originalName = $name->getAttribute(AttributeKey::ORIGINAL_NAME); - if (! $originalName instanceof Name) { - continue; - } - - $parentNode = $name->getAttribute(AttributeKey::PARENT_NODE); - if (! $parentNode instanceof Node) { - throw new ShouldNotHappenException(); - } - - $shortName = $originalName->toString(); - $namesAndParents[] = new NameAndParent($shortName, $name, $parentNode); - } - - return $namesAndParents; - } - - /** - * @return NameAndParent[] - */ - private function resolveUsedClassNames(Node $node): array - { - $namesAndParents = []; - - /** @var ClassLike[] $classLikes */ - $classLikes = $this->betterNodeFinder->findClassLikes($node); - - foreach ($classLikes as $classLike) { - $classLikeName = $classLike->name; - if (! $classLikeName instanceof Identifier) { - continue; - } - - $name = $this->nodeNameResolver->getName($classLikeName); - if (! is_string($name)) { - continue; - } - - $namesAndParents[] = new NameAndParent($name, $classLikeName, $classLike); - } - - return $namesAndParents; - } - - /** - * @return NameAndParent[] - */ - private function resolveTraitUseNames(Node $node): array - { - $namesAndParents = []; - - /** @var Identifier[] $identifiers */ - $identifiers = $this->betterNodeFinder->findInstanceOf($node, Identifier::class); - - foreach ($identifiers as $identifier) { - $parentNode = $identifier->getAttribute(AttributeKey::PARENT_NODE); - if (! $parentNode instanceof UseUse) { - continue; - } - - $shortName = $identifier->name; - $namesAndParents[] = new NameAndParent($shortName, $identifier, $parentNode); - } - - return $namesAndParents; - } -} diff --git a/packages/NameImporting/ValueObject/NameAndParent.php b/packages/NameImporting/ValueObject/NameAndParent.php deleted file mode 100644 index bce9aec2e59..00000000000 --- a/packages/NameImporting/ValueObject/NameAndParent.php +++ /dev/null @@ -1,37 +0,0 @@ -nameNode; - } - - public function getParentNode(): Node - { - return $this->parentNode; - } - - public function matchShortName(string $desiredShortName): bool - { - return strtolower($this->shortName) === strtolower($desiredShortName); - } -} diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/fixture.php.inc b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/fixture.php.inc deleted file mode 100644 index 58c7ff66581..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,35 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/handle_static_cal.php.inc b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/handle_static_cal.php.inc deleted file mode 100644 index 6695608dc60..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/handle_static_cal.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/interace_extending.php.inc b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/interace_extending.php.inc deleted file mode 100644 index 0e2c897ff7c..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/interace_extending.php.inc +++ /dev/null @@ -1,23 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/keep_nette_symfony_finder.php.inc b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/keep_nette_symfony_finder.php.inc deleted file mode 100644 index fe7f0a28d17..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/keep_nette_symfony_finder.php.inc +++ /dev/null @@ -1,19 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_alias_as_used_namespace.php.inc b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_alias_as_used_namespace.php.inc deleted file mode 100644 index 926aa9b0468..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_alias_as_used_namespace.php.inc +++ /dev/null @@ -1,17 +0,0 @@ - diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_class_name.php.inc b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_class_name.php.inc deleted file mode 100644 index 0e72c346160..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_class_name.php.inc +++ /dev/null @@ -1,9 +0,0 @@ - 'bar', - NoAlias::class => 'noalias', - ]; - - public function run() - { - Bar::execute(); - Bar::DATA; - } -} - -?> diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_nette_symfony_filesystem_alias.php.inc b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_nette_symfony_filesystem_alias.php.inc deleted file mode 100644 index de7546e2d15..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_nette_symfony_filesystem_alias.php.inc +++ /dev/null @@ -1,26 +0,0 @@ -filesystem = $filesystem; - } - - public function fixComposerJson(string $composerJsonFile): void - { - $fileContent = NetteFileSystem::read($composerJsonFile); - $this->filesystem->dumpFile($composerJsonFile, '...'); - } -} diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_no_namespace_class_name.php.inc b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_no_namespace_class_name.php.inc deleted file mode 100644 index 57610242799..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_no_namespace_class_name.php.inc +++ /dev/null @@ -1,7 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/unneeded_trait_name.php.inc b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/unneeded_trait_name.php.inc deleted file mode 100644 index c1534df267a..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/unneeded_trait_name.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/used_in_class_constant.php.inc b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/used_in_class_constant.php.inc deleted file mode 100644 index df23ea43150..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/used_in_class_constant.php.inc +++ /dev/null @@ -1,45 +0,0 @@ - 'bar', - NoAlias::class => 'noalias', - ]; - - public function run() - { - BarAlias::execute(); - BarAlias::DATA; - } -} - -?> ------ - 'bar', - NoAlias::class => 'noalias', - ]; - - public function run() - { - Bar::execute(); - Bar::DATA; - } -} - -?> diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/used_union_type.php.inc b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/used_union_type.php.inc deleted file mode 100644 index c9309fc6956..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/used_union_type.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/RemoveUnusedAliasRectorTest.php b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/RemoveUnusedAliasRectorTest.php deleted file mode 100644 index 10cab2e24f7..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/RemoveUnusedAliasRectorTest.php +++ /dev/null @@ -1,33 +0,0 @@ -doTestFileInfo($fileInfo); - } - - /** - * @return Iterator - */ - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/AbstractInterface.php b/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/AbstractInterface.php deleted file mode 100644 index e68a52d1ea7..00000000000 --- a/rules-tests/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Source/AbstractInterface.php +++ /dev/null @@ -1,10 +0,0 @@ -services(); - $services->set(RemoveUnusedAliasRector::class); -}; diff --git a/rules/CodingStyle/Naming/NameRenamer.php b/rules/CodingStyle/Naming/NameRenamer.php deleted file mode 100644 index e68bd4e4a02..00000000000 --- a/rules/CodingStyle/Naming/NameRenamer.php +++ /dev/null @@ -1,215 +0,0 @@ -getParentNode(); - $usedName = $nameAndParent->getNameNode(); - - if ($parentNode instanceof TraitUse) { - $this->renameTraitUse($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof Class_) { - $this->renameClass($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof Param) { - $this->renameParam($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof New_) { - $this->renameNew($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof ClassMethod) { - $this->renameClassMethod($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof Interface_) { - $this->renameInterface($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof StaticCall) { - $this->renameStaticCall($lastName, $parentNode); - } - - if ($parentNode instanceof UnionType) { - $this->renameUnionType($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof NullableType) { - $this->renameNullableType($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof Instanceof_) { - $this->renameInInstanceOf($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof ClassConstFetch) { - $this->renameClassConstFetch($lastName, $parentNode, $usedName); - } - } - } - - private function renameClassConstFetch( - string $lastName, - ClassConstFetch $classConstFetch, - Name | Identifier $usedNameNode - ): void { - if (! $this->nodeNameResolver->areNamesEqual($classConstFetch->class, $usedNameNode)) { - return; - } - - $classConstFetch->class = new Name($lastName); - } - - private function renameInInstanceOf( - string $lastName, - Instanceof_ $instanceof, - Name | Identifier $usedNameNode - ): void { - if (! $this->nodeNameResolver->areNamesEqual($instanceof->class, $usedNameNode)) { - return; - } - - $instanceof->class = new Name($lastName); - } - - private function renameNullableType( - string $lastName, - NullableType $nullableType, - Name | Identifier $usedNameNode - ): void { - if (! $this->nodeNameResolver->areNamesEqual($nullableType->type, $usedNameNode)) { - return; - } - - $nullableType->type = new Name($lastName); - } - - private function renameTraitUse(string $lastName, TraitUse $traitUse, Name | Identifier $usedNameNode): void - { - foreach ($traitUse->traits as $key => $traitName) { - if (! $this->nodeNameResolver->areNamesEqual($traitName, $usedNameNode)) { - continue; - } - - $traitUse->traits[$key] = new Name($lastName); - } - } - - private function renameClass(string $lastName, Class_ $class, Name | Identifier $usedNameNode): void - { - if ($class->name !== null && $this->nodeNameResolver->areNamesEqual($class->name, $usedNameNode)) { - $class->name = new Identifier($lastName); - } - - if ($class->extends !== null && $this->nodeNameResolver->areNamesEqual($class->extends, $usedNameNode)) { - $class->extends = new Name($lastName); - } - - foreach ($class->implements as $key => $implementNode) { - if ($this->nodeNameResolver->areNamesEqual($implementNode, $usedNameNode)) { - $class->implements[$key] = new Name($lastName); - } - } - } - - private function renameParam(string $lastName, Param $param, Name | Identifier $usedNameNode): void - { - if ($param->type === null) { - return; - } - - if (! $this->nodeNameResolver->areNamesEqual($param->type, $usedNameNode)) { - return; - } - - $param->type = new Name($lastName); - } - - private function renameUnionType(string $lastName, UnionType $unionType, Identifier|Name $usedNameNode): void - { - foreach ($unionType->types as $key => $unionedType) { - if (! $this->nodeNameResolver->areNamesEqual($unionedType, $usedNameNode)) { - continue; - } - - if (! $unionedType instanceof Name) { - continue; - } - - $unionType->types[$key] = new Name($lastName); - } - } - - private function renameNew(string $lastName, New_ $new, Name | Identifier $usedNameNode): void - { - if ($this->nodeNameResolver->areNamesEqual($new->class, $usedNameNode)) { - $new->class = new Name($lastName); - } - } - - private function renameClassMethod( - string $lastName, - ClassMethod $classMethod, - Name | Identifier $usedNameNode - ): void { - if ($classMethod->returnType === null) { - return; - } - - if (! $this->nodeNameResolver->areNamesEqual($classMethod->returnType, $usedNameNode)) { - return; - } - - $classMethod->returnType = new Name($lastName); - } - - private function renameInterface(string $lastName, Interface_ $interface, Name | Identifier $usedNameNode): void - { - foreach ($interface->extends as $key => $extendInterfaceName) { - if (! $this->nodeNameResolver->areNamesEqual($extendInterfaceName, $usedNameNode)) { - continue; - } - - $interface->extends[$key] = new Name($lastName); - } - } - - private function renameStaticCall(string $lastName, StaticCall $staticCall): void - { - $staticCall->class = new Name($lastName); - } -} diff --git a/rules/CodingStyle/Node/DocAliasResolver.php b/rules/CodingStyle/Node/DocAliasResolver.php deleted file mode 100644 index e4a3295e9d8..00000000000 --- a/rules/CodingStyle/Node/DocAliasResolver.php +++ /dev/null @@ -1,93 +0,0 @@ -\w+)(\\\\)?#s'; - - public function __construct( - private SimpleCallableNodeTraverser $simpleCallableNodeTraverser, - private PhpDocInfoFactory $phpDocInfoFactory - ) { - } - - /** - * @return string[] - */ - public function resolve(Node $node): array - { - $possibleDocAliases = []; - - $this->simpleCallableNodeTraverser->traverseNodesWithCallable($node, function (Node $node) use ( - &$possibleDocAliases - ): void { - $docComment = $node->getDocComment(); - if (! $docComment instanceof Doc) { - return; - } - - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - $possibleDocAliases = $this->collectVarType($phpDocInfo, $possibleDocAliases); - - // e.g. "use Dotrine\ORM\Mapping as ORM" etc. - $matches = Strings::matchAll($docComment->getText(), self::DOC_ALIAS_REGEX); - foreach ($matches as $match) { - $possibleDocAliases[] = $match['possible_alias']; - } - }); - - return array_unique($possibleDocAliases); - } - - /** - * @param string[] $possibleDocAliases - * @return string[] - */ - private function collectVarType(PhpDocInfo $phpDocInfo, array $possibleDocAliases): array - { - $possibleDocAliases = $this->appendPossibleAliases($phpDocInfo->getVarType(), $possibleDocAliases); - $possibleDocAliases = $this->appendPossibleAliases($phpDocInfo->getReturnType(), $possibleDocAliases); - - foreach ($phpDocInfo->getParamTypesByName() as $paramType) { - $possibleDocAliases = $this->appendPossibleAliases($paramType, $possibleDocAliases); - } - - return $possibleDocAliases; - } - - /** - * @param string[] $possibleDocAliases - * @return string[] - */ - private function appendPossibleAliases(Type $varType, array $possibleDocAliases): array - { - if ($varType instanceof AliasedObjectType) { - $possibleDocAliases[] = $varType->getClassName(); - } - - if ($varType instanceof UnionType) { - foreach ($varType->getTypes() as $type) { - $possibleDocAliases = $this->appendPossibleAliases($type, $possibleDocAliases); - } - } - - return $possibleDocAliases; - } -} diff --git a/rules/CodingStyle/Node/UseNameAliasToNameResolver.php b/rules/CodingStyle/Node/UseNameAliasToNameResolver.php deleted file mode 100644 index adb6b017438..00000000000 --- a/rules/CodingStyle/Node/UseNameAliasToNameResolver.php +++ /dev/null @@ -1,42 +0,0 @@ - - */ - public function resolve(File $file): array - { - $useNamesAliasToName = []; - - $shortNames = $this->shortNameResolver->resolveFromFile($file); - foreach ($shortNames as $alias => $useImport) { - if (! is_string($alias)) { - continue; - } - - $shortName = $this->classNaming->getShortName($useImport); - if ($shortName === $alias) { - continue; - } - - $useNamesAliasToName[$shortName][] = $alias; - } - - return $useNamesAliasToName; - } -} diff --git a/rules/CodingStyle/Rector/Use_/RemoveUnusedAliasRector.php b/rules/CodingStyle/Rector/Use_/RemoveUnusedAliasRector.php deleted file mode 100644 index 303bf4b85bc..00000000000 --- a/rules/CodingStyle/Rector/Use_/RemoveUnusedAliasRector.php +++ /dev/null @@ -1,244 +0,0 @@ - - */ - private array $useNamesAliasToName = []; - - /** - * @var string[] - */ - private array $resolvedDocPossibleAliases = []; - - public function __construct( - private DocAliasResolver $docAliasResolver, - private UseAnalyzer $useAnalyzer, - private UseNameAliasToNameResolver $useNameAliasToNameResolver, - private NameRenamer $nameRenamer, - private ClassNameImportSkipper $classNameImportSkipper, - private FullyQualifiedFromUseFinder $fullyQualifiedFromUseFinder - ) { - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition( - 'Removes unused use aliases. Keep annotation aliases like "Doctrine\ORM\Mapping as ORM" to keep convention format', - [ - new CodeSample( - <<<'CODE_SAMPLE' -use Symfony\Kernel as BaseKernel; - -class SomeClass extends BaseKernel -{ -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -use Symfony\Kernel; - -class SomeClass extends Kernel -{ -} -CODE_SAMPLE - ), - ] - ); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [Use_::class]; - } - - /** - * @param Use_ $node - */ - public function refactor(Node $node): ?Node - { - if ($this->shouldSkipUse($node)) { - return null; - } - - $searchNode = $this->resolveSearchNode($node); - if (! $searchNode instanceof Node) { - return null; - } - - $this->namesAndParents = $this->useAnalyzer->resolveUsedNameNodes($searchNode); - $this->resolvedDocPossibleAliases = $this->docAliasResolver->resolve($searchNode); - $this->useNamesAliasToName = $this->useNameAliasToNameResolver->resolve($this->file); - - // lowercase - $this->resolvedDocPossibleAliases = $this->lowercaseArray($this->resolvedDocPossibleAliases); - - $this->useNamesAliasToName = array_change_key_case($this->useNamesAliasToName, CASE_LOWER); - - foreach ($node->uses as $use) { - if ($use->alias === null) { - continue; - } - - $lastName = $use->name->getLast(); - - /** @var string $aliasName */ - $aliasName = $this->getName($use->alias); - if ($this->shouldSkip($node, $lastName, $aliasName)) { - continue; - } - - // only last name is used → no need for alias - $matchedNamesAndParents = $this->matchNamesAndParentsByShort($lastName); - if ($matchedNamesAndParents !== []) { - $use->alias = null; - continue; - } - - $this->refactorAliasName($node, $use->name->toString(), $lastName, $use); - } - - return $node; - } - - private function shouldSkipUse(Use_ $use): bool - { - // skip cases without namespace, problematic to analyse - $namespace = $this->betterNodeFinder->findParentType($use, Namespace_::class); - if (! $namespace instanceof Node) { - return true; - } - - return ! $this->hasUseAlias($use); - } - - /** - * @param string[] $values - * @return string[] - */ - private function lowercaseArray(array $values): array - { - return array_map('strtolower', $values); - } - - private function shouldSkip(Use_ $use, string $lastName, string $aliasName): bool - { - // PHP is case insensitive - $loweredLastName = strtolower($lastName); - $loweredAliasName = strtolower($aliasName); - - // both are used → nothing to remove - if ($this->areBothShortNamesUsed($loweredLastName, $loweredLastName)) { - return true; - } - - // part of some @Doc annotation - if (in_array($loweredAliasName, $this->resolvedDocPossibleAliases, true)) { - return true; - } - - return (bool) $this->fullyQualifiedFromUseFinder->matchAliasNamespace($use, $loweredAliasName); - } - - private function refactorAliasName(Use_ $use, string $fullUseUseName, string $lastName, UseUse $useUse): void - { - $parentUse = $use->getAttribute(AttributeKey::PARENT_NODE); - if (! $parentUse instanceof Node) { - return; - } - - /** @var Use_[] $uses */ - $uses = $this->betterNodeFinder->find($parentUse, function (Node $node) use ($use): bool { - if ($node === $use) { - return false; - } - - return $node instanceof Use_; - }); - - if ($this->classNameImportSkipper->isShortNameInUseStatement(new Name($lastName), $uses)) { - return; - } - - $parentsAndNames = $this->matchNamesAndParentsByShort($fullUseUseName); - if ($parentsAndNames === []) { - return; - } - - $this->nameRenamer->renameNameNode($parentsAndNames, $lastName); - $useUse->alias = null; - } - - private function hasUseAlias(Use_ $use): bool - { - foreach ($use->uses as $useUse) { - if ($useUse->alias !== null) { - return true; - } - } - - return false; - } - - private function resolveSearchNode(Use_ $use): ?Node - { - $searchNode = $use->getAttribute(AttributeKey::PARENT_NODE); - if ($searchNode !== null) { - return $searchNode; - } - - return $use->getAttribute(AttributeKey::NEXT_NODE); - } - - private function areBothShortNamesUsed(string $firstName, string $secondName): bool - { - $firstNamesAndParents = $this->matchNamesAndParentsByShort($firstName); - $secondNamesAndParents = $this->matchNamesAndParentsByShort($secondName); - - return $firstNamesAndParents !== [] && $secondNamesAndParents !== []; - } - - /** - * @return NameAndParent[] - */ - private function matchNamesAndParentsByShort(string $shortName): array - { - return array_filter( - $this->namesAndParents, - fn (NameAndParent $nameAndParent): bool => $nameAndParent->matchShortName($shortName) - ); - } -} diff --git a/src/PhpParser/NodeFinder/FullyQualifiedFromUseFinder.php b/src/PhpParser/NodeFinder/FullyQualifiedFromUseFinder.php deleted file mode 100644 index 72f6e1c7a9f..00000000000 --- a/src/PhpParser/NodeFinder/FullyQualifiedFromUseFinder.php +++ /dev/null @@ -1,39 +0,0 @@ -betterNodeFinder->findFirstNext($use, function (Node $node) use ($loweredAliasName): bool { - if (! $node instanceof FullyQualified) { - return false; - } - - $originalName = $node->getAttribute(AttributeKey::ORIGINAL_NAME); - if (! $originalName instanceof Name) { - return false; - } - - $loweredOriginalName = strtolower($originalName->toString()); - $loweredOriginalNameNamespace = Strings::before($loweredOriginalName, '\\'); - return $loweredAliasName === $loweredOriginalNameNamespace; - }); - } -}