[Php80] Do not remove array<mixed> inside Union type on UnionTypesRector (#2358)

* [Php80] Do not remove array<mixed> inside Union type on UnionTypesRector

* Fixed 🎉

* also allow mixed[]

* clean up

* clean up
This commit is contained in:
Abdul Malik Ikhsan 2022-05-25 13:52:50 +07:00 committed by GitHub
parent e1ecbe90fc
commit 4c53b206a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 8 deletions

View File

@ -2,7 +2,7 @@
namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;
final class RemoveDocArrayTyped
final class DoNotRemoveDocArrayTypedMixed
{
/**
* @return bool|float|int|string|array<mixed>
@ -19,8 +19,11 @@ final class RemoveDocArrayTyped
namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;
final class RemoveDocArrayTyped
final class DoNotRemoveDocArrayTypedMixed
{
/**
* @return bool|float|int|string|array<mixed>
*/
public function normalizeNodeValue($value): bool|float|int|string|array
{
return $value;

View File

@ -2,7 +2,7 @@
namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;
final class RemoveDocArrayTyped2
final class DoNotRemoveDocArrayTypedMixed2
{
/**
* @param bool|float|int|string|array<mixed> $value
@ -19,8 +19,11 @@ final class RemoveDocArrayTyped2
namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;
final class RemoveDocArrayTyped2
final class DoNotRemoveDocArrayTypedMixed2
{
/**
* @param bool|float|int|string|array<mixed> $value
*/
public function normalizeNodeValue(bool|float|int|string|array $value)
{
return $value;

View File

@ -0,0 +1,33 @@
<?php
namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;
final class DoNotRemoveDocArrayTypedMixed3
{
/**
* @param bool|float|int|string|mixed[] $value
*/
public function normalizeNodeValue($value)
{
return $value;
}
}
?>
-----
<?php
namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;
final class DoNotRemoveDocArrayTypedMixed3
{
/**
* @param bool|float|int|string|mixed[] $value
*/
public function normalizeNodeValue(bool|float|int|string|array $value)
{
return $value;
}
}
?>

View File

@ -0,0 +1,15 @@
<?php
namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;
final class SkipWithArrayMixed
{
/**
* @param object|array<mixed> $foo
* @param array<mixed> $bar
*/
public function run(object|array $foo, array $bar): int
{
return 5;
}
}

View File

@ -20,6 +20,7 @@ use Rector\BetterPhpDocParser\ValueObject\PhpDoc\VariadicAwareParamTagValueNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\DeadCode\TypeNodeAnalyzer\GenericTypeNodeAnalyzer;
use Rector\DeadCode\TypeNodeAnalyzer\MixedArrayTypeNodeAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
@ -29,6 +30,7 @@ final class DeadParamTagValueNodeAnalyzer
private readonly NodeNameResolver $nodeNameResolver,
private readonly TypeComparator $typeComparator,
private readonly GenericTypeNodeAnalyzer $genericTypeNodeAnalyzer,
private readonly MixedArrayTypeNodeAnalyzer $mixedArrayTypeNodeAnalyzer
) {
}
@ -63,6 +65,10 @@ final class DeadParamTagValueNodeAnalyzer
return $this->isEmptyDescription($paramTagValueNode, $param->type);
}
if ($this->mixedArrayTypeNodeAnalyzer->hasMixedArrayType($paramTagValueNode->type)) {
return false;
}
if (! $this->genericTypeNodeAnalyzer->hasGenericType($paramTagValueNode->type)) {
return $this->isEmptyDescription($paramTagValueNode, $param->type);
}

View File

@ -15,10 +15,6 @@ final class GenericTypeNodeAnalyzer
foreach ($types as $type) {
if ($type instanceof GenericTypeNode) {
if ($type->type->name === 'array') {
continue;
}
return true;
}
}