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
5 changes: 5 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3776,6 +3776,11 @@ private function createConditionalExpressions(
&& !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no()
)
|| $guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->yes()
|| (
!array_key_exists($exprString, $theirExpressionTypes)
&& $holder->getType()->equals($guardHolder->getType())
&& !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no()
)
)
) {
continue;
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14469.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Bug14469Nsrt;

use function PHPStan\Testing\assertType;

function testConditionalExpressionOverNarrowing(array $R, bool $var1, object $user): void {
$aa = null;

if ($var1) {
$aa = $user->id === 10 ? 2 : null;
} elseif ($R['aa']) {
$aa = $R['aa'];
}

if ($aa) {
assertType('mixed', $R['aa']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,10 @@ public function testBug12852(): void
]);
}

public function testBug14469(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-14469.php'], []);
}

}
88 changes: 88 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-14469.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php declare(strict_types = 1);

namespace Bug14469;

function t(array $R, bool $var1, object $user, bool $is): array {
$aa = null;

if ($var1) {
$aa = $user->id === 10 ? 2 : null;
} elseif ($R['aa']) {
$aa = $R['aa'];
}

if ($aa) {
if (!$R['aa']) {
return [];
}
}
return $R;
}

function testIfConstantCondition(array $R, bool $var1, object $user): void {
$aa = null;

if ($var1) {
$aa = $user->id === 10 ? 2 : null;
} elseif ($R['aa']) {
$aa = $R['aa'];
}

if ($aa) {
if ($R['aa']) {
// not always true
}
}
}

function testFalseyBranch(array $R, bool $var1, object $user): void {
$bb = 'default';

if ($var1) {
$bb = $user->id === 10 ? null : 'active';
} elseif (!$R['bb']) {
$bb = $R['bb'];
}

if (!$bb) {
if ($R['bb']) {
return;
}
}
}

function testWithElse(array $R, bool $var1, object $user): void {
$aa = null;

if ($var1) {
$aa = $user->id === 10 ? 2 : null;
} elseif ($R['aa']) {
$aa = $R['aa'];
} else {
$aa = 0;
}

if ($aa) {
if (!$R['aa']) {
return;
}
}
}

function testMultipleElseif(array $R, bool $var1, bool $var2, object $user): void {
$aa = null;

if ($var1) {
$aa = $user->id === 10 ? 2 : null;
} elseif ($var2) {
$aa = 42;
} elseif ($R['aa']) {
$aa = $R['aa'];
}

if ($aa) {
if (!$R['aa']) {
return;
}
}
}
Loading