diff --git a/rules/CodingStyle/Node/NameImporter.php b/rules/CodingStyle/Node/NameImporter.php index 945255bc378..dc5821210aa 100644 --- a/rules/CodingStyle/Node/NameImporter.php +++ b/rules/CodingStyle/Node/NameImporter.php @@ -9,6 +9,7 @@ use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\GroupUse; use PhpParser\Node\Stmt\Use_; +use PHPStan\Analyser\Scope; use Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper; use Rector\Naming\Naming\AliasNameResolver; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -90,6 +91,12 @@ private function importNameAndCollectNewUseStatement( return $nameInUse; } + $nameInNamespacedScope = $this->resolveNameInNamespacedScope($fullyQualified); + if ($nameInNamespacedScope instanceof Name) { + $nameInNamespacedScope->setAttribute(AttributeKey::NAMESPACED_NAME, $fullyQualified->toString()); + return $nameInNamespacedScope; + } + // the same end is already imported → skip if ($this->classNameImportSkipper->shouldSkipNameForFullyQualifiedObjectType( $file, @@ -111,6 +118,34 @@ private function importNameAndCollectNewUseStatement( return $fullyQualifiedObjectType->getShortNameNode(); } + private function resolveNameInNamespacedScope(FullyQualified $fullyQualified): ?Name + { + /** + * Note: Don't use ScopeFetcher::fetch() on Name instance, + * Scope can be null on Name + * This is part of ScopeAnalyzer::NON_REFRESHABLE_NODES + * @see https://github.com/rectorphp/rector-src/blob/9929af7c0179929b4fde6915cb7a06c3141dde6c/src/NodeAnalyzer/ScopeAnalyzer.php#L17 + */ + $scope = $fullyQualified->getAttribute(AttributeKey::SCOPE); + if (! $scope instanceof Scope) { + return null; + } + + $namespace = $scope->getNamespace(); + if ($namespace === null) { + return null; + } + + $shortName = $fullyQualified->getLast(); + $namepaceFullyQualifiedName = substr($fullyQualified->toString(), 0, -strlen($shortName) - 1); + + if ($namepaceFullyQualifiedName === $namespace) { + return new Name($shortName); + } + + return null; + } + private function addUseImport( File $file, FullyQualified $fullyQualified, diff --git a/tests/Issues/AutoImport/Fixture/fqcn_current_same_class.php.inc b/tests/Issues/AutoImport/Fixture/fqcn_current_same_class.php.inc new file mode 100644 index 00000000000..bdab86555ff --- /dev/null +++ b/tests/Issues/AutoImport/Fixture/fqcn_current_same_class.php.inc @@ -0,0 +1,21 @@ + +----- +