Skip to content

Commit bfa99fe

Browse files
committed
Prefer exact type over mixed[] in case of empty array in ClassMethodArrayDocblockParamFromLocalCallsRector
1 parent 6d40f8a commit bfa99fe

File tree

6 files changed

+110
-6
lines changed

6 files changed

+110
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject;
6+
7+
final class ObjectsOverruleEmptyArray
8+
{
9+
public function go(array $emptyArray)
10+
{
11+
$this->run($this->getItemsWithArray());
12+
$this->run($this->getItems());
13+
14+
$this->run($emptyArray);
15+
}
16+
17+
public function run(array $result)
18+
{
19+
}
20+
21+
/**
22+
* @return SomeReturnedObject[]|array
23+
*/
24+
private function getItems()
25+
{
26+
return [];
27+
}
28+
29+
/**
30+
* @return SomeReturnedObject[]
31+
*/
32+
private function getItemsWithArray()
33+
{
34+
return [];
35+
}
36+
}
37+
38+
?>
39+
-----
40+
<?php
41+
42+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;
43+
44+
use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject;
45+
46+
final class ObjectsOverruleEmptyArray
47+
{
48+
public function go(array $emptyArray)
49+
{
50+
$this->run($this->getItemsWithArray());
51+
$this->run($this->getItems());
52+
53+
$this->run($emptyArray);
54+
}
55+
56+
/**
57+
* @param \Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject[] $result
58+
*/
59+
public function run(array $result)
60+
{
61+
}
62+
63+
/**
64+
* @return SomeReturnedObject[]|array
65+
*/
66+
private function getItems()
67+
{
68+
return [];
69+
}
70+
71+
/**
72+
* @return SomeReturnedObject[]
73+
*/
74+
private function getItemsWithArray()
75+
{
76+
return [];
77+
}
78+
}
79+
80+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source;
4+
5+
final class SomeReturnedObject
6+
{
7+
}

rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ private function correctSelfType(Type $argValueType): Type
116116
private function unionToSingleType(array $staticTypesByArgumentPosition): array
117117
{
118118
$staticTypeByArgumentPosition = [];
119+
119120
foreach ($staticTypesByArgumentPosition as $position => $staticTypes) {
120121
$unionedType = $this->typeFactory->createMixedPassedOrUnionType($staticTypes);
121122

src/NodeTypeResolver/NodeTypeCorrector/AccessoryNonEmptyArrayTypeCorrector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public function correct(Type $mainType): Type
3030

3131
if ($type instanceof ArrayType
3232
&& $type->getIterableValueType() instanceof IntersectionType
33-
&& $type->getIterableValueType()->isString()->yes()) {
33+
&& $type->getIterableValueType()
34+
->isString()
35+
->yes()) {
3436
return new ArrayType(new MixedType(), new StringType());
3537
}
3638
}

src/NodeTypeResolver/PHPStan/Type/TypeFactory.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public function createMixedPassedOrUnionType(array $types, bool $keepConstantTyp
4747
$types = $this->unwrapUnionedTypes($types);
4848
$types = $this->uniquateTypes($types, $keepConstantTypes);
4949

50-
return $this->createUnionOrSingleType($types);
50+
// remove dummy array mixed[], as no value
51+
$typesWithoutMixed = array_filter($types, fn (Type $type): bool => ! $this->isArrayMixedMixedType($type));
52+
53+
return $this->createUnionOrSingleType($typesWithoutMixed);
5154
}
5255

5356
/**
@@ -220,4 +223,17 @@ private function unwrapConstantArrayTypes(ConstantArrayType $constantArrayType):
220223

221224
return $unwrappedTypes;
222225
}
226+
227+
private function isArrayMixedMixedType(Type $type): bool
228+
{
229+
if (! $type instanceof ArrayType) {
230+
return false;
231+
}
232+
233+
if (! $type->getItemType() instanceof MixedType) {
234+
return false;
235+
}
236+
237+
return $type->getKeyType() instanceof MixedType;
238+
}
223239
}

src/PostRector/Collector/UseNodesToAddCollector.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ public function getUseImportTypesByNode(File $file): array
8383
return $objectTypes;
8484
}
8585

86-
public function hasImport(
87-
File $file,
88-
FullyQualifiedObjectType $fullyQualifiedObjectType
89-
): bool {
86+
public function hasImport(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType): bool
87+
{
9088
$useImports = $this->getUseImportTypesByNode($file);
9189

9290
foreach ($useImports as $useImport) {

0 commit comments

Comments
 (0)