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
23 changes: 23 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3539,6 +3539,9 @@ private function createConditionalExpressions(
array $mergedExpressionTypes,
): array
{
$globalVariableCallback = fn (Node $node) => $node instanceof Variable && is_string($node->name) && $this->isGlobalVariable($node->name);
$nodeFinder = new NodeFinder();

$newVariableTypes = $ourExpressionTypes;
foreach ($theirExpressionTypes as $exprString => $holder) {
if (!array_key_exists($exprString, $mergedExpressionTypes)) {
Expand Down Expand Up @@ -3595,6 +3598,16 @@ private function createConditionalExpressions(
continue;
}

$expr = $holder->getExpr();
$containsSuperGlobal = $expr->getAttribute(self::CONTAINS_SUPER_GLOBAL_ATTRIBUTE_NAME);
if ($containsSuperGlobal === null) {
$containsSuperGlobal = $nodeFinder->findFirst($expr, $globalVariableCallback) !== null;
$expr->setAttribute(self::CONTAINS_SUPER_GLOBAL_ATTRIBUTE_NAME, $containsSuperGlobal);
}
if ($containsSuperGlobal === true) {
continue;
}

$variableTypeGuards = $typeGuards;
unset($variableTypeGuards[$exprString]);

Expand Down Expand Up @@ -3622,6 +3635,16 @@ private function createConditionalExpressions(
continue;
}

$expr = $mergedExprTypeHolder->getExpr();
$containsSuperGlobal = $expr->getAttribute(self::CONTAINS_SUPER_GLOBAL_ATTRIBUTE_NAME);
if ($containsSuperGlobal === null) {
$containsSuperGlobal = $nodeFinder->findFirst($expr, $globalVariableCallback) !== null;
$expr->setAttribute(self::CONTAINS_SUPER_GLOBAL_ATTRIBUTE_NAME, $containsSuperGlobal);
}
if ($containsSuperGlobal === true) {
continue;
}

foreach ($typeGuards as $guardExprString => $guardHolder) {
$conditionalExpression = new ConditionalExpressionHolder([$guardExprString => $guardHolder], new ExpressionTypeHolder($mergedExprTypeHolder->getExpr(), new ErrorType(), TrinaryLogic::createNo()));
$conditionalExpressions[$exprString][$conditionalExpression->getKey()] = $conditionalExpression;
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 @@ -526,6 +526,13 @@ public function testBug9503(): void
$this->analyse([__DIR__ . '/data/bug-9503.php'], []);
}

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

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

public function testBug14393(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
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