From 2427c5a094a39018e0c558482faf218d577e3382 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 14 Oct 2022 16:43:45 +0700 Subject: [PATCH] [Php80] Handle crash after transform with trait-string on UnionTypesRector (#2985) Co-authored-by: GitHub Action --- .../TypeMapper/IntersectionTypeMapper.php | 3 +-- .../TypeMapper/ObjectTypeMapper.php | 4 ++++ ...do_not_remove_generic_with_intersection.php.inc | 10 +++++----- .../skip_object_type_not_exists.php.inc | 11 +++++++++++ .../Fixture/skip_union_trait_string.php.inc | 14 ++++++++++++++ 5 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 rules-tests/Php74/Rector/Property/TypedPropertyRector/FixtureUnionIntersectionTypes/skip_object_type_not_exists.php.inc create mode 100644 rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_union_trait_string.php.inc diff --git a/packages/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php b/packages/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php index e696aa06172..9d95b15a79b 100644 --- a/packages/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php +++ b/packages/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php @@ -13,7 +13,6 @@ use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode; -use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\Php\PhpVersionProvider; use Rector\Core\ValueObject\PhpVersionFeature; use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface; @@ -90,7 +89,7 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node $resolvedTypeName = self::STRING; $resolvedType = new Name(self::STRING); } elseif (! $resolvedType instanceof Name) { - throw new ShouldNotHappenException(); + return null; } else { $resolvedTypeName = (string) $resolvedType; } diff --git a/packages/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php b/packages/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php index c47fad52e37..84906111be9 100644 --- a/packages/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php +++ b/packages/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php @@ -98,6 +98,10 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node return new FullyQualified($className); } + if ($type instanceof NonExistingObjectType) { + return null; + } + if (! $type instanceof GenericObjectType) { // fallback return new FullyQualified($type->getClassName()); diff --git a/rules-tests/Php74/Rector/Property/TypedPropertyRector/FixtureUnionIntersectionTypes/do_not_remove_generic_with_intersection.php.inc b/rules-tests/Php74/Rector/Property/TypedPropertyRector/FixtureUnionIntersectionTypes/do_not_remove_generic_with_intersection.php.inc index 0894ed2930c..93468d9daaf 100644 --- a/rules-tests/Php74/Rector/Property/TypedPropertyRector/FixtureUnionIntersectionTypes/do_not_remove_generic_with_intersection.php.inc +++ b/rules-tests/Php74/Rector/Property/TypedPropertyRector/FixtureUnionIntersectionTypes/do_not_remove_generic_with_intersection.php.inc @@ -2,8 +2,8 @@ namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureUnionIntersectionTypes; -use MockObject; -use ObjectRepository; +use PHPUnit\Framework\MockObject\MockObject; +use Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\Doctrine\ObjectRepository; final class DemoFile { @@ -19,15 +19,15 @@ final class DemoFile namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureUnionIntersectionTypes; -use MockObject; -use ObjectRepository; +use PHPUnit\Framework\MockObject\MockObject; +use Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\Doctrine\ObjectRepository; final class DemoFile { /** * @var MockObject&ObjectRepository */ - private \MockObject&\ObjectRepository $itemCountryRepo; + private \PHPUnit\Framework\MockObject\MockObject&\Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\Doctrine\ObjectRepository $itemCountryRepo; } ?> diff --git a/rules-tests/Php74/Rector/Property/TypedPropertyRector/FixtureUnionIntersectionTypes/skip_object_type_not_exists.php.inc b/rules-tests/Php74/Rector/Property/TypedPropertyRector/FixtureUnionIntersectionTypes/skip_object_type_not_exists.php.inc new file mode 100644 index 00000000000..72fea88da15 --- /dev/null +++ b/rules-tests/Php74/Rector/Property/TypedPropertyRector/FixtureUnionIntersectionTypes/skip_object_type_not_exists.php.inc @@ -0,0 +1,11 @@ + + */ + private $itemCountryRepo; +} diff --git a/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_union_trait_string.php.inc b/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_union_trait_string.php.inc new file mode 100644 index 00000000000..2b85a46f444 --- /dev/null +++ b/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_union_trait_string.php.inc @@ -0,0 +1,14 @@ +