forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-9455.php
More file actions
84 lines (65 loc) · 1.2 KB
/
bug-9455.php
File metadata and controls
84 lines (65 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php // lint >= 8.0
declare(strict_types = 1);
namespace Bug9455;
use function PHPStan\Testing\assertType;
class A {
public function __construct(private int $id){}
public function getId(): int {
return $this->id;
}
}
class B {
public function __construct(private int $id, private ?A $a = null){}
public function getId(): int {
return $this->id;
}
public function getA(): ?A {
return $this->a;
}
}
class HelloWorld
{
public function testFails(): void
{
$a = new A(1);
$b = new B(1, $a);
$hasA = $b->getA() !== null;
if($hasA) {
assertType('Bug9455\A', $b->getA());
}
}
public function testSucceeds(): void
{
$a = new A(1);
$b = new B(1, $a);
if($b->getA() !== null) {
assertType('Bug9455\A', $b->getA());
}
}
}
class C {
/**
* @phpstan-impure
*/
public function getA(): ?A {
return rand(0, 1) ? new A(1) : null;
}
}
class ImpureTest
{
public function testImpureMethodNotNarrowed(): void
{
$c = new C();
$hasA = $c->getA() !== null;
if($hasA) {
assertType('Bug9455\A|null', $c->getA());
}
}
public function testImpureMethodInline(): void
{
$c = new C();
if($c->getA() !== null) {
assertType('Bug9455\A|null', $c->getA());
}
}
}