Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
20 changes: 19 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,24 @@
);
}

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

if (!$specified->getCertainty()->equals($condition->getCertainty())) {
return false;
}

$conditionType = $condition->getType();
if (!$conditionType->isInteger()->yes() || $conditionType->isConstantScalarValue()->yes()) {

Check warning on line 2158 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $conditionType = $condition->getType(); - if (!$conditionType->isInteger()->yes() || $conditionType->isConstantScalarValue()->yes()) { + if (!$conditionType->isInteger()->yes() || !$conditionType->isConstantScalarValue()->no()) { return false; }

Check warning on line 2158 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $conditionType = $condition->getType(); - if (!$conditionType->isInteger()->yes() || $conditionType->isConstantScalarValue()->yes()) { + if (!$conditionType->isInteger()->yes() || !$conditionType->isConstantScalarValue()->no()) { return false; }
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.

Can't this be extended to more Types ?

You ddin't explain why it would work for some and not some others. Stop focusing on IntegerRange

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 background tasks have finished. The work is complete — commit pushed, all tests and static analysis passing.

return false;
}

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

private function expressionTypeIsUnchangeable(ExpressionTypeHolder $typeHolder): bool
{
$expr = $typeHolder->getExpr();
Expand Down Expand Up @@ -3218,7 +3236,7 @@
}
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