Skip to content

Commit 42f8514

Browse files
authored
[TypeDeclaration] Skip noisy array-shape union return docblock in ReturnTypeFromStrictNewArrayRector (#8422)
1 parent 054379c commit 42f8514

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;
4+
5+
final class UnionArrayShapesNoDoc
6+
{
7+
public function getRowFormatBookEntry($abe, $onlyCashRegister = false)
8+
{
9+
$data = [];
10+
foreach ($abe as $x) {
11+
if ($onlyCashRegister) {
12+
$data[] = ['amount' => 1, 'cash_register_id' => 2, 'text' => 't'];
13+
} else {
14+
$data[] = ['amount' => 1, 'date' => 'd', 'text' => 't'];
15+
}
16+
}
17+
18+
return $data;
19+
}
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;
27+
28+
final class UnionArrayShapesNoDoc
29+
{
30+
public function getRowFormatBookEntry($abe, $onlyCashRegister = false): array
31+
{
32+
$data = [];
33+
foreach ($abe as $x) {
34+
if ($onlyCashRegister) {
35+
$data[] = ['amount' => 1, 'cash_register_id' => 2, 'text' => 't'];
36+
} else {
37+
$data[] = ['amount' => 1, 'date' => 'd', 'text' => 't'];
38+
}
39+
}
40+
41+
return $data;
42+
}
43+
}
44+
45+
?>

‎rules/TypeDeclaration/NodeAnalyzer/StrictReturnNewArrayResolver.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use PHPStan\Type\NeverType;
2121
use PHPStan\Type\Type;
2222
use PHPStan\Type\TypeCombinator;
23+
use PHPStan\Type\TypeTraverser;
24+
use PHPStan\Type\UnionType;
2325
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
2426
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
2527
use Rector\NodeNameResolver\NodeNameResolver;
@@ -202,6 +204,11 @@ private function matchArrayAssignedVariable(array $stmts): array
202204

203205
private function shouldAddReturnArrayDocType(Type $arrayType): bool
204206
{
207+
// a union of multiple distinct array shapes produces a noisy doc type, skip it
208+
if ($this->hasNoisyArrayShapeUnion($arrayType)) {
209+
return false;
210+
}
211+
205212
if ($arrayType instanceof ConstantArrayType) {
206213
if ($arrayType->getIterableValueType() instanceof NeverType) {
207214
return false;
@@ -215,4 +222,37 @@ private function shouldAddReturnArrayDocType(Type $arrayType): bool
215222

216223
return true;
217224
}
225+
226+
private function hasNoisyArrayShapeUnion(Type $type): bool
227+
{
228+
$isNoisy = false;
229+
TypeTraverser::map($type, function (Type $currentType, callable $traverse) use (&$isNoisy): Type {
230+
if ($currentType instanceof UnionType && $this->hasMultipleArrayVariants($currentType)) {
231+
$isNoisy = true;
232+
}
233+
234+
return $traverse($currentType);
235+
});
236+
237+
return $isNoisy;
238+
}
239+
240+
private function hasMultipleArrayVariants(UnionType $unionType): bool
241+
{
242+
$arrayVariantCount = 0;
243+
foreach ($unionType->getTypes() as $type) {
244+
if (! $type->isArray()->yes()) {
245+
continue;
246+
}
247+
248+
// an empty array [] collapses into the sibling variant, ignore it
249+
if ($type->getIterableValueType() instanceof NeverType) {
250+
continue;
251+
}
252+
253+
++$arrayVariantCount;
254+
}
255+
256+
return $arrayVariantCount >= 2;
257+
}
218258
}

0 commit comments

Comments
 (0)