Skip to content

Commit 077c040

Browse files
committed
Update property-null-after-assignment.php
1 parent 9d91353 commit 077c040

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

tests/PHPStan/Analyser/nsrt/property-null-after-assignment.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3-
namespace PropertyNullAfterAssignment;
3+
namespace PropertyNullAfterAssignmentStrictTypesDisabled;
44

55
use function PHPStan\Testing\assertNativeType;
66
use 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

Comments
 (0)