Skip to content

Commit

Permalink
[NodeAnalyzer] Remove ClassAnalyzer::isAnonymousClassName()
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Oct 4, 2024
1 parent 447afc1 commit 1d7d915
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
33 changes: 4 additions & 29 deletions src/NodeAnalyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,17 @@
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Util\StringUtils;

final class ClassAnalyzer
{
/**
* @var string
* @see https://regex101.com/r/FQH6RT/2
*/
private const ANONYMOUS_CLASS_REGEX = '#^AnonymousClass\w+$#';

public function isAnonymousClassName(string $className): bool
{
return StringUtils::isMatch($className, self::ANONYMOUS_CLASS_REGEX);
}

public function isAnonymousClass(Node $node): bool
{
if (! $node instanceof Class_) {
return $node instanceof New_ && $this->isAnonymousClass($node->class);
}

if ($node->isAnonymous()) {
return true;
}

$scope = $node->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
if ($node instanceof New_) {
return $this->isAnonymousClass($node->class);
}

$classReflection = $scope->getClassReflection();
if ($classReflection instanceof ClassReflection) {
return $classReflection->isAnonymous();
if ($node instanceof Class_) {
return $node->isAnonymous();
}

return false;
Expand Down
11 changes: 10 additions & 1 deletion src/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,16 @@ private function isMatchObjectWithoutClassType(

private function isAnonymousObjectType(Type $type): bool
{
return $type instanceof ObjectType && $this->classAnalyzer->isAnonymousClassName($type->getClassName());
if (! $type instanceof ObjectType) {
return false;
}

$classReflection = $type->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

return $classReflection->isAnonymous();
}

private function isUnionTypeable(Type $first, Type $second): bool
Expand Down

0 comments on commit 1d7d915

Please sign in to comment.