[Php80] Handle union collection of FullyQualifieds on ClassPropertyAssignToConstructorPromotionRector (#722)

Co-authored-by: Jáchym Toušek <enumag@gmail.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-08-20 17:27:01 +07:00 committed by GitHub
parent c043528bfc
commit 998029d4fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 1 deletions

View File

@ -0,0 +1,32 @@
<?php
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
class MultiUnrelatedFullyQualified
{
public \DateTime $x;
public \stdClass $y;
public function __construct(
\DateTime $x,
\stdClass $y
) {
$this->x = $x;
$this->y = $y;
}
}
?>
-----
<?php
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
class MultiUnrelatedFullyQualified
{
public function __construct(public \DateTime $x, public \stdClass $y)
{
}
}
?>

View File

@ -0,0 +1,51 @@
<?php
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
interface X {}
class Y implements X {}
class Z {}
final class UnionFullyQualified
{
public Y $y;
public Z $z;
public function __construct(Y $y, Z $z)
{
$this->y = $y;
$this->z = $z;
}
public function getX(): X
{
return $this->y;
}
}
?>
-----
<?php
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
interface X {}
class Y implements X {}
class Z {}
final class UnionFullyQualified
{
public function __construct(public Y $y, public Z $z)
{
}
public function getX(): X
{
return $this->y;
}
}
?>

View File

@ -13,6 +13,7 @@ use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\ValueObject\MethodName;
@ -21,6 +22,7 @@ use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\Php80\ValueObject\PropertyPromotionCandidate;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
final class PromotedPropertyCandidateResolver
@ -182,8 +184,18 @@ final class PromotedPropertyCandidateResolver
);
}
$isAllFullyQualifiedObjectType = true;
if ($propertyType instanceof UnionType) {
foreach ($propertyType->getTypes() as $type) {
if (! $type instanceof FullyQualifiedObjectType) {
$isAllFullyQualifiedObjectType = false;
break;
}
}
}
// different types, not a good to fit
return ! $this->typeComparator->areTypesEqual($propertyType, $matchedParamType);
return ! $isAllFullyQualifiedObjectType && ! $this->typeComparator->areTypesEqual($propertyType, $matchedParamType);
}
/**