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
6 changes: 6 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3263,6 +3263,12 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
foreach ($conditions as $conditionalExprString => $expressions) {
$certainty = TrinaryLogic::lazyExtremeIdentity($expressions, static fn (ConditionalExpressionHolder $holder) => $holder->getTypeHolder()->getCertainty());
if ($certainty->no()) {
if (
array_key_exists($conditionalExprString, $scope->expressionTypes)
&& $scope->expressionTypes[$conditionalExprString]->getCertainty()->yes()
) {
continue;
}
unset($scope->expressionTypes[$conditionalExprString]);
} else {
if (array_key_exists($conditionalExprString, $scope->expressionTypes)) {
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1531,4 +1531,13 @@ public function testBug6688(): void
]);
}

public function testBug14353(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-14353.php'], []);
}

}
68 changes: 68 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14353.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Bug14353;

/** @return array<int> */
function get(): array
{
return [1];
}

class Test
{
public function test(): void
{
$reports = [];

foreach (get() as $report) {
$reports[$report] = $report;
}

if (isset($this->data)) {
foreach ($reports as $report_id => $report) {
$report_ids[$report_id] = 1;
}
} else {
foreach ($reports as $report_id => $report) {
$report_ids[$report_id] = 1;
}
}

if (isset($report_ids)) {
var_dump($report_ids);

foreach ($reports as $report) {}

var_dump($report_ids);
}
}

public function testWithIfBranch(): void
{
$reports = [];

foreach (get() as $report) {
$reports[$report] = $report;
}

if (isset($this->data)) {
foreach ($reports as $report_id => $report) {
$report_ids[$report_id] = 1;
}
} else {
foreach ($reports as $report_id => $report) {
$report_ids[$report_id] = 1;
}
}

if (isset($report_ids)) {
var_dump($report_ids);

if ($reports === []) {
echo 'empty';
}

var_dump($report_ids);
}
}
}
Loading