Skip to content

Commit ce46023

Browse files
authored
[TypeDeclarationDocblocks] Handle with duplicated nested type[] array docblock on DocblockReturnArrayFromDirectArrayInstanceRector (#7353)
* [TypeDeclarationDocblocks] Handle with duplicated nested type[] array docblock on DocblockReturnArrayFromDirectArrayInstanceRector * note to debug * Fix * reference
1 parent a340f8c commit ce46023

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
4+
5+
/**
6+
* @see https://github.com/codeigniter4/CodeIgniter4/blob/89fee62b666c07550197203a94fb7dd94303d7ca/tests/system/CodeIgniterTest.php#L893
7+
*/
8+
final class WithDuplicatedNestedArray
9+
{
10+
public static function run(): iterable
11+
{
12+
$testingUrls = [
13+
'test',
14+
'test?important_parameter=1',
15+
];
16+
17+
return [
18+
'$cacheQueryString=false' => [false, 1, $testingUrls],
19+
'$cacheQueryString=true' => [true, 5, $testingUrls],
20+
'$cacheQueryString=array' => [['important_parameter'], 3, $testingUrls],
21+
];
22+
}
23+
}
24+
25+
?>
26+
-----
27+
<?php
28+
29+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
30+
31+
/**
32+
* @see https://github.com/codeigniter4/CodeIgniter4/blob/89fee62b666c07550197203a94fb7dd94303d7ca/tests/system/CodeIgniterTest.php#L893
33+
*/
34+
final class WithDuplicatedNestedArray
35+
{
36+
/**
37+
* @return array<string, string[][]|bool[]|int[]>
38+
*/
39+
public static function run(): iterable
40+
{
41+
$testingUrls = [
42+
'test',
43+
'test?important_parameter=1',
44+
];
45+
46+
return [
47+
'$cacheQueryString=false' => [false, 1, $testingUrls],
48+
'$cacheQueryString=true' => [true, 5, $testingUrls],
49+
'$cacheQueryString=array' => [['important_parameter'], 3, $testingUrls],
50+
];
51+
}
52+
}
53+
54+
?>

src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPStan\Type\Type;
2020
use PHPStan\Type\UnionType;
2121
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
22+
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode;
2223
use Rector\NodeAnalyzer\PropertyAnalyzer;
2324
use Rector\Php\PhpVersionProvider;
2425
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
@@ -57,12 +58,32 @@ public function getNodeClass(): string
5758
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
5859
{
5960
$unionTypesNodes = [];
61+
$existingTypes = [];
62+
6063
foreach ($type->getTypes() as $unionedType) {
6164
if ($unionedType instanceof ArrayType && $unionedType->getItemType() instanceof NeverType) {
6265
$unionedType = new ArrayType($unionedType->getKeyType(), new MixedType());
6366
}
6467

65-
$unionTypesNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType);
68+
$unionedType = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType);
69+
70+
if ($unionedType instanceof SpacingAwareArrayTypeNode && $unionedType->type instanceof BracketsAwareUnionTypeNode) {
71+
foreach ($unionedType->type->types as $key => $innerTypeNode) {
72+
$printedInnerType = (string) $innerTypeNode;
73+
if (in_array($printedInnerType, $existingTypes, true)) {
74+
unset($unionedType->type->types[$key]);
75+
continue;
76+
}
77+
78+
$existingTypes[] = $printedInnerType;
79+
}
80+
81+
if ($unionedType->type->types === []) {
82+
continue;
83+
}
84+
}
85+
86+
$unionTypesNodes[] = $unionedType;
6687
}
6788

6889
return new BracketsAwareUnionTypeNode($unionTypesNodes);

0 commit comments

Comments
 (0)