Skip to content

Commit

Permalink
[Php80] Handle union collection of FullyQualifieds on ClassPropertyAs…
Browse files Browse the repository at this point in the history
…signToConstructorPromotionRector (#722)

Co-authored-by: Jáchym Toušek <enumag@gmail.com>
  • Loading branch information
samsonasik and enumag committed Aug 20, 2021
1 parent c043528 commit 998029d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -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;
}
}
?>
14 changes: 13 additions & 1 deletion rules/Php80/NodeAnalyzer/PromotedPropertyCandidateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand All @@ -21,6 +22,7 @@
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
Expand Down Expand Up @@ -182,8 +184,18 @@ private function hasConflictingParamType(Param $param, Type $propertyType): bool
);
}

$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);
}

/**
Expand Down

0 comments on commit 998029d

Please sign in to comment.