File tree 4 files changed +64
-1
lines changed
4 files changed +64
-1
lines changed Original file line number Diff line number Diff line change @@ -3677,6 +3677,7 @@ private function enterArrowFunctionWithoutReflection(Expr\ArrowFunction $arrowFu
3677
3677
$ variableTypes = $ this ->variableTypes ;
3678
3678
$ mixed = new MixedType ();
3679
3679
$ parameterVariables = [];
3680
+ $ parameterVariableExpressions = [];
3680
3681
foreach ($ arrowFunction ->params as $ i => $ parameter ) {
3681
3682
if ($ parameter ->type === null ) {
3682
3683
$ parameterType = $ mixed ;
@@ -3706,6 +3707,7 @@ private function enterArrowFunctionWithoutReflection(Expr\ArrowFunction $arrowFu
3706
3707
3707
3708
$ variableTypes [$ parameter ->var ->name ] = VariableTypeHolder::createYes ($ parameterType );
3708
3709
$ parameterVariables [] = $ parameter ->var ->name ;
3710
+ $ parameterVariableExpressions [] = $ parameter ->var ;
3709
3711
}
3710
3712
3711
3713
if ($ arrowFunction ->static ) {
@@ -3767,7 +3769,7 @@ private function enterArrowFunctionWithoutReflection(Expr\ArrowFunction $arrowFu
3767
3769
}
3768
3770
}
3769
3771
3770
- return $ this ->scopeFactory ->create (
3772
+ $ scope = $ this ->scopeFactory ->create (
3771
3773
$ this ->context ,
3772
3774
$ this ->isDeclareStrictTypes (),
3773
3775
$ this ->constantTypes ,
@@ -3785,6 +3787,12 @@ private function enterArrowFunctionWithoutReflection(Expr\ArrowFunction $arrowFu
3785
3787
$ this ->afterExtractCall ,
3786
3788
$ this ->parentScope ,
3787
3789
);
3790
+
3791
+ foreach ($ parameterVariableExpressions as $ expr ) {
3792
+ $ scope = $ scope ->invalidateExpression ($ expr );
3793
+ }
3794
+
3795
+ return $ scope ;
3788
3796
}
3789
3797
3790
3798
public function isParameterValueNullable (Node \Param $ parameter ): bool
Original file line number Diff line number Diff line change @@ -684,6 +684,7 @@ public function dataFileAsserts(): iterable
684
684
}
685
685
686
686
yield from $ this ->gatherAssertTypes (__DIR__ . '/data/bug-6500.php ' );
687
+ yield from $ this ->gatherAssertTypes (__DIR__ . '/../Rules/Comparison/data/bug-6473.php ' );
687
688
}
688
689
689
690
/**
Original file line number Diff line number Diff line change @@ -113,4 +113,10 @@ public function testTreatPhpDocTypesAsCertainRegression(bool $treatPhpDocTypesAs
113
113
$ this ->analyse ([__DIR__ . '/../DeadCode/data/bug-without-issue-1.php ' ], []);
114
114
}
115
115
116
+ public function testBug6473 (): void
117
+ {
118
+ $ this ->treatPhpDocTypesAsCertain = true ;
119
+ $ this ->analyse ([__DIR__ . '/data/bug-6473.php ' ], []);
120
+ }
121
+
116
122
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Bug6473 ;
4
+
5
+ use function PHPStan \Testing \assertType ;
6
+
7
+ class Point {
8
+ public bool $ visited = false ;
9
+
10
+ /**
11
+ * @return Point[]
12
+ */
13
+ public function getNeighbours (): array {
14
+ return [ new Point ];
15
+ }
16
+
17
+ public function doFoo ()
18
+ {
19
+ $ seen = [];
20
+
21
+ foreach ([new Point , new Point ] as $ p ) {
22
+
23
+ $ p ->visited = true ;
24
+ assertType ('true ' , $ p ->visited );
25
+ $ seen = [
26
+ ... $ seen ,
27
+ ... array_filter ( $ p ->getNeighbours (), static fn (Point $ p ) => !$ p ->visited )
28
+ ];
29
+ assertType ('true ' , $ p ->visited );
30
+ }
31
+ }
32
+
33
+ public function doFoo2 ()
34
+ {
35
+ $ seen = [];
36
+
37
+ foreach ([new Point , new Point ] as $ p ) {
38
+
39
+ $ p ->visited = true ;
40
+ assertType ('true ' , $ p ->visited );
41
+ $ seen = [
42
+ ... $ seen ,
43
+ ... array_filter ( $ p ->getNeighbours (), static fn (Point $ p2 ) => !$ p2 ->visited )
44
+ ];
45
+ assertType ('true ' , $ p ->visited );
46
+ }
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments