mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-20 23:41:57 +02:00
[CodingStyle] Prevent adding non-namespaced imports to non-namespaced class
This commit is contained in:
parent
4107774ffc
commit
23c0fb1d2a
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Application;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
@ -155,7 +156,9 @@ final class UseAddingCommander implements CommanderInterface
|
||||
// B. no namespace? add in the top
|
||||
// first clean
|
||||
$nodes = $this->useImportsRemover->removeImportsFromStmts($nodes, $removedShortUses);
|
||||
$useImportTypes = $this->filterOutNonNamespacedNames($useImportTypes);
|
||||
// then add, to prevent adding + removing false positive of same short use
|
||||
|
||||
return $this->useImportsAdder->addImportsToStmts($nodes, $useImportTypes, $functionUseImportTypes);
|
||||
}
|
||||
|
||||
@ -287,4 +290,24 @@ final class UseAddingCommander implements CommanderInterface
|
||||
|
||||
return $this->useImportTypesInFilePath[$filePath] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevents
|
||||
* @param FullyQualifiedObjectType[] $useImportTypes
|
||||
* @return FullyQualifiedObjectType[]
|
||||
*/
|
||||
private function filterOutNonNamespacedNames(array $useImportTypes): array
|
||||
{
|
||||
$namespacedUseImportTypes = [];
|
||||
|
||||
foreach ($useImportTypes as $useImportType) {
|
||||
if (! Strings::contains($useImportType->getClassName(), '\\')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$namespacedUseImportTypes[] = $useImportType;
|
||||
}
|
||||
|
||||
return $namespacedUseImportTypes;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class SearchagentController extends ApplicationController
|
||||
{
|
||||
}
|
||||
|
||||
class ApplicationController
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user