Skip to content

Commit 3d71766

Browse files
phpstan-botVincentLangletclaude
authored
Invalidate tracked superglobal narrowings after impure function, method, static and constructor calls (#6007)
Co-authored-by: VincentLanglet <9052536+VincentLanglet@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 56db2a0 commit 3d71766

3 files changed

Lines changed: 241 additions & 20 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -641,32 +641,19 @@ public function afterOpenSslCall(string $openSslFunctionName): self
641641
/**
642642
* Forgets every tracked volatile global-state expression: argument-less
643643
* function-call expressions whose value reflects mutable global/output-buffer
644-
* state rather than just their arguments (ob_get_level(), openssl_error_string()).
645-
* Any call to code PHPStan cannot inspect may change that state transitively, so
646-
* these narrowings must be forgotten afterwards. These functions take no
647-
* arguments, so the exact key lookups keep this O(1) in the common case where
648-
* nothing is tracked.
644+
* state rather than just their arguments (ob_get_level(), openssl_error_string()),
645+
* as well as superglobal variables and their offsets. Any call to code PHPStan
646+
* cannot inspect may change that state transitively, so these narrowings must be
647+
* forgotten afterwards. See VolatileExpressionHelper for the O(1) common-case
648+
* lookup strategy.
649649
*/
650650
public function invalidateVolatileExpressions(): self
651651
{
652652
$expressionTypes = $this->expressionTypes;
653653
$nativeExpressionTypes = $this->nativeExpressionTypes;
654654

655-
$changed = false;
656-
foreach (['ob_get_level', 'openssl_error_string'] as $functionName) {
657-
foreach ([$functionName . '()', '\\' . $functionName . '()'] as $exprString) {
658-
if (
659-
!array_key_exists($exprString, $expressionTypes)
660-
&& !array_key_exists($exprString, $nativeExpressionTypes)
661-
) {
662-
continue;
663-
}
664-
665-
unset($expressionTypes[$exprString]);
666-
unset($nativeExpressionTypes[$exprString]);
667-
$changed = true;
668-
}
669-
}
655+
$changed = VolatileExpressionHelper::invalidateVolatileFunctionCalls($expressionTypes, $nativeExpressionTypes);
656+
$changed = VolatileExpressionHelper::invalidateSuperglobals($expressionTypes, $nativeExpressionTypes) || $changed;
670657

671658
if (!$changed) {
672659
return $this;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use function array_key_exists;
6+
use function array_keys;
7+
use function str_starts_with;
8+
9+
/**
10+
* Removes tracked narrowings of mutable global state from an expression-type map.
11+
*
12+
* Two kinds of expressions are volatile in this sense: argument-less function-call
13+
* expressions whose value reflects mutable global/output-buffer state rather than
14+
* just their arguments (ob_get_level(), openssl_error_string()), and superglobal
15+
* variables together with their offsets ($_GET, $_GET['x'], ...). Any call to code
16+
* PHPStan cannot inspect may change that state transitively, so these narrowings
17+
* must be forgotten afterwards.
18+
*/
19+
final class VolatileExpressionHelper
20+
{
21+
22+
private const VOLATILE_FUNCTION_NAMES = ['ob_get_level', 'openssl_error_string'];
23+
24+
/**
25+
* Forgets tracked argument-less volatile function-call expressions.
26+
*
27+
* The functions take no arguments, so the exact key lookups keep this O(1) in
28+
* the common case where nothing is tracked.
29+
*
30+
* @param array<string, ExpressionTypeHolder> $expressionTypes
31+
* @param array<string, ExpressionTypeHolder> $nativeExpressionTypes
32+
* @return bool whether anything was removed
33+
*/
34+
public static function invalidateVolatileFunctionCalls(array &$expressionTypes, array &$nativeExpressionTypes): bool
35+
{
36+
$changed = false;
37+
foreach (self::VOLATILE_FUNCTION_NAMES as $functionName) {
38+
foreach ([$functionName . '()', '\\' . $functionName . '()'] as $exprString) {
39+
if (
40+
!array_key_exists($exprString, $expressionTypes)
41+
&& !array_key_exists($exprString, $nativeExpressionTypes)
42+
) {
43+
continue;
44+
}
45+
46+
unset($expressionTypes[$exprString]);
47+
unset($nativeExpressionTypes[$exprString]);
48+
$changed = true;
49+
}
50+
}
51+
52+
return $changed;
53+
}
54+
55+
/**
56+
* Forgets tracked superglobal variables and their offsets.
57+
*
58+
* The bare superglobal variables are looked up by exact key, keeping this O(1)
59+
* in the common case where nothing is tracked. The unbounded set of superglobal
60+
* offset keys ($_GET['x'], ...) is only scanned once a bare superglobal variable
61+
* is known to be tracked, which is guaranteed whenever any of its offsets is
62+
* tracked (narrowing an offset in specifyExpressionType() always also narrows the
63+
* container variable).
64+
*
65+
* @param array<string, ExpressionTypeHolder> $expressionTypes
66+
* @param array<string, ExpressionTypeHolder> $nativeExpressionTypes
67+
* @return bool whether anything was removed
68+
*/
69+
public static function invalidateSuperglobals(array &$expressionTypes, array &$nativeExpressionTypes): bool
70+
{
71+
$hasTrackedSuperglobal = false;
72+
foreach (Scope::SUPERGLOBAL_VARIABLES as $superglobalName) {
73+
$variableString = '$' . $superglobalName;
74+
if (
75+
!array_key_exists($variableString, $expressionTypes)
76+
&& !array_key_exists($variableString, $nativeExpressionTypes)
77+
) {
78+
continue;
79+
}
80+
81+
$hasTrackedSuperglobal = true;
82+
break;
83+
}
84+
85+
if (!$hasTrackedSuperglobal) {
86+
return false;
87+
}
88+
89+
$changed = false;
90+
foreach (array_keys($expressionTypes + $nativeExpressionTypes) as $exprString) {
91+
$isSuperglobal = false;
92+
foreach (Scope::SUPERGLOBAL_VARIABLES as $superglobalName) {
93+
$variableString = '$' . $superglobalName;
94+
if ($exprString === $variableString || str_starts_with($exprString, $variableString . '[')) {
95+
$isSuperglobal = true;
96+
break;
97+
}
98+
}
99+
100+
if (!$isSuperglobal) {
101+
continue;
102+
}
103+
104+
unset($expressionTypes[$exprString]);
105+
unset($nativeExpressionTypes[$exprString]);
106+
$changed = true;
107+
}
108+
109+
return $changed;
110+
}
111+
112+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12799;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function setGet(): void
8+
{
9+
$_GET['x'] = 'b';
10+
}
11+
12+
/** @phpstan-pure */
13+
function pureFunction(): int
14+
{
15+
return 1;
16+
}
17+
18+
class Setter
19+
{
20+
21+
public function set(): void
22+
{
23+
$_GET['x'] = 'b';
24+
}
25+
26+
public static function setStatic(): void
27+
{
28+
$_GET['x'] = 'b';
29+
}
30+
31+
public function __construct()
32+
{
33+
$_GET['x'] = 'b';
34+
}
35+
36+
}
37+
38+
class PureSetter
39+
{
40+
41+
/** @phpstan-pure */
42+
public function set(): int
43+
{
44+
return 1;
45+
}
46+
47+
/** @phpstan-pure */
48+
public static function setStatic(): int
49+
{
50+
return 1;
51+
}
52+
53+
/** @phpstan-pure */
54+
public function __construct()
55+
{
56+
}
57+
58+
}
59+
60+
function viaFunction(): void
61+
{
62+
$_GET['x'] = 'a';
63+
assertType("'a'", $_GET['x']);
64+
setGet();
65+
assertType('mixed', $_GET['x']);
66+
}
67+
68+
function viaMethod(Setter $s): void
69+
{
70+
$_GET['x'] = 'a';
71+
$s->set();
72+
assertType('mixed', $_GET['x']);
73+
}
74+
75+
function viaStaticMethod(): void
76+
{
77+
$_GET['x'] = 'a';
78+
Setter::setStatic();
79+
assertType('mixed', $_GET['x']);
80+
}
81+
82+
function viaNew(): void
83+
{
84+
$_GET['x'] = 'a';
85+
new Setter();
86+
assertType('mixed', $_GET['x']);
87+
}
88+
89+
function pureCallKeepsNarrowing(): void
90+
{
91+
$_GET['x'] = 'a';
92+
strlen('foo');
93+
assertType("'a'", $_GET['x']);
94+
}
95+
96+
function pureFunctionKeepsNarrowing(): void
97+
{
98+
$_GET['x'] = 'a';
99+
pureFunction();
100+
assertType("'a'", $_GET['x']);
101+
}
102+
103+
function pureMethodKeepsNarrowing(PureSetter $s): void
104+
{
105+
$_GET['x'] = 'a';
106+
$s->set();
107+
assertType("'a'", $_GET['x']);
108+
}
109+
110+
function pureStaticMethodKeepsNarrowing(): void
111+
{
112+
$_GET['x'] = 'a';
113+
PureSetter::setStatic();
114+
assertType("'a'", $_GET['x']);
115+
}
116+
117+
function pureNewKeepsNarrowing(): void
118+
{
119+
$_GET['x'] = 'a';
120+
new PureSetter();
121+
assertType("'a'", $_GET['x']);
122+
}

0 commit comments

Comments
 (0)