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
13 changes: 12 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2758,14 +2758,25 @@
$nativeTypes = $scope->nativeExpressionTypes;
$nativeTypes[$exprString] = new ExpressionTypeHolder($expr, $nativeType, $certainty);

$conditionalExpressions = $this->conditionalExpressions;
if ($certainty->yes() && array_key_exists($exprString, $conditionalExpressions)) {
$conditionalExpressions[$exprString] = array_filter(
$conditionalExpressions[$exprString],
static fn (ConditionalExpressionHolder $holder) => !$holder->getTypeHolder()->getCertainty()->no(),

Check warning on line 2765 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": @@ @@ if ($certainty->yes() && array_key_exists($exprString, $conditionalExpressions)) { $conditionalExpressions[$exprString] = array_filter( $conditionalExpressions[$exprString], - static fn (ConditionalExpressionHolder $holder) => !$holder->getTypeHolder()->getCertainty()->no(), + static fn (ConditionalExpressionHolder $holder) => $holder->getTypeHolder()->getCertainty()->yes(), ); if ($conditionalExpressions[$exprString] === []) { unset($conditionalExpressions[$exprString]);

Check warning on line 2765 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": @@ @@ if ($certainty->yes() && array_key_exists($exprString, $conditionalExpressions)) { $conditionalExpressions[$exprString] = array_filter( $conditionalExpressions[$exprString], - static fn (ConditionalExpressionHolder $holder) => !$holder->getTypeHolder()->getCertainty()->no(), + static fn (ConditionalExpressionHolder $holder) => $holder->getTypeHolder()->getCertainty()->yes(), ); if ($conditionalExpressions[$exprString] === []) { unset($conditionalExpressions[$exprString]);
);
if ($conditionalExpressions[$exprString] === []) {
unset($conditionalExpressions[$exprString]);
}
}

$scope = $this->scopeFactory->create(
$this->context,
$this->isDeclareStrictTypes(),
$this->getFunction(),
$this->getNamespace(),
$expressionTypes,
$nativeTypes,
$this->conditionalExpressions,
$conditionalExpressions,
$this->inClosureBindScopeClasses,
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
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 @@ -1514,4 +1514,13 @@ public function testBug14117(): void
]);
}

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

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

namespace Bug14353;

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

class Test
{
/** @var mixed */
public $data;

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);
}
}
}
Loading