11<?php
22
3- namespace PropertyNullAfterAssignment ;
3+ namespace PropertyNullAfterAssignmentStrictTypesDisabled ;
44
55use function PHPStan \Testing \assertNativeType ;
66use function PHPStan \Testing \assertType ;
77
8- class HelloWorld {
8+ class KeepsPropertyNonNullable {
99 private readonly int $ i ;
1010
1111 public function __construct ()
@@ -25,3 +25,47 @@ function getIntOrNull(): ?int {
2525 }
2626 return 1 ;
2727}
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