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
22 changes: 20 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,20 @@
return in_array($variableName, self::SUPERGLOBAL_VARIABLES, true);
}

private function exprContainsSuperGlobal(Expr $expr): bool
{
$containsSuperGlobal = $expr->getAttribute(self::CONTAINS_SUPER_GLOBAL_ATTRIBUTE_NAME);
if ($containsSuperGlobal !== null) {
return $containsSuperGlobal;
}

$nodeFinder = new NodeFinder();
$containsSuperGlobal = $nodeFinder->findFirst($expr, fn (Node $node) => $node instanceof Variable && is_string($node->name) && $this->isGlobalVariable($node->name)) !== null;
$expr->setAttribute(self::CONTAINS_SUPER_GLOBAL_ATTRIBUTE_NAME, $containsSuperGlobal);

return $containsSuperGlobal;
}

/** @api */
public function hasConstant(Name $name): bool
{
Expand Down Expand Up @@ -3603,9 +3617,13 @@
}

foreach ($variableTypeGuards as $guardExprString => $guardHolder) {
$exprExistsInTheirs = array_key_exists($exprString, $theirExpressionTypes)
&& $theirExpressionTypes[$exprString]->getCertainty()->yes();

Check warning on line 3621 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": @@ @@ foreach ($variableTypeGuards as $guardExprString => $guardHolder) { $exprExistsInTheirs = array_key_exists($exprString, $theirExpressionTypes) - && $theirExpressionTypes[$exprString]->getCertainty()->yes(); + && !$theirExpressionTypes[$exprString]->getCertainty()->no(); if (!$exprExistsInTheirs) { $exprExistsInTheirs = $this->exprContainsSuperGlobal($holder->getExpr()); }

Check warning on line 3621 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": @@ @@ foreach ($variableTypeGuards as $guardExprString => $guardHolder) { $exprExistsInTheirs = array_key_exists($exprString, $theirExpressionTypes) - && $theirExpressionTypes[$exprString]->getCertainty()->yes(); + && !$theirExpressionTypes[$exprString]->getCertainty()->no(); if (!$exprExistsInTheirs) { $exprExistsInTheirs = $this->exprContainsSuperGlobal($holder->getExpr()); }
if (!$exprExistsInTheirs) {
$exprExistsInTheirs = $this->exprContainsSuperGlobal($holder->getExpr());
}
if (
array_key_exists($exprString, $theirExpressionTypes)
&& $theirExpressionTypes[$exprString]->getCertainty()->yes()
$exprExistsInTheirs
&& array_key_exists($guardExprString, $theirExpressionTypes)
&& $theirExpressionTypes[$guardExprString]->getCertainty()->yes()
&& !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no()
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,11 @@ public function testBug14393(): void
]);
}

public function testBug14421(): void
{
$this->treatPhpDocTypesAsCertain = true;

$this->analyse([__DIR__ . '/data/bug-14421.php'], []);
}

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

namespace Bug14421;

/** @phpstan-impure */
function get_optional_int(): ?int {
return random_int(0, 1) ? 42 : null;
}

if (isset($_SESSION['a'])) {
$b = $_SESSION['a'];
}
else {
$b = get_optional_int();
}
if ($b !== null) {
if (!isset($_SESSION['a'])) {
echo 'this is absolutely possible';
}
}
Loading