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
25 changes: 0 additions & 25 deletions rules/CodingStyle/ClassNameImport/AliasUsesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

namespace Rector\CodingStyle\ClassNameImport;

use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\UseItem;
use Rector\PhpParser\Node\FileNode;

final readonly class AliasUsesResolver
{
Expand All @@ -19,28 +16,6 @@ public function __construct(
) {
}

/**
* @param Stmt[] $stmts
* @return string[]
*/
public function resolveFromNode(Node $node, array $stmts): array
{
if (! $node instanceof Namespace_ && ! $node instanceof FileNode) {
/** @var Namespace_[]|FileNode[] $namespaces */
$namespaces = array_filter(
$stmts,
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileNode
);
if (count($namespaces) !== 1) {
return [];
}

$node = current($namespaces);
}

return $this->resolveFromStmts($node->stmts);
}

/**
* @param Stmt[] $stmts
* @return string[]
Expand Down
104 changes: 104 additions & 0 deletions rules/CodingStyle/ClassNameImport/ClassNameImportContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Rector\CodingStyle\ClassNameImport;

use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Namespace_;
use Rector\PhpParser\Node\FileNode;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\ValueObject\Application\File;

/**
* Per-file import facts shared across skip voters. Aliased uses and used import types are
* file-global, so they are resolved once per file stmts version and reused for every imported name.
*/
final class ClassNameImportContext
{
private ?File $cachedFile = null;

private int $cachedVersion = -1;

/**
* @var string[]
*/
private array $aliasedUses = [];

/**
* @var array<AliasedObjectType|FullyQualifiedObjectType>
*/
private array $usedImportTypes = [];

public function __construct(
private readonly AliasUsesResolver $aliasUsesResolver
) {
}

/**
* @return string[]
*/
public function resolveAliasedUses(File $file): array
{
$this->ensureResolved($file);

return $this->aliasedUses;
}

/**
* @return array<AliasedObjectType|FullyQualifiedObjectType>
*/
public function resolveUsedImportTypes(File $file): array
{
$this->ensureResolved($file);

return $this->usedImportTypes;
}

private function ensureResolved(File $file): void
{
$newStmtsVersion = $file->getNewStmtsVersion();
if ($file === $this->cachedFile && $newStmtsVersion === $this->cachedVersion) {
return;
}

$this->cachedFile = $file;
$this->cachedVersion = $newStmtsVersion;
$this->aliasedUses = $this->resolveAliasedUsesFromFile($file);
$this->usedImportTypes = $this->resolveUsedImportTypesFromFile($file);
}

/**
* @return string[]
*/
private function resolveAliasedUsesFromFile(File $file): array
{
/** @var Namespace_[]|FileNode[] $namespaces */
$namespaces = array_filter(
$file->getNewStmts(),
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileNode
);

if (count($namespaces) !== 1) {
return [];
}

$namespace = current($namespaces);

return $this->aliasUsesResolver->resolveFromStmts($namespace->stmts);
}

/**
* @return array<AliasedObjectType|FullyQualifiedObjectType>
*/
private function resolveUsedImportTypesFromFile(File $file): array
{
$fileNode = $file->getFileNode();
if (! $fileNode instanceof FileNode) {
return [];
}

return $fileNode->resolveUsedImportTypes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\CodingStyle\ClassNameImport\ClassNameImportSkipVoter;

use PhpParser\Node;
use Rector\CodingStyle\ClassNameImport\AliasUsesResolver;
use Rector\CodingStyle\ClassNameImport\ClassNameImportContext;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\ValueObject\Application\File;
Expand All @@ -22,13 +22,13 @@
final readonly class AliasClassNameImportSkipVoter implements ClassNameImportSkipVoterInterface
{
public function __construct(
private AliasUsesResolver $aliasUsesResolver
private ClassNameImportContext $classNameImportContext
) {
}

public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $node): bool
{
$aliasedUses = $this->aliasUsesResolver->resolveFromNode($node, $file->getNewStmts());
$aliasedUses = $this->classNameImportContext->resolveAliasedUses($file);
$longNameLowered = strtolower($fullyQualifiedObjectType->getClassName());
$shortNameLowered = $fullyQualifiedObjectType->getShortNameLowered();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\CodingStyle\ClassNameImport\ClassNameImportSkipVoter;

use PhpParser\Node;
use Rector\CodingStyle\ClassNameImport\ClassNameImportContext;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\PhpParser\Node\FileNode;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\ValueObject\Application\File;

Expand All @@ -19,14 +19,14 @@
*/
final readonly class UsesClassNameImportSkipVoter implements ClassNameImportSkipVoterInterface
{
public function __construct(
private ClassNameImportContext $classNameImportContext
) {
}

public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $node): bool
{
$fileNode = $file->getFileNode();
if (! $fileNode instanceof FileNode) {
return false;
}

$useImportTypes = $fileNode->resolveUsedImportTypes();
$useImportTypes = $this->classNameImportContext->resolveUsedImportTypes($file);

foreach ($useImportTypes as $useImportType) {
if (! $useImportType->equals($fullyQualifiedObjectType) && $useImportType->areShortNamesEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ public function configure(array $configuration): void

private function hasMatchingMethodName(ClassMethod $classMethod): bool
{
foreach ($this->methodVisibilities as $methodVisibility) {
if ($this->isName($classMethod, $methodVisibility->getMethod())) {
return true;
}
}

return false;
return array_any($this->methodVisibilities, fn(ChangeMethodVisibility $methodVisibility): bool => $this->isName($classMethod, $methodVisibility->getMethod()));
}
}
Loading