Skip to content
Merged
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
7 changes: 3 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,16 +548,15 @@ public function getVariableType(string $variableName): Type
}
}

if ($this->isGlobalVariable($variableName)) {
return new ArrayType(new BenevolentUnionType([new IntegerType(), new StringType()]), new MixedType(true));
}

if ($this->hasVariableType($variableName)->no()) {
throw new UndefinedVariableException($this, $variableName);
}

$varExprString = '$' . $variableName;
if (!array_key_exists($varExprString, $this->expressionTypes)) {
if ($this->isGlobalVariable($variableName)) {
return new ArrayType(new BenevolentUnionType([new IntegerType(), new StringType()]), new MixedType(true));
}
return new MixedType();
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ private static function findTestFiles(): iterable

yield __DIR__ . '/../Rules/Arrays/data/bug-11679.php';
yield __DIR__ . '/../Rules/Methods/data/bug-4801.php';
yield __DIR__ . '/../Rules/Arrays/data/narrow-superglobal.php';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,4 +870,11 @@ public function testBug8649(): void
$this->analyse([__DIR__ . '/data/bug-8649.php'], []);
}

public function testNarrowSuperglobals(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;

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

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/narrow-superglobal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace NarrowsSuperGlobal;

use function PHPStan\Testing\assertType;

class HelloWorld
{
public function doFoo(): void
{
if (array_key_exists('HTTP_HOST', $_SERVER)) {
assertType("non-empty-array&hasOffset('HTTP_HOST')", $_SERVER);
echo $_SERVER['HTTP_HOST'];
}
}
}
Loading