Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 8 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4141,17 +4141,21 @@ private function createConditionalExpressions(
continue;
}

$conditionalExpression = new ConditionalExpressionHolder($variableTypeGuards, $holder);
$conditionalExpressions[$exprString][$conditionalExpression->getKey()] = $conditionalExpression;
foreach ($variableTypeGuards as $guardExprString => $guardHolder) {
$conditionalExpression = new ConditionalExpressionHolder([$guardExprString => $guardHolder], $holder);
$conditionalExpressions[$exprString][$conditionalExpression->getKey()] = $conditionalExpression;
}
}

foreach ($mergedExpressionTypes as $exprString => $mergedExprTypeHolder) {
if (array_key_exists($exprString, $ourExpressionTypes)) {
continue;
}

$conditionalExpression = new ConditionalExpressionHolder($typeGuards, new ExpressionTypeHolder($mergedExprTypeHolder->getExpr(), new ErrorType(), TrinaryLogic::createNo()));
$conditionalExpressions[$exprString][$conditionalExpression->getKey()] = $conditionalExpression;
foreach ($typeGuards as $guardExprString => $guardHolder) {
$conditionalExpression = new ConditionalExpressionHolder([$guardExprString => $guardHolder], new ExpressionTypeHolder($mergedExprTypeHolder->getExpr(), new ErrorType(), TrinaryLogic::createNo()));
$conditionalExpressions[$exprString][$conditionalExpression->getKey()] = $conditionalExpression;
}
}

return $conditionalExpressions;
Expand Down
69 changes: 69 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-5051.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types = 1);

namespace Bug5051;

use function PHPStan\Testing\assertType;

class HelloWorld
{
public function test(?object $data): void
{
if ($data === null) {
$foo = 'bar';
$update = false;
} else {
$foo = 'baz';
$update = true;
}

assertType('object|null', $data);
assertType("'bar'|'baz'", $foo);

if ($update) {
assertType('object', $data);
}
}

public function testWithBooleans(?object $data): void
{
if ($data === null) {
$update = false;
$foo = false;
} else {
$update = true;
$foo = true;
}

if ($update) {
assertType('object', $data);
}
}

public function testWithDifferentVariableNames(?object $data): void
{
if ($data === null) {
$update = false;
$foo = 'bar';
} else {
$update = true;
$fuu = 'baz';
}

if ($update) {
assertType('object', $data);
}
}

public function testWithoutExtraAssignment(?object $data): void
{
if ($data === null) {
$update = false;
} else {
$update = true;
}

if ($update) {
assertType('object', $data);
}
}
}
Loading