mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-22 18:54:39 +01:00
[CodingStyle] add partial support already imported support to ImportFullyQualifiedNamesRector (#1447)
[CodingStyle] add partial support already imported support to ImportFullyQualifiedNamesRector
This commit is contained in:
commit
00465053a1
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -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',
|
||||
|
Loading…
x
Reference in New Issue
Block a user