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
4 changes: 3 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3956,7 +3956,9 @@ public function mergeWith(?self $otherScope): self

$expr = $expressionTypeHolder->getExpr();

return $expr instanceof Variable || $expr instanceof VirtualNode;
return $expr instanceof Variable
|| $expr instanceof FuncCall
|| $expr instanceof VirtualNode;
};

$mergedExpressionTypes = array_filter($mergedExpressionTypes, $filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class NumberComparisonOperatorsConstantConditionRuleTest extends RuleTestCase

private bool $treatPhpDocTypesAsCertain = true;

private bool $polluteScopeWithAlwaysIterableForeach = true;

protected function getRule(): Rule
{
return new NumberComparisonOperatorsConstantConditionRule(
Expand All @@ -23,6 +25,11 @@ protected function getRule(): Rule
);
}

protected function shouldPolluteScopeWithAlwaysIterableForeach(): bool
{
return $this->polluteScopeWithAlwaysIterableForeach;
}

public function testBug8277(): void
{
$this->analyse([__DIR__ . '/data/bug-8277.php'], []);
Expand Down Expand Up @@ -269,6 +276,12 @@ public function testBug3387(): void
$this->analyse([__DIR__ . '/data/bug-3387.php'], []);
}

public function testBug13984(): void
{
$this->polluteScopeWithAlwaysIterableForeach = false;
$this->analyse([__DIR__ . '/data/bug-13984.php'], []);
}

public function testBug13874(): void
{
$this->analyse([__DIR__ . '/data/bug-13874.php'], []);
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-13984.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types = 1);

namespace Bug13984;

$list = [
'a',
'b',
'c',
];

/** @param list<string> $list */
function acceptList(array $list): bool {
if (count($list) < 1) {
return false;
}

$compare = ['a', 'b', 'c'];

foreach($list as $key => $item) {
foreach ($compare as $k => $v) {
if ($item === $v && $v !== 'a') {
unset($list[$key]);
}
}
}

if (count($list) > 0) {
return true;
}

return false;
}

assert(acceptList($list) === true);
Loading