Skip to content

Commit 52e4b42

Browse files
committed
test nullable
1 parent 7fdb85b commit 52e4b42

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

tests/PHPStan/Analyser/nsrt/bug-12902-non-strict.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types = 0); // lint >= 8.1
22

3-
namespace Bug12902;
3+
namespace Bug12902NonStrict;
44

55
use function PHPStan\Testing\assertNativeType;
66
use function PHPStan\Testing\assertType;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php declare(strict_types = 0); // lint >= 8.1
2+
3+
namespace RememberNonNullablePropertyWhenStrictTypesDisabled;
4+
5+
use function PHPStan\Testing\assertNativeType;
6+
use function PHPStan\Testing\assertType;
7+
8+
class KeepsPropertyNonNullable {
9+
private readonly int $i;
10+
11+
public function __construct()
12+
{
13+
$this->i = getIntOrNull();
14+
}
15+
16+
public function doFoo(): void {
17+
assertType('int', $this->i);
18+
assertNativeType('int', $this->i);
19+
}
20+
}
21+
22+
class DontCoercePhpdocType {
23+
/** @var int */
24+
private $i;
25+
26+
public function __construct()
27+
{
28+
$this->i = getIntOrNull();
29+
}
30+
31+
public function doFoo(): void {
32+
assertType('int', $this->i);
33+
assertNativeType('mixed', $this->i);
34+
}
35+
}
36+
37+
function getIntOrNull(): ?int {
38+
if (rand(0, 1) === 0) {
39+
return null;
40+
}
41+
return 1;
42+
}
43+
44+
45+
class KeepsPropertyNonNullable2 {
46+
private int|float $i;
47+
48+
public function __construct()
49+
{
50+
$this->i = getIntOrFloatOrNull();
51+
}
52+
53+
public function doFoo(): void {
54+
assertType('float|int', $this->i);
55+
assertNativeType('float|int', $this->i);
56+
}
57+
}
58+
59+
function getIntOrFloatOrNull(): null|int|float {
60+
if (rand(0, 1) === 0) {
61+
return null;
62+
}
63+
64+
if (rand(0, 10) === 0) {
65+
return 1.0;
66+
}
67+
return 1;
68+
}
69+
70+
class NarrowsNativeUnion {
71+
private readonly int|float $i;
72+
73+
public function __construct()
74+
{
75+
$this->i = getInt();
76+
}
77+
78+
public function doFoo(): void {
79+
assertType('int', $this->i);
80+
assertNativeType('int', $this->i);
81+
}
82+
}
83+
84+
function getInt(): int {
85+
return 1;
86+
}

0 commit comments

Comments
 (0)