Fix fqn doc with alraedy PHP imported namespace (#1891)

Fix fqn doc with alraedy PHP imported namespace
This commit is contained in:
Tomáš Votruba 2019-08-24 12:38:08 +02:00 committed by GitHub
commit ca1cff688c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 9 deletions

View File

@ -13,12 +13,16 @@ use Rector\CodingStyle\Application\UseAddingCommander;
use Rector\CodingStyle\Imports\AliasUsesResolver;
use Rector\CodingStyle\Imports\ShortNameResolver;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\ImportFullyQualifiedNamesRectorTest;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see ImportFullyQualifiedNamesRectorTest
*/
final class ImportFullyQualifiedNamesRector extends AbstractRector
{
/**

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;
final class IncludeUsedLocalClass
{
/**
* @var \Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture\SharedShortName
*/
private $join;
public function __construct(SharedShortName $join)
{
}
public function getJoin(): SharedShortName
{
return $this->join;
}
}
?>
-----
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;
final class IncludeUsedLocalClass
{
/**
* @var SharedShortName
*/
private $join;
public function __construct(SharedShortName $join)
{
}
public function getJoin(): SharedShortName
{
return $this->join;
}
}
?>

View File

@ -22,6 +22,7 @@ final class ImportFullyQualifiedNamesRectorTest extends AbstractRectorTestCase
// same short class with namespace
yield [__DIR__ . '/Fixture/same_namespaced_class.php.inc'];
yield [__DIR__ . '/Fixture/skip_same_namespaced_used_class.php.inc'];
yield [__DIR__ . '/Fixture/include_used_local_class.php.inc'];
yield [__DIR__ . '/Fixture/fixture.php.inc'];
yield [__DIR__ . '/Fixture/double_import.php.inc'];

View File

@ -712,13 +712,17 @@ final class DocBlockManipulator
$namespaceName = $node->getAttribute(AttributeKey::NAMESPACE_NAME);
// the class in the same namespace as different file can se used in this code, the short names would colide → skip
if (class_exists($namespaceName . '\\' . $shortName)) {
if ($this->isCurrentNamespaceSameShortClassAlreadyUsed(
$node,
$namespaceName . '\\' . $shortName,
$shortName
)) {
return $attributeAwareNode;
$currentNamespaceShortName = $namespaceName . '\\' . $shortName;
if (class_exists($currentNamespaceShortName)) {
if ($currentNamespaceShortName !== $fullyQualifiedName) {
if ($this->isCurrentNamespaceSameShortClassAlreadyUsed(
$node,
$currentNamespaceShortName,
$shortName
)) {
return $attributeAwareNode;
}
}
}
@ -751,11 +755,11 @@ final class DocBlockManipulator
$joinChar = '|'; // default
if (Strings::contains($type, '|')) { // intersection
$types = explode('|', $type);
$joinChar = '|';
$types = explode($joinChar, $type);
} elseif (Strings::contains($type, '&')) { // union
$types = explode('&', $type);
$joinChar = '&';
$types = explode($joinChar, $type);
} else {
$types = [$type];
}