Skip to content

Commit a888358

Browse files
committed
[DeadCode] Keep @return union docblock when native type involves array (preserves iterable value type)
1 parent af4c1cf commit a888358

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessUnionReturnDocblockRector\Fixture;
4+
5+
final class SkipNullableArrayNative
6+
{
7+
/**
8+
* @return mixed[]|null
9+
*/
10+
public function run(): ?array
11+
{
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessUnionReturnDocblockRector\Fixture;
4+
5+
final class SkipUnionWithArrayNative
6+
{
7+
/**
8+
* @return string|mixed[]|int
9+
*/
10+
public function run(): string|array|int
11+
{
12+
}
13+
}

rules/DeadCode/Rector/ClassMethod/RemoveUselessUnionReturnDocblockRector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public function refactor(Node $node): ?Node
106106

107107
$nativeReturnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node->returnType);
108108

109-
// keep array native type, the docblock may carry element types
110-
if ($nativeReturnType->isArray()->yes()) {
109+
// keep array-involving native type (array, ?array, union with array), the docblock may carry element types
110+
if (! $nativeReturnType->isArray()->no()) {
111111
return null;
112112
}
113113

0 commit comments

Comments
 (0)