Skip to content

Commit 5a54f35

Browse files
authored
skip trimming of union object types in ClassMethodArrayDocblockParamFromLocalCallsRector (#7355)
1 parent 780ca32 commit 5a54f35

File tree

4 files changed

+106
-6
lines changed

4 files changed

+106
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\AnotherReturnedObject;
6+
use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject;
7+
8+
final class UnionObjects
9+
{
10+
public function go()
11+
{
12+
$this->run($this->getUnion());
13+
}
14+
15+
private function run(array $items)
16+
{
17+
}
18+
19+
/**
20+
* @return SomeReturnedObject[]|AnotherReturnedObject[]
21+
*/
22+
private function getUnion(): array
23+
{
24+
return [];
25+
}
26+
}
27+
28+
?>
29+
-----
30+
<?php
31+
32+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;
33+
34+
use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\AnotherReturnedObject;
35+
use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject;
36+
37+
final class UnionObjects
38+
{
39+
public function go()
40+
{
41+
$this->run($this->getUnion());
42+
}
43+
44+
/**
45+
* @param \Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\AnotherReturnedObject[]|\Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject[] $items
46+
*/
47+
private function run(array $items)
48+
{
49+
}
50+
51+
/**
52+
* @return SomeReturnedObject[]|AnotherReturnedObject[]
53+
*/
54+
private function getUnion(): array
55+
{
56+
return [];
57+
}
58+
}
59+
60+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source;
4+
5+
final class AnotherReturnedObject
6+
{
7+
8+
}

rules/Privatization/TypeManipulator/TypeNormalizer.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,18 @@ public function generalizeConstantTypes(Type $type): Type
101101

102102
if (count($uniqueGeneralizedUnionTypes) > 1) {
103103
$generalizedUnionType = new UnionType($uniqueGeneralizedUnionTypes);
104+
104105
// avoid too huge print in docblock
105106
$unionedDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
106107
$generalizedUnionType
107108
);
108109

109110
// too long
110-
if (strlen((string) $unionedDocType) > self::MAX_PRINTED_UNION_DOC_LENGHT) {
111+
if (strlen(
112+
(string) $unionedDocType
113+
) > self::MAX_PRINTED_UNION_DOC_LENGHT && $this->avoidPrintedDocblockTrimming(
114+
$generalizedUnionType
115+
) === false) {
111116
$alwaysKnownArrayType = $this->narrowToAlwaysKnownArrayType($generalizedUnionType);
112117
if ($alwaysKnownArrayType instanceof ArrayType) {
113118
return $alwaysKnownArrayType;
@@ -164,4 +169,20 @@ private function narrowToAlwaysKnownArrayType(UnionType $unionType): ?ArrayType
164169
);
165170
return new ArrayType($arrayUniqueKeyType, new MixedType());
166171
}
172+
173+
/**
174+
* Is object only? avoid trimming, as auto import handles it better
175+
*/
176+
private function avoidPrintedDocblockTrimming(UnionType $unionType): bool
177+
{
178+
if ($unionType->getConstantScalarTypes() !== []) {
179+
return false;
180+
}
181+
182+
if ($unionType->getConstantArrays() !== []) {
183+
return false;
184+
}
185+
186+
return $unionType->getObjectClassNames() !== [];
187+
}
167188
}

rules/TypeDeclarationDocblocks/Rector/Class_/ClassMethodArrayDocblockParamFromLocalCallsRector.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace Rector\TypeDeclarationDocblocks\Rector\Class_;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Param;
89
use PhpParser\Node\Stmt\Class_;
910
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
11+
use PHPStan\Type\ArrayType;
1012
use PHPStan\Type\MixedType;
1113
use PHPStan\Type\Type;
1214
use PHPStan\Type\TypeCombinator;
@@ -93,11 +95,7 @@ public function refactor(Node $node): ?Node
9395
$classMethodParameterTypes = $this->callTypesResolver->resolveTypesFromCalls($methodCalls);
9496

9597
foreach ($classMethod->getParams() as $parameterPosition => $param) {
96-
if ($param->type === null) {
97-
continue;
98-
}
99-
100-
if (! $this->isName($param->type, 'array')) {
98+
if (! $this->hasParamArrayType($param)) {
10199
continue;
102100
}
103101

@@ -118,6 +116,10 @@ public function refactor(Node $node): ?Node
118116
continue;
119117
}
120118

119+
if ($resolvedParameterType instanceof ArrayType && $resolvedParameterType->getItemType() instanceof MixedType && $resolvedParameterType->getKeyType() instanceof MixedType) {
120+
continue;
121+
}
122+
121123
// in case of array type declaration, null cannot be passed or is already casted
122124
$resolvedParameterType = TypeCombinator::removeNull($resolvedParameterType);
123125

@@ -140,4 +142,13 @@ public function refactor(Node $node): ?Node
140142

141143
return $node;
142144
}
145+
146+
private function hasParamArrayType(Param $param): bool
147+
{
148+
if (! $param->type instanceof Node) {
149+
return false;
150+
}
151+
152+
return $this->isName($param->type, 'array');
153+
}
143154
}

0 commit comments

Comments
 (0)