Skip to content

Commit

Permalink
Fix union type check of object (#4893)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 1, 2023
1 parent c5897e5 commit 65fd201
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,14 @@ private function isUnionTypeable(Type $first, Type $second): bool
private function isMatchingUnionType(Type $resolvedType, ObjectType $requiredObjectType): bool
{
$type = TypeCombinator::removeNull($resolvedType);

// for falsy nullables
$type = TypeCombinator::remove($type, new ConstantBooleanType(false));

if ($type->isSuperTypeOf($requiredObjectType)->yes()) {
return true;
}

if (! $type instanceof ObjectType) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;

final class RenameReflectionMethod
{
public function run(\ReflectionMethod|\ReflectionFunction $reflectionMethod)
{
return $reflectionMethod->getTentativeReturnType();
}
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;

final class RenameReflectionMethod
{
public function run(\ReflectionMethod|\ReflectionFunction $reflectionMethod)
{
return $reflectionMethod->getReturnType();
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
new MethodCallRename(Foo::class, 'old', 'new'),
new MethodCallRename(NewInterface::class, 'some_old', 'some_new'),
new MethodCallRename(DifferentInterface::class, 'renameMe', 'toNewVersion'),

// reflection method name
new MethodCallRename(ReflectionMethod::class, 'getTentativeReturnType', 'getReturnType'),

// with array key
new MethodCallRenameWithArrayKey('Nette\Utils\Html', 'addToArray', 'addToHtmlArray', 'hey'),
]);
Expand Down

0 comments on commit 65fd201

Please sign in to comment.