Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/CodingStyle/src/Imports/ShortNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class ShortNameResolver
*/
private $callableNodeTraverser;

/**
* @var string[][]
*/
private $shortNamesByNamespaceObjectHash = [];

public function __construct(CallableNodeTraverser $callableNodeTraverser)
{
$this->callableNodeTraverser = $callableNodeTraverser;
Expand All @@ -26,10 +31,22 @@ public function __construct(CallableNodeTraverser $callableNodeTraverser)
*/
public function resolveForNode(Node $node): array
{
/** @var Namespace_|null $namespace */
$namespace = $node->getAttribute(AttributeKey::NAMESPACE_NODE);

if ($namespace === null) {
return [];
}

$namespaceHash = spl_object_hash($namespace);
if (isset($this->shortNamesByNamespaceObjectHash[$namespaceHash])) {
return $this->shortNamesByNamespaceObjectHash[$namespaceHash];
}

if ($namespace instanceof Namespace_) {
return $this->resolveForNamespace($namespace);
$shortNames = $this->resolveForNamespace($namespace);
$this->shortNamesByNamespaceObjectHash[$namespaceHash] = $shortNames;
return $shortNames;
}

return [];
Expand Down
38 changes: 33 additions & 5 deletions packages/CodingStyle/src/Rector/Use_/RemoveUnusedAliasRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use PhpParser\NodeVisitor\NameResolver;
use Rector\CodingStyle\Imports\ShortNameResolver;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
Expand All @@ -40,9 +42,24 @@ final class RemoveUnusedAliasRector extends AbstractRector
*/
private $callableNodeTraverser;

public function __construct(CallableNodeTraverser $callableNodeTraverser)
{
/**
* @var ShortNameResolver
*/
private $shortNameResolver;

/**
* @var ClassNaming
*/
private $classNaming;

public function __construct(
CallableNodeTraverser $callableNodeTraverser,
ShortNameResolver $shortNameResolver,
ClassNaming $classNaming
) {
$this->callableNodeTraverser = $callableNodeTraverser;
$this->shortNameResolver = $shortNameResolver;
$this->classNaming = $classNaming;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -83,8 +100,14 @@ public function refactor(Node $node): ?Node
{
$this->resolvedNodeNames = [];
$this->resolveUsedNameNodes($node);
if ($this->resolvedNodeNames === []) {
return null;

// collect differentiated aliases
$useNamesAliasToName = [];

$shortNames = $this->shortNameResolver->resolveForNode($node);
foreach ($shortNames as $alias => $useImport) {
$shortName = $this->classNaming->getShortName($useImport);
$useNamesAliasToName[$shortName][] = $alias;
}

foreach ($node->uses as $use) {
Expand All @@ -96,7 +119,7 @@ public function refactor(Node $node): ?Node
$aliasName = $this->getName($use->alias);

// both are used → nothing to remove
if (isset($this->resolvedNodeNames[$lastName]) && isset($this->resolvedNodeNames[$aliasName])) {
if (isset($this->resolvedNodeNames[$lastName], $this->resolvedNodeNames[$aliasName])) {
continue;
}

Expand All @@ -113,6 +136,11 @@ public function refactor(Node $node): ?Node

// only alias name is used → use last name directly
if (isset($this->resolvedNodeNames[$aliasName])) {
// keep to differentiate 2 alaises classes
if (isset($useNamesAliasToName[$lastName]) && count($useNamesAliasToName[$lastName]) > 1) {
continue;
}

$this->renameNameNode($this->resolvedNodeNames[$aliasName], $lastName);
$use->alias = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@ class DifferentNamespacesSameClass
$secondStandalone = new SecondStandalone;
}
}

?>

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public function test(): void
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/used.php.inc',
__DIR__ . '/Fixture/class_name.php.inc',
__DIR__ . '/Fixture/different_namespaces_same_name.php.inc',
# no namespace
__DIR__ . '/Fixture/no_namespace.php.inc',
__DIR__ . '/Fixture/no_namespace_class_name.php.inc',
Expand All @@ -23,6 +22,8 @@ public function test(): void
# interfaces
__DIR__ . '/Fixture/interace_extending.php.inc',
__DIR__ . '/Fixture/doc_block.php.inc',
# skip
__DIR__ . '/Fixture/skip_different_namespaces_same_name.php.inc',
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function refactor(Node $node): ?Node
$this->undefinedVariables[] = $variableName;
});
} catch (BreakScopeException $breakScopeException) {
// nothing
// @ignoreException
}

if ($this->undefinedVariables === []) {
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,6 @@ parameters:
- '#Cannot cast array<string\>\|bool\|string\|null to string#'
- '#Method Rector\\ContributorTools\\Configuration\\ConfigurationFactory\:\:resolveCategoryFromFqnNodeTypes\(\) should return string but returns string\|null#'
- '#Method Rector\\Legacy\\Rector\\ClassMethod\\ChangeSingletonToServiceRector\:\:matchStaticPropertyFetchAndGetSingletonMethodName\(\) should return array<string\>\|null but returns array<int, string\|null\>#'

# future compat
- '#Call to function method_exists\(\) with (.*?) will always evaluate to false#'
2 changes: 1 addition & 1 deletion src/EventDispatcher/AutowiredEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(array $eventSubscribers)
$this->addSubscriber($eventSubscriber);
}

if (method_exists(get_parent_class($this), '__construct')) {
if (method_exists(parent::class, '__construct')) {
parent::__construct();
}
}
Expand Down