Skip to content

Commit

Permalink
[CodingStyle] Remove parent lookup on ShortNameResolver (#4260)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jun 17, 2023
1 parent 96dc306 commit ad65e04
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(

public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $node): bool
{
$classLikeNames = $this->shortNameResolver->resolveShortClassLikeNamesForNode($node);
$classLikeNames = $this->shortNameResolver->resolveShortClassLikeNames($file);
if ($classLikeNames === []) {
return false;
}
Expand Down
13 changes: 9 additions & 4 deletions rules/CodingStyle/ClassNameImport/ShortNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,19 @@ public function resolveFromFile(File $file): array
* Collects all "class <SomeClass>", "trait <SomeTrait>" and "interface <SomeInterface>"
* @return string[]
*/
public function resolveShortClassLikeNamesForNode(Node $node): array
public function resolveShortClassLikeNames(File $file): array
{
$namespace = $this->betterNodeFinder->findParentType($node, Namespace_::class);
if (! $namespace instanceof Namespace_) {
// only handle namespace nodes
$newStmts = $file->getNewStmts();

/** @var Namespace_[] $namespaces */
$namespaces = array_filter($newStmts, static fn (Stmt $stmt): bool => $stmt instanceof Namespace_);
if (count($namespaces) !== 1) {
// only handle single namespace nodes
return [];
}

$namespace = current($namespaces);

/** @var ClassLike[] $classLikes */
$classLikes = $this->betterNodeFinder->findInstanceOf($namespace, ClassLike::class);

Expand Down

0 comments on commit ad65e04

Please sign in to comment.