From 14f096f8ae25766233e5e45bc2bfce02a2331e19 Mon Sep 17 00:00:00 2001
From: Tomas Votruba <tomas.vot@gmail.com>
Date: Thu, 8 Aug 2019 16:31:31 +0200
Subject: [PATCH] skip same-namespace-short name in
 ImportFullyQualifiedNamesRector

---
 .../Fixture/import_param_doc.php.inc               | 14 +++++++-------
 .../Fixture/import_return_doc.php.inc              |  6 +++---
 .../Source/NormalParamClass.php                    |  8 ++++++++
 .../Source/NormalReturnClass.php                   |  8 ++++++++
 .../PhpDoc/NodeAnalyzer/DocBlockManipulator.php    |  9 +++++++++
 5 files changed, 35 insertions(+), 10 deletions(-)
 create mode 100644 packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/NormalParamClass.php
 create mode 100644 packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/NormalReturnClass.php

diff --git a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/import_param_doc.php.inc b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/import_param_doc.php.inc
index ec37b4b8f92..711bdeb20a6 100644
--- a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/import_param_doc.php.inc
+++ b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/import_param_doc.php.inc
@@ -5,10 +5,10 @@ namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRe
 class ImportParamDoc
 {
     /**
-     * @param \Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\AlreadyClassName $alreadyClassName
-     * @return \Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\AlreadyClassName
+     * @param \Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\NormalParamClass $alreadyClassName
+     * @return \Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\NormalParamClass
      */
-    public function create(\Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\AlreadyClassName $alreadyClassName)
+    public function create(\Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\NormalParamClass $alreadyClassName)
     {
     }
 }
@@ -19,14 +19,14 @@ class ImportParamDoc
 
 namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;
 
-use Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\AlreadyClassName;
+use Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\NormalParamClass;
 class ImportParamDoc
 {
     /**
-     * @param AlreadyClassName $alreadyClassName
-     * @return AlreadyClassName
+     * @param NormalParamClass $alreadyClassName
+     * @return NormalParamClass
      */
-    public function create(AlreadyClassName $alreadyClassName)
+    public function create(NormalParamClass $alreadyClassName)
     {
     }
 }
diff --git a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/import_return_doc.php.inc b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/import_return_doc.php.inc
index eeb43be0e48..138b8b98385 100644
--- a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/import_return_doc.php.inc
+++ b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/import_return_doc.php.inc
@@ -7,7 +7,7 @@ use Rector\CodingStyle\Tests\Rector\Namespace_;
 class ImportReturnDoc
 {
     /**
-     * @return Namespace_\ImportFullyQualifiedNamesRector\Source\AlreadyClassName
+     * @return Namespace_\ImportFullyQualifiedNamesRector\Source\NormalReturnClass
      */
     public function create()
     {
@@ -20,13 +20,13 @@ class ImportReturnDoc
 
 namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;
 
-use Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\AlreadyClassName;
+use Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\NormalReturnClass;
 use Rector\CodingStyle\Tests\Rector\Namespace_;
 
 class ImportReturnDoc
 {
     /**
-     * @return AlreadyClassName
+     * @return NormalReturnClass
      */
     public function create()
     {
diff --git a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/NormalParamClass.php b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/NormalParamClass.php
new file mode 100644
index 00000000000..2a444d75e8b
--- /dev/null
+++ b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/NormalParamClass.php
@@ -0,0 +1,8 @@
+<?php declare(strict_types=1);
+
+namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source;
+
+final class NormalParamClass
+{
+
+}
diff --git a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/NormalReturnClass.php b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/NormalReturnClass.php
new file mode 100644
index 00000000000..2fb5a6cd9d7
--- /dev/null
+++ b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/NormalReturnClass.php
@@ -0,0 +1,8 @@
+<?php declare(strict_types=1);
+
+namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source;
+
+final class NormalReturnClass
+{
+
+}
diff --git a/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php b/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php
index 3bc79ddf67e..3b44a27252e 100644
--- a/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php
+++ b/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php
@@ -35,6 +35,7 @@ use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
 use Rector\CodingStyle\Application\UseAddingCommander;
 use Rector\Exception\ShouldNotHappenException;
 use Rector\NodeTypeResolver\Exception\MissingTagException;
+use Rector\NodeTypeResolver\Node\AttributeKey;
 use Rector\NodeTypeResolver\Php\ParamTypeInfo;
 use Rector\NodeTypeResolver\Php\ReturnTypeInfo;
 use Rector\NodeTypeResolver\Php\VarTypeInfo;
@@ -680,6 +681,14 @@ final class DocBlockManipulator
         string $shortName,
         string $fullyQualifiedName
     ): AttributeAwareNodeInterface {
+        // the name is already in the same namespace implicitly
+        $namespaceName = $node->getAttribute(AttributeKey::NAMESPACE_NAME);
+
+        // the class in the same namespace as differnt file can se used in this code, the short names would colide → skip
+        if (class_exists($namespaceName . '\\' . $shortName)) {
+            return $attributeAwareNode;
+        }
+
         if ($this->useAddingCommander->isShortImported($node, $fullyQualifiedName)) {
             if ($this->useAddingCommander->isImportShortable($node, $fullyQualifiedName)) {
                 $attributeAwareNode->name = $shortName;