Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

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

use DateTime;
use stdClass;

final class UnionArray
{
public function run($a, $b)
{
if ($a) {
return null;
}

if ($b) {
return new DateTime('now');
}

return [];
}
}

?>
-----
<?php

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

use DateTime;
use stdClass;

final class UnionArray
{
public function run($a, $b): \DateTime|array|null
{
if ($a) {
return null;
}

if ($b) {
return new DateTime('now');
}

return [];
}
}

?>
43 changes: 0 additions & 43 deletions rules/TypeDeclaration/TypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -26,7 +25,6 @@ final class TypeNormalizer
private array $collectedNestedArrayTypes = [];

public function __construct(
private readonly TypeFactory $typeFactory,
private readonly PrivatesAccessor $privatesAccessor
) {
}
Expand Down Expand Up @@ -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);
}
}
Comment on lines -90 to -101
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was used by ReturnTypeDeclarationRector which read docblock 20153a9

which now we use native type for return type inferer so it should safe to remove


if ($traversedType instanceof NeverType) {
return new MixedType();
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
}