mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-13 22:52:23 +00:00
[Php80] Handle union collection of FullyQualifieds on ClassPropertyAssignToConstructorPromotionRector (#722)
Co-authored-by: Jáchym Toušek <enumag@gmail.com>
This commit is contained in:
parent
c043528bfc
commit
998029d4fc
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user