1
1
<?php
2
2
3
- namespace PropertyNullAfterAssignment ;
3
+ namespace PropertyNullAfterAssignmentStrictTypesDisabled ;
4
4
5
5
use function PHPStan \Testing \assertNativeType ;
6
6
use function PHPStan \Testing \assertType ;
7
7
8
- class HelloWorld {
8
+ class KeepsPropertyNonNullable {
9
9
private readonly int $ i ;
10
10
11
11
public function __construct ()
@@ -25,3 +25,47 @@ function getIntOrNull(): ?int {
25
25
}
26
26
return 1 ;
27
27
}
28
+
29
+
30
+ class KeepsPropertyNonNullable2 {
31
+ private readonly int |float $ i ;
32
+
33
+ public function __construct ()
34
+ {
35
+ $ this ->i = getIntOrFloatOrNull ();
36
+ }
37
+
38
+ public function doFoo (): void {
39
+ assertType ('float|int ' , $ this ->i );
40
+ assertNativeType ('float|int ' , $ this ->i );
41
+ }
42
+ }
43
+
44
+ function getIntOrFloatOrNull (): null |int |float {
45
+ if (rand (0 , 1 ) === 0 ) {
46
+ return null ;
47
+ }
48
+
49
+ if (rand (0 , 10 ) === 0 ) {
50
+ return 1.0 ;
51
+ }
52
+ return 1 ;
53
+ }
54
+
55
+ class NarrowsUnion {
56
+ private readonly int |float $ i ;
57
+
58
+ public function __construct ()
59
+ {
60
+ $ this ->i = getInt ();
61
+ }
62
+
63
+ public function doFoo (): void {
64
+ assertType ('int ' , $ this ->i );
65
+ assertNativeType ('int ' , $ this ->i );
66
+ }
67
+ }
68
+
69
+ function getInt (): int {
70
+ return 1 ;
71
+ }
0 commit comments