@@ -1168,13 +1168,20 @@ private function processStmtNode(
1168
1168
}
1169
1169
} elseif ($ stmt instanceof Static_) {
1170
1170
$ hasYield = false ;
1171
- $ comment = CommentHelper::getDocComment ($ stmt );
1171
+
1172
+ $ vars = [];
1172
1173
foreach ($ stmt ->vars as $ var ) {
1173
1174
$ scope = $ this ->processStmtNode ($ var , $ scope , $ nodeCallback )->getScope ();
1174
- if ($ comment === null || !is_string ($ var ->var ->name )) {
1175
+ if (!is_string ($ var ->var ->name )) {
1175
1176
continue ;
1176
1177
}
1177
- $ scope = $ this ->processVarAnnotation ($ scope , $ var ->var ->name , $ comment , false );
1178
+
1179
+ $ vars [] = $ var ->var ->name ;
1180
+ }
1181
+
1182
+ $ comment = CommentHelper::getDocComment ($ stmt );
1183
+ if ($ comment !== null ) {
1184
+ $ scope = $ this ->processVarAnnotation ($ scope , $ vars , $ comment );
1178
1185
}
1179
1186
} elseif ($ stmt instanceof StaticVar) {
1180
1187
$ hasYield = false ;
@@ -1523,7 +1530,7 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
1523
1530
if ($ expr ->var instanceof Variable && is_string ($ expr ->var ->name )) {
1524
1531
$ comment = CommentHelper::getDocComment ($ expr );
1525
1532
if ($ comment !== null ) {
1526
- $ scope = $ this ->processVarAnnotation ($ scope , $ expr ->var ->name , $ comment, false , $ varChangedScope );
1533
+ $ scope = $ this ->processVarAnnotation ($ scope , [ $ expr ->var ->name ] , $ comment , $ varChangedScope );
1527
1534
}
1528
1535
}
1529
1536
@@ -1552,6 +1559,7 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
1552
1559
$ scope = $ this ->lookForArrayDestructuringArray ($ scope , $ expr ->var , $ scope ->getType ($ expr ->expr ));
1553
1560
$ comment = CommentHelper::getDocComment ($ expr );
1554
1561
if ($ comment !== null ) {
1562
+ $ vars = [];
1555
1563
foreach ($ expr ->var ->items as $ arrayItem ) {
1556
1564
if ($ arrayItem === null ) {
1557
1565
continue ;
@@ -1560,15 +1568,17 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
1560
1568
continue ;
1561
1569
}
1562
1570
1571
+ $ vars [] = $ arrayItem ->value ->name ;
1572
+ }
1573
+
1574
+ if (count ($ vars ) > 0 ) {
1563
1575
$ varChangedScope = false ;
1564
- $ scope = $ this ->processVarAnnotation ($ scope , $ arrayItem ->value ->name , $ comment , true , $ varChangedScope );
1565
- if ($ varChangedScope ) {
1566
- continue ;
1576
+ $ scope = $ this ->processVarAnnotation ($ scope , $ vars , $ comment , $ varChangedScope );
1577
+ if (!$ varChangedScope ) {
1578
+ $ scope = $ this ->processStmtVarAnnotation ($ scope , new Node \Stmt \Expression ($ expr , [
1579
+ 'comments ' => $ expr ->getAttribute ('comments ' ),
1580
+ ]), null );
1567
1581
}
1568
-
1569
- $ scope = $ this ->processStmtVarAnnotation ($ scope , new Node \Stmt \Expression ($ expr , [
1570
- 'comments ' => $ expr ->getAttribute ('comments ' ),
1571
- ]), null );
1572
1582
}
1573
1583
}
1574
1584
}
@@ -2820,7 +2830,14 @@ private function processStmtVarAnnotation(MutatingScope $scope, Node\Stmt $stmt,
2820
2830
return $ scope ;
2821
2831
}
2822
2832
2823
- private function processVarAnnotation (MutatingScope $ scope , string $ variableName , string $ comment , bool $ strict , bool &$ changed = false ): MutatingScope
2833
+ /**
2834
+ * @param MutatingScope $scope
2835
+ * @param array<int, string> $variableNames
2836
+ * @param string $comment
2837
+ * @param bool $changed
2838
+ * @return MutatingScope
2839
+ */
2840
+ private function processVarAnnotation (MutatingScope $ scope , array $ variableNames , string $ comment , bool &$ changed = false ): MutatingScope
2824
2841
{
2825
2842
$ function = $ scope ->getFunction ();
2826
2843
$ resolvedPhpDoc = $ this ->fileTypeMapper ->getResolvedPhpDoc (
@@ -2832,27 +2849,29 @@ private function processVarAnnotation(MutatingScope $scope, string $variableName
2832
2849
);
2833
2850
$ varTags = $ resolvedPhpDoc ->getVarTags ();
2834
2851
2835
- if (isset ($ varTags [$ variableName ])) {
2852
+ foreach ($ variableNames as $ variableName ) {
2853
+ if (!isset ($ varTags [$ variableName ])) {
2854
+ continue ;
2855
+ }
2856
+
2836
2857
$ variableType = $ varTags [$ variableName ]->getType ();
2837
2858
$ changed = true ;
2838
- return $ scope ->assignVariable ($ variableName , $ variableType );
2839
-
2859
+ $ scope = $ scope ->assignVariable ($ variableName , $ variableType );
2840
2860
}
2841
2861
2842
- if (! $ strict && count ($ varTags ) === 1 && isset ($ varTags [0 ])) {
2862
+ if (count ( $ variableNames ) === 1 && count ($ varTags ) === 1 && isset ($ varTags [0 ])) {
2843
2863
$ variableType = $ varTags [0 ]->getType ();
2844
2864
$ changed = true ;
2845
- return $ scope ->assignVariable ($ variableName , $ variableType );
2846
-
2865
+ $ scope = $ scope ->assignVariable ($ variableNames [0 ], $ variableType );
2847
2866
}
2848
2867
2849
2868
return $ scope ;
2850
2869
}
2851
2870
2852
2871
private function enterForeach (MutatingScope $ scope , Foreach_ $ stmt ): MutatingScope
2853
2872
{
2854
- $ comment = CommentHelper::getDocComment ($ stmt );
2855
2873
$ iterateeType = $ scope ->getType ($ stmt ->expr );
2874
+ $ vars = [];
2856
2875
if ($ stmt ->valueVar instanceof Variable && is_string ($ stmt ->valueVar ->name )) {
2857
2876
$ scope = $ scope ->enterForeach (
2858
2877
$ stmt ->expr ,
@@ -2863,21 +2882,17 @@ private function enterForeach(MutatingScope $scope, Foreach_ $stmt): MutatingSco
2863
2882
? $ stmt ->keyVar ->name
2864
2883
: null
2865
2884
);
2866
- if ($ comment !== null ) {
2867
- $ scope = $ this ->processVarAnnotation ($ scope , $ stmt ->valueVar ->name , $ comment , true );
2868
- }
2885
+ $ vars [] = $ stmt ->valueVar ->name ;
2869
2886
}
2870
2887
2871
2888
if (
2872
2889
$ stmt ->keyVar instanceof Variable && is_string ($ stmt ->keyVar ->name )
2873
2890
) {
2874
2891
$ scope = $ scope ->enterForeachKey ($ stmt ->expr , $ stmt ->keyVar ->name );
2875
-
2876
- if ($ comment !== null ) {
2877
- $ scope = $ this ->processVarAnnotation ($ scope , $ stmt ->keyVar ->name , $ comment , true );
2878
- }
2892
+ $ vars [] = $ stmt ->keyVar ->name ;
2879
2893
}
2880
2894
2895
+ $ comment = CommentHelper::getDocComment ($ stmt );
2881
2896
if (
2882
2897
$ comment === null
2883
2898
&& $ iterateeType instanceof ConstantArrayType
@@ -2902,7 +2917,6 @@ private function enterForeach(MutatingScope $scope, Foreach_ $stmt): MutatingSco
2902
2917
$ exprType = $ scope ->getType ($ stmt ->expr );
2903
2918
$ itemType = $ exprType ->getIterableValueType ();
2904
2919
$ scope = $ this ->lookForArrayDestructuringArray ($ scope , $ stmt ->valueVar , $ itemType );
2905
- $ comment = CommentHelper::getDocComment ($ stmt );
2906
2920
if ($ comment !== null ) {
2907
2921
foreach ($ stmt ->valueVar ->items as $ arrayItem ) {
2908
2922
if ($ arrayItem === null ) {
@@ -2912,11 +2926,15 @@ private function enterForeach(MutatingScope $scope, Foreach_ $stmt): MutatingSco
2912
2926
continue ;
2913
2927
}
2914
2928
2915
- $ scope = $ this -> processVarAnnotation ( $ scope , $ arrayItem ->value ->name , $ comment , true ) ;
2929
+ $ vars [] = $ arrayItem ->value ->name ;
2916
2930
}
2917
2931
}
2918
2932
}
2919
2933
2934
+ if ($ comment !== null ) {
2935
+ $ scope = $ this ->processVarAnnotation ($ scope , $ vars , $ comment );
2936
+ }
2937
+
2920
2938
return $ scope ;
2921
2939
}
2922
2940
0 commit comments