Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
52 changes: 52 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9455.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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());
}
}
}
Loading