From 7cdd03ed7733e2d8d12d68e5750a89b7f0dda829 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 14 Dec 2021 15:22:38 +0100 Subject: [PATCH] [PHP 8.1] Fix IntersectionTypesRector for non-object types (#1495) --- .../Fixture/skip_scalar_types.php.inc | 13 ++++++++++++ .../Fixture/some_class.php.inc | 16 +++++++++----- .../Source/FirstObjectToIntersect.php | 10 +++++++++ .../Source/SecondObjectToIntersect.php | 10 +++++++++ .../FunctionLike/IntersectionTypesRector.php | 21 +++++++++++++++++++ 5 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 rules-tests/Php81/Rector/FunctionLike/IntersectionTypesRector/Fixture/skip_scalar_types.php.inc create mode 100644 rules-tests/Php81/Rector/FunctionLike/IntersectionTypesRector/Source/FirstObjectToIntersect.php create mode 100644 rules-tests/Php81/Rector/FunctionLike/IntersectionTypesRector/Source/SecondObjectToIntersect.php diff --git a/rules-tests/Php81/Rector/FunctionLike/IntersectionTypesRector/Fixture/skip_scalar_types.php.inc b/rules-tests/Php81/Rector/FunctionLike/IntersectionTypesRector/Fixture/skip_scalar_types.php.inc new file mode 100644 index 00000000000..0f82e7f6f15 --- /dev/null +++ b/rules-tests/Php81/Rector/FunctionLike/IntersectionTypesRector/Fixture/skip_scalar_types.php.inc @@ -0,0 +1,13 @@ +isIntersectionableType($paramType)) { + continue; + } + $phpParserIntersectionType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode( $paramType, TypeKind::PARAM() @@ -123,4 +128,20 @@ private function refactorParamTypes( $this->hasChanged = true; } } + + /** + * Only class-type are supported https://wiki.php.net/rfc/pure-intersection-types#supported_types + */ + private function isIntersectionableType(IntersectionType $intersectionType): bool + { + foreach ($intersectionType->getTypes() as $intersectionedType) { + if ($intersectionedType instanceof TypeWithClassName) { + continue; + } + + return false; + } + + return true; + } }