Skip to content

Commit

Permalink
Add ClassNameFromObjectTypeResolver to cover TypeWithClassName instan…
Browse files Browse the repository at this point in the history
…ce checks (#6420)
  • Loading branch information
samsonasik authored Nov 12, 2024
1 parent 06adce3 commit bcfb598
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/StaticTypeMapper/Resolver/ClassNameFromObjectTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Rector\StaticTypeMapper\Resolver;

use PHPStan\Type\Type;

final class ClassNameFromObjectTypeResolver
{
public static function resolve(Type $type): ?string
{
$objectClassNames = $type->getObjectClassNames();

if (count($objectClassNames) !== 1) {
return null;
}

return $objectClassNames[0];
}
}
8 changes: 5 additions & 3 deletions src/StaticTypeMapper/ValueObject/Type/AliasedObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpParser\Node\Stmt\UseUse;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;

/**
* @api
Expand Down Expand Up @@ -55,13 +55,15 @@ public function areShortNamesEqual(self | FullyQualifiedObjectType $comparedObje

public function equals(Type $type): bool
{
$className = ClassNameFromObjectTypeResolver::resolve($type);

// compare with FQN classes
if ($type instanceof TypeWithClassName) {
if ($className !== null) {
if ($type instanceof self && $this->fullyQualifiedClass === $type->getFullyQualifiedName()) {
return true;
}

if ($this->fullyQualifiedClass === $type->getClassName()) {
if ($this->fullyQualifiedClass === $className) {
return true;
}
}
Expand Down

0 comments on commit bcfb598

Please sign in to comment.