Skip to content

Fix issue 8467 intersect resolved conditional types #2059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 7, 2022
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
12 changes: 8 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
use function array_map;
use function array_merge;
use function array_pop;
use function array_reverse;
use function array_slice;
use function count;
use function explode;
Expand Down Expand Up @@ -3735,7 +3734,7 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
}

foreach ($scope->conditionalExpressions as $conditionalExprString => $conditionalExpressions) {
foreach (array_reverse($conditionalExpressions) as $conditionalExpression) {
foreach ($conditionalExpressions as $conditionalExpression) {
foreach ($conditionalExpression->getConditionExpressionTypeHolders() as $holderExprString => $conditionalTypeHolder) {
if (!array_key_exists($holderExprString, $specifiedExpressions) || !$specifiedExpressions[$holderExprString]->equals($conditionalTypeHolder)) {
continue 2;
Expand All @@ -3745,10 +3744,15 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
if ($conditionalExpression->getTypeHolder()->getCertainty()->no()) {
unset($scope->expressionTypes[$conditionalExprString]);
} else {
$scope->expressionTypes[$conditionalExprString] = $conditionalExpression->getTypeHolder();
$scope->expressionTypes[$conditionalExprString] = array_key_exists($conditionalExprString, $scope->expressionTypes)
? new ExpressionTypeHolder(
$scope->expressionTypes[$conditionalExprString]->getExpr(),
TypeCombinator::intersect($scope->expressionTypes[$conditionalExprString]->getType(), $conditionalExpression->getTypeHolder()->getType()),
TrinaryLogic::maxMin($scope->expressionTypes[$conditionalExprString]->getCertainty(), $conditionalExpression->getTypeHolder()->getCertainty()),
)
: $conditionalExpression->getTypeHolder();
$specifiedExpressions[$conditionalExprString] = $conditionalExpression->getTypeHolder();
}
continue 2;
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8421.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/imagick-pixel.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-8467a.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8467b.php');
}

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

namespace Bug8467b;

use function PHPStan\Testing\assertType;

class Test {
public function foo (?string $cwd, bool $initialClone = false): void {
if ($initialClone) {
$origCwd = $cwd;
$cwd = null;
}

if ($initialClone && isset($origCwd)) {
assertType('string', $origCwd);
assertType('string|null', $cwd); // could be null
}
}

/**
* @param mixed[]|null $mirrors
*
* @return list<non-empty-string>
*
* @phpstan-param list<array{url: non-empty-string, preferred: bool}>|null $mirrors
*/
protected function getUrls(?string $url, ?array $mirrors, ?string $ref, ?string $type, string $urlType): array
{
if (!$url) {
return [];
}

if ($urlType === 'dist' && false !== strpos($url, '%')) {
assertType('string|null', $type);
$url = 'test';
}
assertType('non-falsy-string', $url);

$urls = [$url];
if ($mirrors) {
foreach ($mirrors as $mirror) {
if ($urlType === 'dist') {
assertType('string|null', $type);
} elseif ($urlType === 'source' && $type === 'git') {
assertType("'git'", $type);
} elseif ($urlType === 'source' && $type === 'hg') {
assertType("'hg'", $type);
} else {
continue;
}
}
}

return $urls;
}
}