Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Analyser/ExprHandler/AssignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,8 @@ private function processSureTypesForConditionalExpressionsAfterAssign(Scope $sco
!$expr instanceof PropertyFetch
&& !$expr instanceof ArrayDimFetch
&& !$expr instanceof FuncCall
&& !$expr instanceof MethodCall
&& !$expr instanceof Expr\StaticCall
) {
continue;
}
Expand Down Expand Up @@ -904,6 +906,8 @@ private function processSureNotTypesForConditionalExpressionsAfterAssign(Scope $
!$expr instanceof PropertyFetch
&& !$expr instanceof ArrayDimFetch
&& !$expr instanceof FuncCall
&& !$expr instanceof MethodCall
&& !$expr instanceof Expr\StaticCall
) {
continue;
}
Expand Down
44 changes: 44 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-5207.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug5207;

use function PHPStan\Testing\assertType;

abstract class HelloWorld {
abstract public function getChild(): ?HelloWorld;

public function sayHello(): void {
Comment thread
staabm marked this conversation as resolved.
$foo = null !== $this->getChild();
if ($foo) {
assertType('Bug5207\HelloWorld', $this->getChild());
}
}

public function sayHelloInline(): void {
if (null !== $this->getChild()) {
assertType('Bug5207\HelloWorld', $this->getChild());
}
}
}

abstract class ImpureWorld {
/**
* @phpstan-impure
*/
abstract public function getChild(): ?ImpureWorld;

public function sayHello(): void {
$foo = null !== $this->getChild();
if ($foo) {
assertType('Bug5207\ImpureWorld|null', $this->getChild());
}
}

public function sayHelloInline(): void {
if (null !== $this->getChild()) {
assertType('Bug5207\ImpureWorld|null', $this->getChild());
}
}
}
84 changes: 84 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9455.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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());
}
}
}
Loading