Skip to content

Fix narrowing of superglobals #3901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
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
12 changes: 12 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4833,6 +4833,8 @@ private function createConditionalExpressions(
private function mergeVariableHolders(array $ourVariableTypeHolders, array $theirVariableTypeHolders): array
{
$intersectedVariableTypeHolders = [];
$globalVariableCallback = fn (Node $node) => $node instanceof Variable && is_string($node->name) && $this->isGlobalVariable($node->name);
$nodeFinder = new NodeFinder();
foreach ($ourVariableTypeHolders as $exprString => $variableTypeHolder) {
if (isset($theirVariableTypeHolders[$exprString])) {
if ($variableTypeHolder === $theirVariableTypeHolders[$exprString]) {
Expand All @@ -4842,6 +4844,11 @@ private function mergeVariableHolders(array $ourVariableTypeHolders, array $thei

$intersectedVariableTypeHolders[$exprString] = $variableTypeHolder->and($theirVariableTypeHolders[$exprString]);
} else {
$expr = $variableTypeHolder->getExpr();
if ($nodeFinder->findFirst($expr, $globalVariableCallback) !== null) {
continue;
}

$intersectedVariableTypeHolders[$exprString] = ExpressionTypeHolder::createMaybe($variableTypeHolder->getExpr(), $variableTypeHolder->getType());
}
}
Expand All @@ -4851,6 +4858,11 @@ private function mergeVariableHolders(array $ourVariableTypeHolders, array $thei
continue;
}

$expr = $variableTypeHolder->getExpr();
if ($nodeFinder->findFirst($expr, $globalVariableCallback) !== null) {
continue;
}

$intersectedVariableTypeHolders[$exprString] = ExpressionTypeHolder::createMaybe($variableTypeHolder->getExpr(), $variableTypeHolder->getType());
}

Expand Down
69 changes: 69 additions & 0 deletions tests/PHPStan/Analyser/nsrt/superglobals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types=1);

namespace Superglobals;

use function PHPStan\Testing\assertNativeType;
use function PHPStan\Testing\assertType;

class Superglobals
{

public function originalTypes(): void
{
assertType('array<mixed>', $GLOBALS);
assertType('array<mixed>', $_SERVER);
assertType('array<mixed>', $_GET);
assertType('array<mixed>', $_POST);
assertType('array<mixed>', $_FILES);
assertType('array<mixed>', $_COOKIE);
assertType('array<mixed>', $_SESSION);
assertType('array<mixed>', $_REQUEST);
assertType('array<mixed>', $_ENV);
}

public function canBeOverwritten(): void
{
$GLOBALS = [];
assertType('array{}', $GLOBALS);
assertNativeType('array{}', $GLOBALS);
}

public function canBePartlyOverwritten(): void
{
$GLOBALS['foo'] = 'foo';
assertType("non-empty-array&hasOffsetValue('foo', 'foo')", $GLOBALS);
assertNativeType("non-empty-array&hasOffsetValue('foo', 'foo')", $GLOBALS);
}

public function canBeNarrowed(): void
{
if (isset($GLOBALS['foo'])) {
assertType("non-empty-array&hasOffsetValue('foo', mixed~null)", $GLOBALS);
assertNativeType("non-empty-array<mixed>&hasOffset('foo')", $GLOBALS); // https://github.com/phpstan/phpstan/issues/8395
} else {
assertType('array<mixed>', $GLOBALS);
assertNativeType('array<mixed>', $GLOBALS);
}
assertType('array<mixed>', $GLOBALS);
assertNativeType('array<mixed>', $GLOBALS);
}

}

function functionScope() {
assertType('array<mixed>', $GLOBALS);
assertNativeType('array<mixed>', $GLOBALS);
}

assertType('array<mixed>', $GLOBALS);
assertNativeType('array<mixed>', $GLOBALS);

function badNarrowing() {
if (empty($_GET['id'])) {
echo "b";
} else {
echo "b";
}
assertType('array<mixed>', $_GET);
assertType('mixed', $_GET['id']);
};
Original file line number Diff line number Diff line change
Expand Up @@ -1001,4 +1001,9 @@ public function testHashing(): void
]);
}

public function testBug12772(): void
Copy link
Contributor

@mvorisek mvorisek Mar 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add also $_ENV['CI'] test explicitly, as I want to make sure GitHub CI environment variable is never assumed to exist on runtime - https://github.com/atk4/ui/blob/235416b31066827226e462f1916d9fc99c0d14b4/demos/init-db.php#L556. And thank you very much for looking into this issue!

Our reproducible environment is the same as here, GitHub CI, so as long as the tests passes here, our should pass then too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get your point, but adaptions here are not related to any environment variables. If Ondřej is fine with this, I expect it to be merged very soon and then we should also see the effects

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

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

namespace Bug12772;

class HelloWorld
{
public function sayHello(DateTimeImutable $date): void
{
foreach ($tables as $table) {

// If view exists, and 'add drop view' is selected: Drop it!
if ($_POST['what'] !== 'nocopy' && isset($_POST['drop_if_exists']) && $_POST['drop_if_exists'] === 'true') {

}
}
}
}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,11 @@ public function testBug9328(): void
$this->analyse([__DIR__ . '/data/bug-9328.php'], []);
}

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

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

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

namespace Bug12771;

final class ErrorReportController
{

public function __invoke(ServerRequest $request)
{
if (
isset($_SESSION['prev_error_subm_time'], $_SESSION['error_subm_count'])
&& $_SESSION['error_subm_count'] >= 3
&& ($_SESSION['prev_error_subm_time'] - time()) <= 3000
) {
$_SESSION['error_subm_count'] = 0;
$_SESSION['prev_errors'] = '';
} else {
$_SESSION['prev_error_subm_time'] = time();
$_SESSION['error_subm_count'] = isset($_SESSION['error_subm_count'])
? $_SESSION['error_subm_count'] + 1
: 0;
}

}
}
Loading