Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeDeclaration] Handle union A extends B on ReturnUnionTypeRector #5149

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions packages/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeCommonTypeNarrower;
use Rector\PHPStanStaticTypeMapper\ValueObject\UnionTypeAnalysis;
use Webmozart\Assert\Assert;
use Webmozart\Assert\InvalidArgumentException;
Expand All @@ -51,7 +50,6 @@ public function __construct(
private readonly DoctrineTypeAnalyzer $doctrineTypeAnalyzer,
private readonly PhpVersionProvider $phpVersionProvider,
private readonly UnionTypeAnalyzer $unionTypeAnalyzer,
private readonly UnionTypeCommonTypeNarrower $unionTypeCommonTypeNarrower,
private readonly NodeNameResolver $nodeNameResolver,
private readonly TypeFactory $typeFactory
) {
Expand Down Expand Up @@ -282,8 +280,13 @@ private function hasObjectAndStaticType(PhpParserUnionType $phpParserUnionType):
*/
private function matchTypeForUnionedTypes(UnionType $unionType, string $typeKind): ?Node
{
$phpParserUnionType = $this->matchPhpParserUnionType($unionType, $typeKind);
// use first unioned type in case of unioned object types
$compatibleObjectTypeNode = $this->processResolveCompatibleObjectCandidates($unionType);
if ($compatibleObjectTypeNode instanceof NullableType || $compatibleObjectTypeNode instanceof FullyQualified) {
return $compatibleObjectTypeNode;
}
samsonasik marked this conversation as resolved.
Show resolved Hide resolved

$phpParserUnionType = $this->matchPhpParserUnionType($unionType, $typeKind);
if ($phpParserUnionType instanceof NullableType) {
return $phpParserUnionType;
}
Expand All @@ -292,12 +295,6 @@ private function matchTypeForUnionedTypes(UnionType $unionType, string $typeKind
return $this->resolveUnionTypeNode($unionType, $phpParserUnionType, $typeKind);
}

// use first unioned type in case of unioned object types
$compatibleObjectTypeNode = $this->processResolveCompatibleObjectCandidates($unionType);
if ($compatibleObjectTypeNode instanceof NullableType || $compatibleObjectTypeNode instanceof FullyQualified) {
return $compatibleObjectTypeNode;
}

$type = $this->typeFactory->createMixedPassedOrUnionType($unionType->getTypes());
if (! $type instanceof UnionType) {
return $this->phpStanStaticTypeMapper->mapToPhpParserNode($type, $typeKind);
Expand Down Expand Up @@ -403,8 +400,7 @@ private function resolveCompatibleObjectCandidate(UnionType $unionType): UnionTy
return $this->correctObjectType($sharedTypeWithClassName);
}

// find least common denominator
return $this->unionTypeCommonTypeNarrower->narrowToSharedObjectType($unionType);
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Source\SomeParentClass;

final class ExtendedClass extends SomeParentClass
{
public function run()
{
if (rand(0, 1)) {
return new ExtendedClass();
}

return new SomeParentClass();
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Source\SomeParentClass;

final class ExtendedClass extends SomeParentClass
{
public function run(): \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Source\SomeParentClass
{
if (rand(0, 1)) {
return new ExtendedClass();
}

return new SomeParentClass();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Source;

class SomeParentClass
{
}