44
55use PhpParser \Node ;
66use PHPStan \Analyser \Scope ;
7+ use PHPStan \Node \Expr \TypeExpr ;
78use PHPStan \Reflection \Php \PhpMethodFromParserNodeReflection ;
89use PHPStan \Rules \Rule ;
9- use PHPStan \ShouldNotHappenException ;
10+ use PHPStan \Type \ Constant \ ConstantArrayType ;
1011use PHPUnit \Framework \TestCase ;
1112use function array_slice ;
1213use function count ;
@@ -40,22 +41,28 @@ public function getNodeType(): string
4041 public function processNode (Node $ node , Scope $ scope ): array
4142 {
4243 if ($ node instanceof Node \Stmt \Return_ || $ node instanceof Node \Expr \YieldFrom) {
43- if (!$ node ->expr instanceof Node \Expr \Array_) {
44+ $ exprType = $ scope ->getType ($ node ->expr );
45+ if (!$ exprType ->isConstantArray ()->yes ()) {
4446 return [];
4547 }
4648
47- $ arrayExprs = [];
48- foreach ($ node ->expr ->items as $ item ) {
49- if (!$ item ->value instanceof Node \Expr \Array_) {
50- return [];
49+ $ constArrays = $ exprType ->getConstantArrays ();
50+ $ constantArrays = [];
51+ foreach ($ constArrays as $ constArray ) {
52+ foreach ($ constArray ->getValueTypes () as $ valueType ) {
53+ if (!$ valueType ->isConstantArray ()->yes ()) {
54+ return [];
55+ }
56+ $ constantArrays [] = $ valueType ;
5157 }
52- $ arrayExprs [] = $ item ->value ;
5358 }
5459 } elseif ($ node instanceof Node \Expr \Yield_) {
55- if (!$ node ->value instanceof Node \Expr \Array_) {
60+ $ exprType = $ scope ->getType ($ node ->value );
61+ if (!$ exprType ->isConstantArray ()->yes ()) {
5662 return [];
5763 }
58- $ arrayExprs = [$ node ->value ];
64+
65+ $ constantArrays = $ exprType ->getConstantArrays ();
5966 } else {
6067 return [];
6168 }
@@ -105,8 +112,8 @@ public function processNode(Node $node, Scope $scope): array
105112 }
106113
107114 foreach ($ testsWithProvider as $ testMethod ) {
108- foreach ($ arrayExprs as $ arrayExpr ) {
109- $ args = $ this ->arrayItemsToArgs ($ arrayExpr );
115+ foreach ($ constantArrays as $ constantArray ) {
116+ $ args = $ this ->arrayItemsToArgs ($ constantArray );
110117 if ($ args === null ) {
111118 continue ;
112119 }
@@ -120,7 +127,7 @@ public function processNode(Node $node, Scope $scope): array
120127 $ var ,
121128 $ testMethod ->getName (),
122129 $ args ,
123- ['startLine ' => $ arrayExpr ->getStartLine ()],
130+ ['startLine ' => $ node ->getStartLine ()],
124131 ));
125132 }
126133 }
@@ -131,15 +138,13 @@ public function processNode(Node $node, Scope $scope): array
131138 /**
132139 * @return array<Node\Arg>
133140 */
134- private function arrayItemsToArgs (Node \ Expr \ Array_ $ array ): ?array
141+ private function arrayItemsToArgs (ConstantArrayType $ array ): ?array
135142 {
136143 $ args = [];
137144
138- foreach ($ array ->items as $ item ) {
145+ foreach ($ array ->getValueTypes () as $ valueType ) {
139146 // XXX named args
140- $ value = $ item ->value ;
141-
142- $ arg = new Node \Arg ($ value );
147+ $ arg = new Node \Arg (new TypeExpr ($ valueType ));
143148 $ args [] = $ arg ;
144149 }
145150
0 commit comments