Skip to content

Commit

Permalink
[CodeQuality] Fix aliased object type on FlipTypeControlToUseExclusiv…
Browse files Browse the repository at this point in the history
…eTypeRector (#6497)

* [CodeQuality] Fix aliased object type on FlipTypeControlToUseExclusiveTypeRector

* [CodeQuality] Fix aliased object type on FlipTypeControlToUseExclusiveTypeRector

* fix in ReflectionResolver
  • Loading branch information
samsonasik authored Nov 24, 2024
1 parent 23f8e4a commit 3c42539
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector\Fixture;

use PhpParser\Node\Scalar\Int_ as SomeInt;

class NullableTypeAliased
{
public function run()
{
$intNode = $this->getIntNode();
if ($intNode === null) {
return;
}
}

private function getIntNode(): ?SomeInt
{
if (rand(0, 1)) {
return new SomeInt(1);
}

return null;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector\Fixture;

use PhpParser\Node\Scalar\Int_ as SomeInt;

class NullableTypeAliased
{
public function run()
{
$intNode = $this->getIntNode();
if (!$intNode instanceof \PhpParser\Node\Scalar\Int_) {
return;
}
}

private function getIntNode(): ?SomeInt
{
if (rand(0, 1)) {
return new SomeInt(1);
}

return null;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
use Rector\TypeDeclaration\TypeAnalyzer\NullableTypeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -86,7 +87,9 @@ private function processConvertToExclusiveType(
Expr $expr,
Identical|NotIdentical $binaryOp
): BooleanNot|Instanceof_ {
$fullyQualifiedType = $objectType instanceof ShortenedObjectType ? $objectType->getFullyQualifiedName() : $objectType->getClassName();
$fullyQualifiedType = $objectType instanceof ShortenedObjectType || $objectType instanceof AliasedObjectType
? $objectType->getFullyQualifiedName()
: $objectType->getClassName();

$instanceof = new Instanceof_($expr, new FullyQualified($fullyQualifiedType));
if ($binaryOp instanceof NotIdentical) {
Expand Down
3 changes: 2 additions & 1 deletion src/Reflection/ReflectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
use Rector\ValueObject\MethodName;

Expand Down Expand Up @@ -142,7 +143,7 @@ public function resolveMethodReflectionFromStaticCall(StaticCall $staticCall): ?
{
$objectType = $this->nodeTypeResolver->getType($staticCall->class);

if ($objectType instanceof ShortenedObjectType) {
if ($objectType instanceof ShortenedObjectType || $objectType instanceof AliasedObjectType) {
/** @var array<class-string> $classNames */
$classNames = [$objectType->getFullyQualifiedName()];
} else {
Expand Down

0 comments on commit 3c42539

Please sign in to comment.