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
18 changes: 13 additions & 5 deletions rules/CodingStyle/ClassNameImport/ShortNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Use_;
use PHPStan\PhpDocParser\Ast\NodeTraverser;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
Expand Down Expand Up @@ -120,16 +121,23 @@ private function resolveForStmts(array $stmts): array
{
$shortNamesToFullyQualifiedNames = [];
$docBlockShortNames = [];
$uses = [];

// single traversal collects both PHP names and docblock short names, so the file stmts are walked once
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use (
&$shortNamesToFullyQualifiedNames,
&$docBlockShortNames
&$docBlockShortNames,
&$uses
): null {
foreach ($this->resolveDocBlockShortNames($node) as $docBlockShortName) {
$docBlockShortNames[] = $docBlockShortName;
}

if ($node instanceof Use_) {
$uses[] = $node;
return null;
}

// class name is used!
if ($node instanceof ClassLike && $node->name instanceof Identifier) {
$fullyQualifiedName = $this->nodeNameResolver->getName($node);
Expand Down Expand Up @@ -160,7 +168,7 @@ private function resolveForStmts(array $stmts): array
return null;
});

$docBlockShortNamesToFullyQualifiedNames = $this->fqnizeShortNames($docBlockShortNames, $stmts);
$docBlockShortNamesToFullyQualifiedNames = $this->fqnizeShortNames($docBlockShortNames, $uses);
/** @var array<string, string> $result */
$result = [...$shortNamesToFullyQualifiedNames, ...$docBlockShortNamesToFullyQualifiedNames];
return $result;
Expand Down Expand Up @@ -202,15 +210,15 @@ static function ($node) use (&$shortNames): null {

/**
* @param string[] $shortNames
* @param Stmt[] $stmts
* @param Use_[] $uses
* @return array<string, string>
*/
private function fqnizeShortNames(array $shortNames, array $stmts): array
private function fqnizeShortNames(array $shortNames, array $uses): array
{
$shortNamesToFullyQualifiedNames = [];

foreach ($shortNames as $shortName) {
$stmtsMatchedName = $this->useImportNameMatcher->matchNameWithStmts($shortName, $stmts);
$stmtsMatchedName = $this->useImportNameMatcher->matchNameWithUses($shortName, $uses);

if ($stmtsMatchedName == null) {
continue;
Expand Down
13 changes: 0 additions & 13 deletions rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

use Nette\Utils\Strings;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\UseItem;
use Rector\Exception\ShouldNotHappenException;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Util\StringUtils;

final readonly class UseImportNameMatcher
Expand All @@ -25,21 +23,10 @@
private const string SHORT_NAME_REGEX = '#^%s(\\\\[\w]+)?$#i';

public function __construct(
private BetterNodeFinder $betterNodeFinder,
private UseImportsResolver $useImportsResolver
) {
}

/**
* @param Stmt[] $stmts
*/
public function matchNameWithStmts(string $tag, array $stmts): ?string
{
/** @var Use_[] $uses */
$uses = $this->betterNodeFinder->findInstanceOf($stmts, Use_::class);
return $this->matchNameWithUses($tag, $uses);
}

/**
* @param array<Use_|GroupUse> $uses
* @return non-empty-string|null
Expand Down
Loading