File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture
rules/TypeDeclarationDocblocks/Rector/ClassMethod Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \TypeDeclarationDocblocks \Rector \ClassMethod \DocblockReturnArrayFromDirectArrayInstanceRector \Fixture ;
4+
5+ final class SkipEmptyArrayMixed
6+ {
7+ /**
8+ * @return mixed[]
9+ */
10+ private static function toArray (): array
11+ {
12+ return [];
13+ }
14+ }
Original file line number Diff line number Diff line change 99use PhpParser \Node \Stmt \ClassMethod ;
1010use PhpParser \Node \Stmt \Function_ ;
1111use PhpParser \Node \Stmt \Return_ ;
12+ use PHPStan \PhpDocParser \Ast \PhpDoc \ReturnTagValueNode ;
13+ use PHPStan \PhpDocParser \Ast \Type \ArrayTypeNode ;
1214use PHPStan \Type \Constant \ConstantArrayType ;
15+ use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfo ;
1316use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
1417use Rector \BetterPhpDocParser \PhpDocManipulator \PhpDocTypeChanger ;
1518use Rector \Rector \AbstractRector ;
@@ -100,6 +103,10 @@ public function refactor(Node $node): ?Node
100103 return null ;
101104 }
102105
106+ if ($ this ->shouldSkipReturnMixedAndEmptyArray ($ phpDocInfo , $ soleReturn ->expr )) {
107+ return null ;
108+ }
109+
103110 // resolve simple type
104111 $ returnedType = $ this ->getType ($ soleReturn ->expr );
105112 if (! $ returnedType instanceof ConstantArrayType) {
@@ -116,4 +123,19 @@ public function refactor(Node $node): ?Node
116123
117124 return $ node ;
118125 }
126+
127+ private function shouldSkipReturnMixedAndEmptyArray (PhpDocInfo $ phpDocInfo , Array_ $ array ): bool
128+ {
129+ if ($ array ->items !== []) {
130+ return false ;
131+ }
132+
133+ $ returnTagValueNode = $ phpDocInfo ->getReturnTagValue ();
134+ if (! $ returnTagValueNode instanceof ReturnTagValueNode) {
135+ return false ;
136+ }
137+
138+ // better than array{}
139+ return $ returnTagValueNode ->type instanceof ArrayTypeNode;
140+ }
119141}
You can’t perform that action at this time.
0 commit comments