[CodingStyle] add partial support already imported support to ImportFullyQualifiedNamesRector (#1447)

[CodingStyle] add partial support already imported support to ImportFullyQualifiedNamesRector
This commit is contained in:
Tomáš Votruba 2019-05-19 12:25:03 +02:00 committed by GitHub
commit 00465053a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 3 deletions

View File

@ -233,6 +233,7 @@ CODE_SAMPLE
}
$name = $node->getAttribute('originalName');
if ($name instanceof Name) {
// already short
if (! Strings::contains($name->toString(), '\\')) {
@ -244,9 +245,7 @@ CODE_SAMPLE
// the short name is already used, skip it
$shortName = $this->classNaming->getShortName($name->toString());
// is already used
if (isset($this->alreadyUsedShortNames[$shortName]) && $this->alreadyUsedShortNames[$shortName] !== $name->toString()) {
if ($this->isShortNameAlreadyUsedForDifferentFqn($node, $shortName)) {
return null;
}
@ -360,4 +359,14 @@ CODE_SAMPLE
$this->importsInClassCollection->reset();
$this->docBlockManipulator->resetImportedNames();
}
// is already used
private function isShortNameAlreadyUsedForDifferentFqn(Name $name, string $shortName): bool
{
if (! isset($this->alreadyUsedShortNames[$shortName])) {
return false;
}
return $this->alreadyUsedShortNames[$shortName] !== $this->getName($name);
}
}

View File

@ -0,0 +1,51 @@
<?php declare(strict_types=1);
namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
final class ImportPartialInstanceOf
{
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
public function isIteratorToArrayFuncCall(Expr $expr): bool
{
return $expr instanceof Node\Expr\FuncCall;
}
}
?>
-----
<?php declare(strict_types=1);
namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
final class ImportPartialInstanceOf
{
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
public function isIteratorToArrayFuncCall(Expr $expr): bool
{
return $expr instanceof FuncCall;
}
}
?>

View File

@ -32,6 +32,7 @@ final class ImportFullyQualifiedNamesRectorTest extends AbstractRectorTestCase
__DIR__ . '/Fixture/many_imports.php.inc',
__DIR__ . '/Fixture/keep_static_method.php.inc',
__DIR__ . '/Fixture/keep_various_request.php.inc',
__DIR__ . '/Fixture/instance_of.php.inc',
// function
__DIR__ . '/Fixture/import_function.php.inc',