From 6fba2c610c6da658795c25e2315a71a31ffcf359 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 27 Mar 2020 15:27:05 +0100 Subject: [PATCH 1/4] cannot work with non-existing class --- .../class_imported_as_alias_without_namespace.php.inc | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/class_imported_as_alias_without_namespace.php.inc diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/class_imported_as_alias_without_namespace.php.inc b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/class_imported_as_alias_without_namespace.php.inc deleted file mode 100644 index 93712466a5e3..000000000000 --- a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/class_imported_as_alias_without_namespace.php.inc +++ /dev/null @@ -1,5 +0,0 @@ - Date: Fri, 27 Mar 2020 15:29:50 +0100 Subject: [PATCH 2/4] unused import is responsibility of coding standard --- .../FixtureAutoImportNames/class_imported_as_alias.php.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/class_imported_as_alias.php.inc b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/class_imported_as_alias.php.inc index 76b9154d3b27..9e627b13d321 100644 --- a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/class_imported_as_alias.php.inc +++ b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/class_imported_as_alias.php.inc @@ -16,6 +16,7 @@ namespace Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Fixture\AutoImpo use Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\SecondNamespace\SecondOriginalClass; use Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\FirstNamespace\FirstOriginalClass as AliasedClass; +use Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\SecondNamespace; $aliasedClass = new AliasedClass(); $secondClass = new SecondOriginalClass(); From 31023ee27b21113a20f4b11c6cdaee2bc43eb9b1 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 27 Mar 2020 15:59:09 +0100 Subject: [PATCH 3/4] # This is a combination of 2 commits. # This is the 1st commit message: fix importing of non-existing classes # The commit message #2 will be skipped: # fixup! fix importing of non-existing classes --- composer.json | 4 +- docs/AllRectorsOverview.md | 2 +- rules/coding-style/src/Node/NameImporter.php | 26 +++++++++- .../Source/Foo.php | 10 ++++ .../Source/safe_count.php | 15 ++++++ .../double_funciton_import.php.inc | 38 -------------- ..._existing_class_without_namespace.php.inc} | 0 .../double_funciton_import.php.inc | 38 ++++++++++++++ .../FunctionAutoImportNamesParameterTest.php | 49 +++++++++++++++++++ .../Source/some_another_function.php | 13 +++++ 10 files changed, 154 insertions(+), 41 deletions(-) create mode 100644 rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/Foo.php create mode 100644 rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/safe_count.php delete mode 100644 rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/double_funciton_import.php.inc rename rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/{class_imported_as_alias_without_namespace_with_false_condition.php.inc => skip_non_existing_class_without_namespace.php.inc} (100%) create mode 100644 rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNamesFunction/double_funciton_import.php.inc create mode 100644 rules/renaming/tests/Rector/Class_/RenameClassRector/FunctionAutoImportNamesParameterTest.php create mode 100644 rules/renaming/tests/Rector/Class_/RenameClassRector/Source/some_another_function.php diff --git a/composer.json b/composer.json index 8f3c979c31d2..24633481053b 100644 --- a/composer.json +++ b/composer.json @@ -220,7 +220,9 @@ "rules/renaming/tests/Rector/Class_/RenameClassRector/Source/TwigFilter.php", "rules/renaming/tests/Rector/Class_/RenameClassRector/Source/Manual_Twig_Filter.php", "rules/solid/tests/Rector/ClassMethod/UseInterfaceOverImplementationInConstructorRector/Source/Apple.php", - "tests/Rector/Namespace_/PseudoNamespaceToNamespaceRector/Source/ChangeMeAnotherNamespace.php" + "tests/Rector/Namespace_/PseudoNamespaceToNamespaceRector/Source/ChangeMeAnotherNamespace.php", + "rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/Foo.php", + "rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/safe_count.php" ] }, "scripts": { diff --git a/docs/AllRectorsOverview.md b/docs/AllRectorsOverview.md index 2a28a235f692..0e5fccdefc65 100644 --- a/docs/AllRectorsOverview.md +++ b/docs/AllRectorsOverview.md @@ -7464,7 +7464,7 @@ Changes property `@var` annotations from annotation to type. - class: [`Rector\Php80\Rector\NotIdentical\StrContainsRector`](/../master/rules/php80/src/Rector/NotIdentical/StrContainsRector.php) - [test fixtures](/../master/rules/php80/tests/Rector/NotIdentical/StrContainsRector/Fixture) -Replace strpos() !== false, strstr and preg_match() with str_contains() +Replace strpos() !== false and strstr() with str_contains() ```diff class SomeClass diff --git a/rules/coding-style/src/Node/NameImporter.php b/rules/coding-style/src/Node/NameImporter.php index 326c893f48d1..2cd7deb38af5 100644 --- a/rules/coding-style/src/Node/NameImporter.php +++ b/rules/coding-style/src/Node/NameImporter.php @@ -7,6 +7,7 @@ use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; +use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\UseUse; use Rector\CodingStyle\Application\UseAddingCommander; @@ -14,6 +15,7 @@ use Rector\CodingStyle\Imports\ImportSkipper; use Rector\Core\Configuration\Option; use Rector\NodeNameResolver\NodeNameResolver; +use Rector\NodeTypeResolver\ClassExistenceStaticHelper; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PHPStan\Type\FullyQualifiedObjectType; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -95,7 +97,7 @@ private function shouldSkipName(Name $name): bool } // is scalar name? - if (in_array($name->toString(), ['true', 'false', 'bool'], true)) { + if (in_array($name->toLowerString(), ['true', 'false', 'bool'], true)) { return true; } @@ -117,6 +119,10 @@ private function shouldSkipName(Name $name): bool } } + if ($this->isNonExistingClassLikeAndFunctionFullyQualifiedName($name)) { + return true; + } + return false; } @@ -187,4 +193,22 @@ private function addUseImport(Name $name, FullyQualifiedObjectType $fullyQualifi $this->useAddingCommander->addUseImport($name, $fullyQualifiedObjectType); } } + + private function isNonExistingClassLikeAndFunctionFullyQualifiedName(Name $name): bool + { + if (! $name instanceof FullyQualified) { + return false; + } + + // skip-non existing class-likes and functions + if (ClassExistenceStaticHelper::doesClassLikeExist($name->toString())) { + return false; + } + + if (function_exists($name->toString())) { + return false; + } + + return true; + } } diff --git a/rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/Foo.php b/rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/Foo.php new file mode 100644 index 000000000000..1d66cfb87142 --- /dev/null +++ b/rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/Foo.php @@ -0,0 +1,10 @@ + ------ - diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/class_imported_as_alias_without_namespace_with_false_condition.php.inc b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/skip_non_existing_class_without_namespace.php.inc similarity index 100% rename from rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/class_imported_as_alias_without_namespace_with_false_condition.php.inc rename to rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNames/skip_non_existing_class_without_namespace.php.inc diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNamesFunction/double_funciton_import.php.inc b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNamesFunction/double_funciton_import.php.inc new file mode 100644 index 000000000000..791dd2966db4 --- /dev/null +++ b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureAutoImportNamesFunction/double_funciton_import.php.inc @@ -0,0 +1,38 @@ + +----- + diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/FunctionAutoImportNamesParameterTest.php b/rules/renaming/tests/Rector/Class_/RenameClassRector/FunctionAutoImportNamesParameterTest.php new file mode 100644 index 000000000000..c0d7fecf4d04 --- /dev/null +++ b/rules/renaming/tests/Rector/Class_/RenameClassRector/FunctionAutoImportNamesParameterTest.php @@ -0,0 +1,49 @@ +doTestFile($filePath); + } + + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureAutoImportNamesFunction'); + } + + protected function getAutoImportNames(): ?bool + { + return true; + } + + /** + * @return mixed[] + */ + protected function getRectorsWithConfiguration(): array + { + return [ + RenameClassRector::class => [ + '$oldToNewClasses' => [ + OldClass::class => NewClass::class, + ], + ], + ]; + } +} diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/Source/some_another_function.php b/rules/renaming/tests/Rector/Class_/RenameClassRector/Source/some_another_function.php new file mode 100644 index 000000000000..8445d29c2118 --- /dev/null +++ b/rules/renaming/tests/Rector/Class_/RenameClassRector/Source/some_another_function.php @@ -0,0 +1,13 @@ + Date: Fri, 27 Mar 2020 16:42:58 +0100 Subject: [PATCH 4/4] fix importing of non-existing classes --- composer.json | 4 +- rules/coding-style/src/Node/NameImporter.php | 15 +++---- .../Fixture/bootstrap_names.php.inc | 42 ++++++++++++------- .../Source/AnotherClass.php | 10 +++++ .../Source/YetAnotherClass.php | 10 +++++ 5 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/AnotherClass.php create mode 100644 rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/YetAnotherClass.php diff --git a/composer.json b/composer.json index 24633481053b..31c5838bffe0 100644 --- a/composer.json +++ b/composer.json @@ -222,7 +222,9 @@ "rules/solid/tests/Rector/ClassMethod/UseInterfaceOverImplementationInConstructorRector/Source/Apple.php", "tests/Rector/Namespace_/PseudoNamespaceToNamespaceRector/Source/ChangeMeAnotherNamespace.php", "rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/Foo.php", - "rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/safe_count.php" + "rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/safe_count.php", + "rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/AnotherClass.php", + "rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/YetAnotherClass.php" ] }, "scripts": { diff --git a/rules/coding-style/src/Node/NameImporter.php b/rules/coding-style/src/Node/NameImporter.php index 2cd7deb38af5..4c5e90ec49a5 100644 --- a/rules/coding-style/src/Node/NameImporter.php +++ b/rules/coding-style/src/Node/NameImporter.php @@ -109,9 +109,12 @@ private function shouldSkipName(Name $name): bool return true; } + if ($this->isNonExistingClassLikeAndFunctionFullyQualifiedName($name)) { + return true; + } + // Importing root namespace classes (like \DateTime) is optional $importShortClasses = $this->parameterProvider->provideParameter(Option::IMPORT_SHORT_CLASSES_PARAMETER); - if (! $importShortClasses) { $name = $this->nodeNameResolver->getName($name); if ($name !== null && substr_count($name, '\\') === 0) { @@ -119,10 +122,6 @@ private function shouldSkipName(Name $name): bool } } - if ($this->isNonExistingClassLikeAndFunctionFullyQualifiedName($name)) { - return true; - } - return false; } @@ -205,10 +204,6 @@ private function isNonExistingClassLikeAndFunctionFullyQualifiedName(Name $name) return false; } - if (function_exists($name->toString())) { - return false; - } - - return true; + return ! function_exists($name->toString()); } } diff --git a/rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/bootstrap_names.php.inc b/rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/bootstrap_names.php.inc index 050e1f2d5c34..ac13a084ab35 100644 --- a/rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/bootstrap_names.php.inc +++ b/rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/bootstrap_names.php.inc @@ -1,30 +1,42 @@ ----- diff --git a/rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/AnotherClass.php b/rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/AnotherClass.php new file mode 100644 index 000000000000..85546c1d4a3d --- /dev/null +++ b/rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/AnotherClass.php @@ -0,0 +1,10 @@ +