Skip to content

Commit

Permalink
[Php80] Do not remove array<mixed> inside Union type on UnionTypesRec…
Browse files Browse the repository at this point in the history
…tor (#2358)

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

* Fixed 🎉

* also allow mixed[]

* clean up

* clean up
  • Loading branch information
samsonasik committed May 25, 2022
1 parent e1ecbe9 commit 4c53b20
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

?>
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 6 additions & 0 deletions rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;

Expand All @@ -29,6 +30,7 @@ public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly TypeComparator $typeComparator,
private readonly GenericTypeNodeAnalyzer $genericTypeNodeAnalyzer,
private readonly MixedArrayTypeNodeAnalyzer $mixedArrayTypeNodeAnalyzer
) {
}

Expand Down Expand Up @@ -63,6 +65,10 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
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);
}
Expand Down
4 changes: 0 additions & 4 deletions rules/DeadCode/TypeNodeAnalyzer/GenericTypeNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public function hasGenericType(UnionTypeNode $unionTypeNode): bool

foreach ($types as $type) {
if ($type instanceof GenericTypeNode) {
if ($type->type->name === 'array') {
continue;
}

return true;
}
}
Expand Down

0 comments on commit 4c53b20

Please sign in to comment.