From 9429364da2910da193ba2ba4b0791ccb04a53939 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 8 Oct 2023 17:16:14 +0700 Subject: [PATCH 1/2] [TypeDeclaration] Handle Union with array type on ReturnUnionTypeRector --- .../Fixture/union_array.php.inc | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_array.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_array.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_array.php.inc new file mode 100644 index 00000000000..7f97220cb06 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_array.php.inc @@ -0,0 +1,49 @@ + +----- + From 07f3a903f26030f2122d4b6e6f53d247c0000c07 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 8 Oct 2023 17:28:46 +0700 Subject: [PATCH 2/2] Fixed :tada: --- .../Fixture/union_array.php.inc | 2 +- rules/TypeDeclaration/TypeNormalizer.php | 43 ------------------- 2 files changed, 1 insertion(+), 44 deletions(-) diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_array.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_array.php.inc index 7f97220cb06..f361f22433f 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_array.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/union_array.php.inc @@ -32,7 +32,7 @@ use stdClass; final class UnionArray { - public function run($a, $b): null|\DateTime|array + public function run($a, $b): \DateTime|array|null { if ($a) { return null; diff --git a/rules/TypeDeclaration/TypeNormalizer.php b/rules/TypeDeclaration/TypeNormalizer.php index ef0c509587e..fea1a2b6e63 100644 --- a/rules/TypeDeclaration/TypeNormalizer.php +++ b/rules/TypeDeclaration/TypeNormalizer.php @@ -12,7 +12,6 @@ use PHPStan\Type\TypeTraverser; use PHPStan\Type\UnionType; use Rector\Core\Util\Reflection\PrivatesAccessor; -use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\TypeDeclaration\ValueObject\NestedArrayType; /** @@ -26,7 +25,6 @@ final class TypeNormalizer private array $collectedNestedArrayTypes = []; public function __construct( - private readonly TypeFactory $typeFactory, private readonly PrivatesAccessor $privatesAccessor ) { } @@ -87,19 +85,6 @@ public function normalizeArrayTypeAndArrayNever(Type $type): Type return $traversedType; } - if ($traversedType instanceof UnionType) { - $traversedTypeTypes = $traversedType->getTypes(); - $countTraversedTypes = count($traversedTypeTypes); - - $collectedTypes = $this->getCollectedTypes($traversedTypeTypes); - $countCollectedTypes = count($collectedTypes); - - // re-create new union types - if ($countTraversedTypes !== $countCollectedTypes && $countTraversedTypes > 2) { - return $this->typeFactory->createMixedPassedOrUnionType($collectedTypes); - } - } - if ($traversedType instanceof NeverType) { return new MixedType(); } @@ -113,25 +98,6 @@ private function isConstantArrayNever(Type $type): bool return $type instanceof ConstantArrayType && $type->getKeyType() instanceof NeverType && $type->getItemType() instanceof NeverType; } - /** - * @param Type[] $traversedTypeTypes - * @return Type[] - */ - private function getCollectedTypes(array $traversedTypeTypes): array - { - $collectedTypes = []; - foreach ($traversedTypeTypes as $traversedTypeType) { - // basically an empty array - not useful at all - if ($this->isArrayNeverType($traversedTypeType)) { - continue; - } - - $collectedTypes[] = $traversedTypeType; - } - - return $collectedTypes; - } - private function collectNestedArrayTypeFromUnionType(UnionType $unionType, int $arrayNesting): void { foreach ($unionType->getTypes() as $unionedType) { @@ -166,13 +132,4 @@ private function createUnionedTypesFromArrayTypes(array $collectedNestedArrayTyp return $unionedTypes[0]; } - - private function isArrayNeverType(Type $type): bool - { - if (! $type instanceof ArrayType) { - return false; - } - - return $type->getKeyType() instanceof NeverType && $type->getItemType() instanceof NeverType; - } }