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
71 changes: 71 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3612,6 +3612,77 @@
}

if (count($typeGuards) === 0) {
// Look for variables whose certainty changed from YES (in our scope) to
// MAYBE (in merged scope) because they don't exist in their scope.
// For each such variable, look for potential type guards among variables
// that were removed from newVariableTypes (because merged type == their
// type) but whose type differs between our and their scope.
// This handles cases like: if (isset($a['key'])) { $b = ...; }
// where $b should be conditionally defined based on $a's narrowed type.
$certaintyChangedVars = [];
foreach ($newVariableTypes as $exprString => $holder) {
if (!$holder->getCertainty()->yes()) {

Check warning on line 3624 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": @@ @@ // where $b should be conditionally defined based on $a's narrowed type. $certaintyChangedVars = []; foreach ($newVariableTypes as $exprString => $holder) { - if (!$holder->getCertainty()->yes()) { + if ($holder->getCertainty()->no()) { continue; } if (!array_key_exists($exprString, $mergedExpressionTypes)) {

Check warning on line 3624 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": @@ @@ // where $b should be conditionally defined based on $a's narrowed type. $certaintyChangedVars = []; foreach ($newVariableTypes as $exprString => $holder) { - if (!$holder->getCertainty()->yes()) { + if ($holder->getCertainty()->no()) { continue; } if (!array_key_exists($exprString, $mergedExpressionTypes)) {
continue;
}
if (!array_key_exists($exprString, $mergedExpressionTypes)) {
continue;
}
if ($mergedExpressionTypes[$exprString]->getCertainty()->yes()) {
continue;
}
if (!$holder->getExpr() instanceof Variable) {
continue;
}
$certaintyChangedVars[$exprString] = $holder;
}

if (count($certaintyChangedVars) > 0) {
$recoveredTypeGuards = [];
foreach ($ourExpressionTypes as $exprString => $holder) {
if ($holder->getExpr() instanceof VirtualNode) {
continue;
}
if (array_key_exists($exprString, $newVariableTypes)) {
continue;
}
if (!array_key_exists($exprString, $mergedExpressionTypes)) {
continue;
}
if (!$holder->getCertainty()->yes()) {

Check warning on line 3651 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 (!array_key_exists($exprString, $mergedExpressionTypes)) { continue; } - if (!$holder->getCertainty()->yes()) { + if ($holder->getCertainty()->no()) { continue; } if (

Check warning on line 3651 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 (!array_key_exists($exprString, $mergedExpressionTypes)) { continue; } - if (!$holder->getCertainty()->yes()) { + if ($holder->getCertainty()->no()) { continue; } if (
continue;
}
if (
array_key_exists($exprString, $theirExpressionTypes)
&& !$theirExpressionTypes[$exprString]->getCertainty()->yes()

Check warning on line 3656 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 ( array_key_exists($exprString, $theirExpressionTypes) - && !$theirExpressionTypes[$exprString]->getCertainty()->yes() + && $theirExpressionTypes[$exprString]->getCertainty()->no() ) { continue; }

Check warning on line 3656 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 ( array_key_exists($exprString, $theirExpressionTypes) - && !$theirExpressionTypes[$exprString]->getCertainty()->yes() + && $theirExpressionTypes[$exprString]->getCertainty()->no() ) { continue; }
) {
continue;
}
if ($mergedExpressionTypes[$exprString]->equalTypes($holder)) {
continue;
}
$recoveredTypeGuards[$exprString] = $holder;
}

foreach ($certaintyChangedVars as $exprString => $holder) {
foreach ($recoveredTypeGuards as $guardExprString => $guardHolder) {
if ($guardExprString === $exprString) {
continue;
}
if (
array_key_exists($exprString, $theirExpressionTypes)
&& $theirExpressionTypes[$exprString]->getCertainty()->yes()

Check warning on line 3673 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 ( array_key_exists($exprString, $theirExpressionTypes) - && $theirExpressionTypes[$exprString]->getCertainty()->yes() + && !$theirExpressionTypes[$exprString]->getCertainty()->no() && array_key_exists($guardExprString, $theirExpressionTypes) && $theirExpressionTypes[$guardExprString]->getCertainty()->yes() && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no()

Check warning on line 3673 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 ( array_key_exists($exprString, $theirExpressionTypes) - && $theirExpressionTypes[$exprString]->getCertainty()->yes() + && !$theirExpressionTypes[$exprString]->getCertainty()->no() && array_key_exists($guardExprString, $theirExpressionTypes) && $theirExpressionTypes[$guardExprString]->getCertainty()->yes() && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no()
&& array_key_exists($guardExprString, $theirExpressionTypes)
&& $theirExpressionTypes[$guardExprString]->getCertainty()->yes()

Check warning on line 3675 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": @@ @@ array_key_exists($exprString, $theirExpressionTypes) && $theirExpressionTypes[$exprString]->getCertainty()->yes() && array_key_exists($guardExprString, $theirExpressionTypes) - && $theirExpressionTypes[$guardExprString]->getCertainty()->yes() + && !$theirExpressionTypes[$guardExprString]->getCertainty()->no() && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() ) { continue;

Check warning on line 3675 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": @@ @@ array_key_exists($exprString, $theirExpressionTypes) && $theirExpressionTypes[$exprString]->getCertainty()->yes() && array_key_exists($guardExprString, $theirExpressionTypes) - && $theirExpressionTypes[$guardExprString]->getCertainty()->yes() + && !$theirExpressionTypes[$guardExprString]->getCertainty()->no() && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() ) { continue;
&& !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no()

Check warning on line 3676 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": @@ @@ && $theirExpressionTypes[$exprString]->getCertainty()->yes() && array_key_exists($guardExprString, $theirExpressionTypes) && $theirExpressionTypes[$guardExprString]->getCertainty()->yes() - && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() + && $guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->yes() ) { continue; }

Check warning on line 3676 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\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ && $theirExpressionTypes[$exprString]->getCertainty()->yes() && array_key_exists($guardExprString, $theirExpressionTypes) && $theirExpressionTypes[$guardExprString]->getCertainty()->yes() - && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() + && !$theirExpressionTypes[$guardExprString]->getType()->isSuperTypeOf($guardHolder->getType())->no() ) { continue; }

Check warning on line 3676 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": @@ @@ && $theirExpressionTypes[$exprString]->getCertainty()->yes() && array_key_exists($guardExprString, $theirExpressionTypes) && $theirExpressionTypes[$guardExprString]->getCertainty()->yes() - && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() + && $guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->yes() ) { continue; }

Check warning on line 3676 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\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ && $theirExpressionTypes[$exprString]->getCertainty()->yes() && array_key_exists($guardExprString, $theirExpressionTypes) && $theirExpressionTypes[$guardExprString]->getCertainty()->yes() - && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() + && !$theirExpressionTypes[$guardExprString]->getType()->isSuperTypeOf($guardHolder->getType())->no() ) { continue; }
) {
continue;
}
$conditionalExpression = new ConditionalExpressionHolder([$guardExprString => $guardHolder], $holder);
$conditionalExpressions[$exprString][$conditionalExpression->getKey()] = $conditionalExpression;
}
}
}

return $conditionalExpressions;
}

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 @@ -1517,4 +1517,13 @@ public function testBug11218(): void
$this->analyse([__DIR__ . '/data/bug-11218.php'], []);
}

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

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-9426.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace Bug9426;

final class A
{
/**
* @param array{something?: string} $a
*/
public function something(array $a): void
{
if (isset($a['something'])) {
$b = new \DateTimeImmutable();
}

$c = [
!isset($a['something']) ? 'something' : $b,
];
}
}
Loading