File tree Expand file tree Collapse file tree 4 files changed +102
-7
lines changed
rules-tests/TypeDeclarationDocblocks/Rector/Class_/ClassMethodArrayDocblockParamFromLocalCallsRector
Privatization/TypeManipulator
TypeDeclarationDocblocks/Rector/Class_ Expand file tree Collapse file tree 4 files changed +102
-7
lines changed Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \TypeDeclarationDocblocks \Rector \Class_ \ClassMethodArrayDocblockParamFromLocalCallsRector \Source ;
4+
5+ final class AnotherReturnedObject
6+ {
7+
8+ }
Original file line number Diff line number Diff line change @@ -101,13 +101,14 @@ 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 ((string ) $ unionedDocType ) > self ::MAX_PRINTED_UNION_DOC_LENGHT && $ this -> avoidPrintedDocblockTrimming ( $ generalizedUnionType ) === false ) {
111112 $ alwaysKnownArrayType = $ this ->narrowToAlwaysKnownArrayType ($ generalizedUnionType );
112113 if ($ alwaysKnownArrayType instanceof ArrayType) {
113114 return $ alwaysKnownArrayType ;
@@ -164,4 +165,20 @@ private function narrowToAlwaysKnownArrayType(UnionType $unionType): ?ArrayType
164165 );
165166 return new ArrayType ($ arrayUniqueKeyType , new MixedType ());
166167 }
168+
169+ /**
170+ * Is object only? avoid trimming, as auto import handles it better
171+ */
172+ private function avoidPrintedDocblockTrimming (UnionType $ unionType ): bool
173+ {
174+ if ($ unionType ->getConstantScalarTypes () !== []) {
175+ return false ;
176+ }
177+
178+ if ($ unionType ->getConstantArrays () !== []) {
179+ return false ;
180+ }
181+
182+ return $ unionType ->getReferencedClasses () !== [];
183+ }
167184}
Original file line number Diff line number Diff line change 77use PhpParser \Node ;
88use PhpParser \Node \Stmt \Class_ ;
99use PHPStan \PhpDocParser \Ast \PhpDoc \ParamTagValueNode ;
10+ use PHPStan \Type \ArrayType ;
1011use PHPStan \Type \MixedType ;
1112use PHPStan \Type \Type ;
1213use PHPStan \Type \TypeCombinator ;
@@ -93,11 +94,7 @@ public function refactor(Node $node): ?Node
9394 $ classMethodParameterTypes = $ this ->callTypesResolver ->resolveTypesFromCalls ($ methodCalls );
9495
9596 foreach ($ classMethod ->getParams () as $ parameterPosition => $ param ) {
96- if ($ param ->type === null ) {
97- continue ;
98- }
99-
100- if (! $ this ->isName ($ param ->type , 'array ' )) {
97+ if (! $ this ->hasParamArrayType ($ param )) {
10198 continue ;
10299 }
103100
@@ -114,7 +111,7 @@ public function refactor(Node $node): ?Node
114111 continue ;
115112 }
116113
117- if ($ resolvedParameterType instanceof MixedType) {
114+ if ($ resolvedParameterType instanceof MixedType || ( $ resolvedParameterType instanceof ArrayType && $ resolvedParameterType -> getItemType () instanceof MixedType && $ resolvedParameterType -> getKeyType () instanceof MixedType) ) {
118115 continue ;
119116 }
120117
@@ -140,4 +137,17 @@ public function refactor(Node $node): ?Node
140137
141138 return $ node ;
142139 }
140+
141+ private function hasParamArrayType (mixed $ param ): bool
142+ {
143+ if ($ param ->type === null ) {
144+ return false ;
145+ }
146+
147+ if (!$ this ->isName ($ param ->type , 'array ' )) {
148+ return false ;
149+ }
150+
151+ return true ;
152+ }
143153}
You can’t perform that action at this time.
0 commit comments