File tree Expand file tree Collapse file tree 2 files changed +112
-0
lines changed
Expand file tree Collapse file tree 2 files changed +112
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace Bug14213 ;
4+
5+ use function PHPStan \Testing \assertType ;
6+
7+ class HelloWorld
8+ {
9+ public static function coalesce_nonsensical (): void
10+ {
11+ $ x0 = $ x1 = $ x2 = null ;
12+
13+ if (rand (0 , 1 )) {
14+ $ x0 = rand (0 , 1 );
15+ $ x1 = rand (2 , 3 );
16+ $ x2 = rand (4 , 5 );
17+ }
18+
19+ // either all 3 variables are null, or all have a int-range value
20+ $ x = (
21+ $ x0 ??
22+ $ x1 ??
23+ $ x2
24+ );
25+
26+ assertType ('int<0, 1>|null ' , $ x );
27+ }
28+
29+ public static function coalesce_int_ranges (): void
30+ {
31+ $ x0 = $ x1 = $ x2 = null ;
32+
33+ if (rand (0 , 1 )) {
34+ $ x0 = rand (0 , 1 );
35+ }
36+ if (rand (0 , 1 )) {
37+ $ x1 = rand (2 , 3 );
38+ }
39+ if (rand (0 , 1 )) {
40+ $ x2 = rand (4 , 5 );
41+ }
42+
43+ $ x = (
44+ $ x0 ??
45+ $ x1 ??
46+ $ x2
47+ );
48+
49+ assertType ('int<0, 5>|null ' , $ x );
50+ }
51+
52+ public static function coalesce_int_range_after_maybe_defined (): void
53+ {
54+ $ x0 = $ x1 = $ x2 = null ;
55+
56+ if (rand (0 , 1 )) {
57+ $ maybeDefined = 10 ;
58+ }
59+ if (rand (0 , 1 )) {
60+ $ x0 = rand (0 , 1 );
61+ }
62+ if (rand (0 , 1 )) {
63+ $ x1 = rand (2 , 3 );
64+ }
65+ if (rand (0 , 1 )) {
66+ $ x2 = rand (4 , 5 );
67+ }
68+
69+ $ x = (
70+ $ maybeDefined ??
71+ $ x0 ??
72+ $ x1 ??
73+ $ x2
74+ );
75+
76+ assertType ('10|int<0, 5>|null ' , $ x );
77+ }
78+
79+ public static function coalesce_int_range_with_last_non_nullable (): void
80+ {
81+ $ x0 = $ x1 = null ;
82+ $ x2 = 20 ;
83+
84+ if (rand (0 , 1 )) {
85+ $ x0 = rand (0 , 1 );
86+ }
87+ if (rand (0 , 1 )) {
88+ $ x1 = rand (2 , 3 );
89+ }
90+ if (rand (0 , 1 )) {
91+ $ x2 = rand (4 , 5 );
92+ }
93+
94+ $ x = (
95+ $ x0 ??
96+ $ x1 ??
97+ $ x2 // cannot be null
98+ );
99+
100+ assertType ('20|int<0, 5> ' , $ x );
101+ }
102+ }
Original file line number Diff line number Diff line change @@ -352,4 +352,14 @@ public function testPr4372(): void
352352 $ this ->analyse ([__DIR__ . '/data/pr-4372-null-coalesce.php ' ], []);
353353 }
354354
355+ public function testBug14213 (): void
356+ {
357+ $ this ->analyse ([__DIR__ . '/../../Analyser/nsrt/bug-14213.php ' ], [
358+ [
359+ 'Variable $x1 on left side of ?? always exists and is always null. ' ,
360+ 22 ,
361+ ],
362+ ]);
363+ }
364+
355365}
You can’t perform that action at this time.
0 commit comments