Skip to content
Merged
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
23 changes: 15 additions & 8 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3805,16 +3805,23 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
}

$conditions = [];
foreach ($scope->conditionalExpressions as $conditionalExprString => $conditionalExpressions) {
foreach ($conditionalExpressions as $conditionalExpression) {
foreach ($conditionalExpression->getConditionExpressionTypeHolders() as $holderExprString => $conditionalTypeHolder) {
if (!array_key_exists($holderExprString, $specifiedExpressions) || !$specifiedExpressions[$holderExprString]->equals($conditionalTypeHolder)) {
continue 2;
}
$prevSpecifiedCount = -1;
while (count($specifiedExpressions) !== $prevSpecifiedCount) {
$prevSpecifiedCount = count($specifiedExpressions);
foreach ($scope->conditionalExpressions as $conditionalExprString => $conditionalExpressions) {
if (array_key_exists($conditionalExprString, $conditions)) {
continue;
}
foreach ($conditionalExpressions as $conditionalExpression) {
foreach ($conditionalExpression->getConditionExpressionTypeHolders() as $holderExprString => $conditionalTypeHolder) {
if (!array_key_exists($holderExprString, $specifiedExpressions) || !$specifiedExpressions[$holderExprString]->equals($conditionalTypeHolder)) {
continue 2;
}
}

$conditions[$conditionalExprString][] = $conditionalExpression;
$specifiedExpressions[$conditionalExprString] = $conditionalExpression->getTypeHolder();
$conditions[$conditionalExprString][] = $conditionalExpression;
$specifiedExpressions[$conditionalExprString] = $conditionalExpression->getTypeHolder();
}
}
}

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

namespace Bug14178;

use function PHPStan\Testing\assertType;

class HelloWorld {
/**
* @return list<string>
*/
public static function diff(
?self $previousVersion,
?self $newVersion,
): array {
$previousVersionExists = $previousVersion !== null;
$newVersionExists = $newVersion !== null;

if (!$previousVersionExists && !$newVersionExists) {
return [];
}

if ($previousVersionExists && !$newVersionExists) {
return ['bar'];
}

if (!$previousVersionExists) {
assertType('true', $newVersionExists);
assertType('Bug14178\\HelloWorld', $newVersion);
$result = [];
$result[] = 'foo';
$categoryString = implode(', ', $newVersion->getSomething());
}

return [];
}

/**
* @return array<string>
*/
private function getSomething(): array {
return ['foo'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function (bool $a, bool $b) {

assertVariableCertainty(TrinaryLogic::createMaybe(), $foo);
if (returnsBool($b)) {
assertVariableCertainty(TrinaryLogic::createMaybe(), $foo); // could be Yes
assertVariableCertainty(TrinaryLogic::createYes(), $foo);
}

if (returnsBool($a)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function (bool $a, bool $b) {

assertVariableCertainty(TrinaryLogic::createMaybe(), $foo);
if ($b) {
assertVariableCertainty(TrinaryLogic::createMaybe(), $foo); // could be Yes
assertVariableCertainty(TrinaryLogic::createYes(), $foo);
}

if ($a) {
Expand Down
Loading