Skip to content
Open
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
16 changes: 15 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,20 @@ public function enterAnonymousFunctionWithoutReflection(
);
}

private function conditionalExpressionHolderMatches(ExpressionTypeHolder $specified, ExpressionTypeHolder $condition): bool
{
if ($specified->equals($condition)) {
return true;
}

$conditionType = $condition->getType();
if (!$conditionType instanceof IntegerRangeType) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instanceof *Type checks are not recommended and this one looks wrong. This should rely on another condition ; maybe a Type:: method ?

Think about this: Why would the check isSuperTypeOf should be made for some types and not for some others ?

Or maybe does it means that the fix should be somewhere else ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All done — both verification runs confirmed clean. The commit addressing VincentLanglet's review has been pushed to the PR.

return false;
}

return $conditionType->isSuperTypeOf($specified->getType())->yes();
}

private function expressionTypeIsUnchangeable(ExpressionTypeHolder $typeHolder): bool
{
$expr = $typeHolder->getExpr();
Expand Down Expand Up @@ -3218,7 +3232,7 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
}
foreach ($conditionalExpressions as $conditionalExpression) {
foreach ($conditionalExpression->getConditionExpressionTypeHolders() as $holderExprString => $conditionalTypeHolder) {
if (!array_key_exists($holderExprString, $specifiedExpressions) || !$specifiedExpressions[$holderExprString]->equals($conditionalTypeHolder)) {
if (!array_key_exists($holderExprString, $specifiedExpressions) || !$this->conditionalExpressionHolderMatches($specifiedExpressions[$holderExprString], $conditionalTypeHolder)) {
continue 2;
}
}
Expand Down
50 changes: 50 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-4090.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types = 1);

namespace Bug4090;

use function PHPStan\Testing\assertType;

/** @param string[] $a */
function foo(array $a): void
{
if (count($a) > 1) {
assertType('non-empty-array<string>', $a);
echo implode(',', $a);
} elseif (count($a) === 1) {
assertType('non-empty-array<string>', $a);
echo trim(current($a));
}
}


/** @param string[] $a */
function bar(array $a): void
{
$count = count($a);
if ($count > 1) {
assertType('non-empty-array<string>', $a);
echo implode(',', $a);
} elseif ($count === 1) {
assertType('non-empty-array<string>', $a);
echo trim(current($a));
}
}


/** @param string[] $a */
function qux(array $a): void
{
switch (count($a)) {
case 0:
assertType('array{}', $a);
break;
case 1:
assertType('non-empty-array<string>', $a);
echo trim(current($a));
break;
default:
assertType('non-empty-array<string>', $a);
echo implode(',', $a);
break;
}
}
Loading