Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
more tests
  • Loading branch information
staabm committed Apr 24, 2026
commit ba701cea561b7949581576a342307ce53b940158
37 changes: 27 additions & 10 deletions tests/PHPStan/Analyser/nsrt/bug-9455.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getId(): int {
/**
* @phpstan-pure
*/
public function getA(): ?A {
public function getPure(): ?A {
return $this->a;
}
}
Expand All @@ -36,10 +36,10 @@ public function testFails(): void
$a = new A(1);
$b = new B(1, $a);

$hasA = $b->getA() !== null;
$hasA = $b->getPure() !== null;

if($hasA) {
assertType('Bug9455\A', $b->getA());
assertType('Bug9455\A', $b->getPure());
}
}

Expand All @@ -48,8 +48,8 @@ public function testSucceeds(): void
$a = new A(1);
$b = new B(1, $a);

if($b->getA() !== null) {
assertType('Bug9455\A', $b->getA());
if($b->getPure() !== null) {
assertType('Bug9455\A', $b->getPure());
}
}
}
Expand All @@ -58,7 +58,7 @@ class C {
/**
* @phpstan-impure
*/
public function getA(): ?A {
public function getImpure(): ?A {
return rand(0, 1) ? new A(1) : null;
}
}
Expand All @@ -69,19 +69,36 @@ public function testImpureMethodNotNarrowed(): void
{
$c = new C();

$hasA = $c->getA() !== null;
$hasA = $c->getImpure() !== null;

if($hasA) {
assertType('Bug9455\A|null', $c->getA());
assertType('Bug9455\A|null', $c->getImpure());
}
}

public function testImpureMethodInline(): void
{
$c = new C();

if($c->getA() !== null) {
assertType('Bug9455\A|null', $c->getA());
if($c->getImpure() !== null) {
assertType('Bug9455\A|null', $c->getImpure());
}
}
}

class D {
public function getUnknownPurity(): ?A {
return rand(0, 1) ? new A(1) : null;
}
}

function doUnknownPurity(): void
{
$d = new D();

$hasA = $d->getUnknownPurity() !== null;

if($hasA) {
assertType('Bug9455\A|null', $d->getUnknownPurity());
}
}