Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 02eb0a8

Browse files
Fix narrowing of superglobals
1 parent 72d2f3b commit 02eb0a8

File tree

6 files changed

+135
-0
lines changed

6 files changed

+135
-0
lines changed

‎src/Analyser/MutatingScope.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4833,6 +4833,8 @@ private function createConditionalExpressions(
48334833
private function mergeVariableHolders(array $ourVariableTypeHolders, array $theirVariableTypeHolders): array
48344834
{
48354835
$intersectedVariableTypeHolders = [];
4836+
$globalVariableCallback = fn (Node $node) => $node instanceof Variable && is_string($node->name) && $this->isGlobalVariable($node->name);
4837+
$nodeFinder = new NodeFinder();
48364838
foreach ($ourVariableTypeHolders as $exprString => $variableTypeHolder) {
48374839
if (isset($theirVariableTypeHolders[$exprString])) {
48384840
if ($variableTypeHolder === $theirVariableTypeHolders[$exprString]) {
@@ -4842,6 +4844,11 @@ private function mergeVariableHolders(array $ourVariableTypeHolders, array $thei
48424844

48434845
$intersectedVariableTypeHolders[$exprString] = $variableTypeHolder->and($theirVariableTypeHolders[$exprString]);
48444846
} else {
4847+
$expr = $variableTypeHolder->getExpr();
4848+
if ($nodeFinder->findFirst($expr, $globalVariableCallback) !== null) {
4849+
continue;
4850+
}
4851+
48454852
$intersectedVariableTypeHolders[$exprString] = ExpressionTypeHolder::createMaybe($variableTypeHolder->getExpr(), $variableTypeHolder->getType());
48464853
}
48474854
}
@@ -4851,6 +4858,11 @@ private function mergeVariableHolders(array $ourVariableTypeHolders, array $thei
48514858
continue;
48524859
}
48534860

4861+
$expr = $variableTypeHolder->getExpr();
4862+
if ($nodeFinder->findFirst($expr, $globalVariableCallback) !== null) {
4863+
continue;
4864+
}
4865+
48544866
$intersectedVariableTypeHolders[$exprString] = ExpressionTypeHolder::createMaybe($variableTypeHolder->getExpr(), $variableTypeHolder->getType());
48554867
}
48564868

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Superglobals;
4+
5+
use function PHPStan\Testing\assertNativeType;
6+
use function PHPStan\Testing\assertType;
7+
8+
class Superglobals
9+
{
10+
11+
public function originalTypes(): void
12+
{
13+
assertType('array<mixed>', $GLOBALS);
14+
assertType('array<mixed>', $_SERVER);
15+
assertType('array<mixed>', $_GET);
16+
assertType('array<mixed>', $_POST);
17+
assertType('array<mixed>', $_FILES);
18+
assertType('array<mixed>', $_COOKIE);
19+
assertType('array<mixed>', $_SESSION);
20+
assertType('array<mixed>', $_REQUEST);
21+
assertType('array<mixed>', $_ENV);
22+
}
23+
24+
public function canBeOverwritten(): void
25+
{
26+
$GLOBALS = [];
27+
assertType('array{}', $GLOBALS);
28+
assertNativeType('array{}', $GLOBALS);
29+
}
30+
31+
public function canBePartlyOverwritten(): void
32+
{
33+
$GLOBALS['foo'] = 'foo';
34+
assertType("non-empty-array&hasOffsetValue('foo', 'foo')", $GLOBALS);
35+
assertNativeType("non-empty-array&hasOffsetValue('foo', 'foo')", $GLOBALS);
36+
}
37+
38+
public function canBeNarrowed(): void
39+
{
40+
if (isset($GLOBALS['foo'])) {
41+
assertType("non-empty-array&hasOffsetValue('foo', mixed~null)", $GLOBALS);
42+
assertNativeType("non-empty-array<mixed>&hasOffset('foo')", $GLOBALS); // https://github.com/phpstan/phpstan/issues/8395
43+
} else {
44+
assertType('array<mixed>', $GLOBALS);
45+
assertNativeType('array<mixed>', $GLOBALS);
46+
}
47+
assertType('array<mixed>', $GLOBALS);
48+
assertNativeType('array<mixed>', $GLOBALS);
49+
}
50+
51+
}
52+
53+
function functionScope() {
54+
assertType('array<mixed>', $GLOBALS);
55+
assertNativeType('array<mixed>', $GLOBALS);
56+
}
57+
58+
assertType('array<mixed>', $GLOBALS);
59+
assertNativeType('array<mixed>', $GLOBALS);
60+
61+
function badNarrowing() {
62+
if (empty($_GET['id'])) {
63+
echo "b";
64+
} else {
65+
echo "b";
66+
}
67+
assertType('array<mixed>', $_GET);
68+
assertType('mixed', $_GET['id']);
69+
};

‎tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,4 +1001,9 @@ public function testHashing(): void
10011001
]);
10021002
}
10031003

1004+
public function testBug12772(): void
1005+
{
1006+
$this->analyse([__DIR__ . '/data/bug-12772.php'], []);
1007+
}
1008+
10041009
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12772;
4+
5+
class HelloWorld
6+
{
7+
public function sayHello(DateTimeImutable $date): void
8+
{
9+
foreach ($tables as $table) {
10+
11+
// If view exists, and 'add drop view' is selected: Drop it!
12+
if ($_POST['what'] !== 'nocopy' && isset($_POST['drop_if_exists']) && $_POST['drop_if_exists'] === 'true') {
13+
14+
}
15+
}
16+
}
17+
}

‎tests/PHPStan/Rules/Variables/IssetRuleTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,11 @@ public function testBug9328(): void
473473
$this->analyse([__DIR__ . '/data/bug-9328.php'], []);
474474
}
475475

476+
public function testBug12771(): void
477+
{
478+
$this->treatPhpDocTypesAsCertain = true;
479+
480+
$this->analyse([__DIR__ . '/data/bug-12771.php'], []);
481+
}
482+
476483
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug12771;
4+
5+
final class ErrorReportController
6+
{
7+
8+
public function __invoke(ServerRequest $request)
9+
{
10+
if (
11+
isset($_SESSION['prev_error_subm_time'], $_SESSION['error_subm_count'])
12+
&& $_SESSION['error_subm_count'] >= 3
13+
&& ($_SESSION['prev_error_subm_time'] - time()) <= 3000
14+
) {
15+
$_SESSION['error_subm_count'] = 0;
16+
$_SESSION['prev_errors'] = '';
17+
} else {
18+
$_SESSION['prev_error_subm_time'] = time();
19+
$_SESSION['error_subm_count'] = isset($_SESSION['error_subm_count'])
20+
? $_SESSION['error_subm_count'] + 1
21+
: 0;
22+
}
23+
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /