Skip to content

Commit 6b9d3e3

Browse files
zonuexeclaude
authored andcommitted
Describe the seeded random number generator as a named part of global state
mt_rand(), rand(), shuffle(), str_shuffle(), array_rand(), mt_srand() and srand() list writes-global.random instead of writes-global, so they no longer forget unrelated remembered reads of global state, such as ini_get() and is_file(), or narrowed superglobals. Nothing reads that state in a remembered way: its readers give a different result on every call. Listing it keeps a call that only advances it, such as rand() or str_shuffle(), on its own line from being reported as having no effect. lcg_value() lists nondeterministic.random only, like random_int(): its generator is its own, mt_srand() and srand() do not seed it, and its readers are random as well. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 712fb79 commit 6b9d3e3

9 files changed

Lines changed: 242 additions & 18 deletions

File tree

‎bin/functionMetadata_original.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
'array_pop' => ['effects' => [SideEffects::WRITES_ARGS . '($array, $stack)']],
101101
'array_product' => ['effects' => []],
102102
'array_push' => ['effects' => [SideEffects::WRITES_ARGS . '($array, $stack)']],
103-
'array_rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL]],
103+
'array_rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL_RANDOM]],
104104
'array_reduce' => ['effects' => [], 'pureUnlessCallableIsImpureParameters' => ['callback' => true]],
105105
'array_replace' => ['effects' => []],
106106
'array_replace_recursive' => ['effects' => []],
@@ -342,7 +342,7 @@
342342
'juliantojd' => ['effects' => []],
343343
'krsort' => ['effects' => [SideEffects::WRITES_ARGS . '($array, $array_arg)']],
344344
'ksort' => ['effects' => [SideEffects::WRITES_ARGS . '($array, $array_arg)']],
345-
'lcg_value' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL]],
345+
'lcg_value' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM]],
346346
'lchgrp' => ['effects' => [SideEffects::IO_FILES, SideEffects::WRITES_GLOBAL, SideEffects::NONDETERMINISTIC]],
347347
'lchown' => ['effects' => [SideEffects::IO_FILES, SideEffects::WRITES_GLOBAL, SideEffects::NONDETERMINISTIC]],
348348
'levenshtein' => ['effects' => []],
@@ -369,7 +369,7 @@
369369
'msg_send' => ['effects' => [SideEffects::IO_PROCESS, SideEffects::NONDETERMINISTIC]],
370370
'msg_set_queue' => ['effects' => [SideEffects::IO_PROCESS, SideEffects::NONDETERMINISTIC]],
371371
'msg_stat_queue' => ['effects' => [SideEffects::IO_PROCESS, SideEffects::NONDETERMINISTIC]],
372-
'mt_srand' => ['effects' => [SideEffects::WRITES_GLOBAL]],
372+
'mt_srand' => ['effects' => [SideEffects::WRITES_GLOBAL_RANDOM]],
373373
'mysqli_affected_rows' => ['effects' => ['*']],
374374
'mysqli_autocommit' => ['effects' => ['*']],
375375
'mysqli_begin_transaction' => ['effects' => ['*']],
@@ -627,7 +627,7 @@
627627
'shmop_size' => ['effects' => [SideEffects::IO_PROCESS, SideEffects::NONDETERMINISTIC]],
628628
'shmop_write' => ['effects' => [SideEffects::IO_PROCESS, SideEffects::NONDETERMINISTIC]],
629629
'show_source' => ['effects' => [SideEffects::IO_FILES, SideEffects::IO_OUTPUT, SideEffects::NONDETERMINISTIC]],
630-
'shuffle' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL, SideEffects::WRITES_ARGS . '($array, $array_arg)']],
630+
'shuffle' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL_RANDOM, SideEffects::WRITES_ARGS . '($array, $array_arg)']],
631631
'similar_text' => ['effects' => [], 'pureUnlessParameterPassedParameters' => ['percent' => true]],
632632
'readfile' => ['effects' => [SideEffects::IO, SideEffects::IO_OUTPUT, SideEffects::NONDETERMINISTIC]],
633633
'rename' => ['effects' => [SideEffects::IO_FILES, SideEffects::WRITES_GLOBAL, SideEffects::NONDETERMINISTIC]],
@@ -780,15 +780,15 @@
780780
'spl_autoload_unregister' => ['effects' => [SideEffects::WRITES_GLOBAL]],
781781
'spl_object_id' => ['effects' => []],
782782
'sprintf' => ['effects' => []],
783-
'srand' => ['effects' => [SideEffects::WRITES_GLOBAL]],
783+
'srand' => ['effects' => [SideEffects::WRITES_GLOBAL_RANDOM]],
784784
'sscanf' => ['effects' => [], 'pureUnlessParameterPassedParameters' => ['vars' => true, 'war' => true]],
785785
'str_decrement' => ['effects' => []],
786786
'str_increment' => ['effects' => []],
787787
// 'count'/'replace_count': PHP 8+ uses the php-8-stubs parameter name, PHP <8 falls
788788
// back to the legacy functionMap.php name.
789789
'str_ireplace' => ['effects' => [], 'pureUnlessParameterPassedParameters' => ['count' => true, 'replace_count' => true]],
790790
'str_replace' => ['effects' => [], 'pureUnlessParameterPassedParameters' => ['count' => true, 'replace_count' => true]],
791-
'str_shuffle' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL]],
791+
'str_shuffle' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL_RANDOM]],
792792
'strcoll' => ['effects' => [SideEffects::READS_GLOBAL]],
793793
'stream_bucket_append' => ['effects' => [SideEffects::WRITES_ARGS . '($brigade)']],
794794
'stream_bucket_make_writeable' => ['effects' => [SideEffects::NONDETERMINISTIC, SideEffects::WRITES_ARGS . '($brigade)']],
@@ -844,8 +844,8 @@
844844
'var_dump' => ['effects' => [SideEffects::IO_OUTPUT]],
845845

846846
// random functions, do not have side effects but are not deterministic
847-
'mt_rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL]],
848-
'rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL]],
847+
'mt_rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL_RANDOM]],
848+
'rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL_RANDOM]],
849849
'random_bytes' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM]],
850850
'random_int' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM]],
851851
'vfprintf' => ['effects' => [SideEffects::IO, SideEffects::WRITES_ARGS . '($stream)', SideEffects::NONDETERMINISTIC]],

‎resources/functionMetadata.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@
10151015
'array_pop' => ['effects' => [SideEffects::WRITES_ARGS . '($array, $stack)']],
10161016
'array_product' => ['effects' => []],
10171017
'array_push' => ['effects' => [SideEffects::WRITES_ARGS . '($array, $stack)']],
1018-
'array_rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL]],
1018+
'array_rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL_RANDOM]],
10191019
'array_reduce' => ['effects' => [], 'pureUnlessCallableIsImpureParameters' => ['callback' => true]],
10201020
'array_replace' => ['effects' => []],
10211021
'array_replace_recursive' => ['effects' => []],
@@ -1709,7 +1709,7 @@
17091709
'krsort' => ['effects' => [SideEffects::WRITES_ARGS . '($array, $array_arg)']],
17101710
'ksort' => ['effects' => [SideEffects::WRITES_ARGS . '($array, $array_arg)']],
17111711
'lcfirst' => ['effects' => []],
1712-
'lcg_value' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL]],
1712+
'lcg_value' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM]],
17131713
'lchgrp' => ['effects' => [SideEffects::IO_FILES, SideEffects::WRITES_GLOBAL, SideEffects::NONDETERMINISTIC]],
17141714
'lchown' => ['effects' => [SideEffects::IO_FILES, SideEffects::WRITES_GLOBAL, SideEffects::NONDETERMINISTIC]],
17151715
'levenshtein' => ['effects' => []],
@@ -1849,8 +1849,8 @@
18491849
'msgfmt_parse' => ['effects' => []],
18501850
'msgfmt_parse_message' => ['effects' => []],
18511851
'mt_getrandmax' => ['effects' => []],
1852-
'mt_rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL]],
1853-
'mt_srand' => ['effects' => [SideEffects::WRITES_GLOBAL]],
1852+
'mt_rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL_RANDOM]],
1853+
'mt_srand' => ['effects' => [SideEffects::WRITES_GLOBAL_RANDOM]],
18541854
'mysqli_affected_rows' => ['effects' => ['*']],
18551855
'mysqli_autocommit' => ['effects' => ['*']],
18561856
'mysqli_begin_transaction' => ['effects' => ['*']],
@@ -2179,7 +2179,7 @@
21792179
'quoted_printable_encode' => ['effects' => []],
21802180
'quotemeta' => ['effects' => []],
21812181
'rad2deg' => ['effects' => []],
2182-
'rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL]],
2182+
'rand' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL_RANDOM]],
21832183
'random_bytes' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM]],
21842184
'random_int' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM]],
21852185
'range' => ['effects' => []],
@@ -2265,7 +2265,7 @@
22652265
'shmop_size' => ['effects' => [SideEffects::IO_PROCESS, SideEffects::NONDETERMINISTIC]],
22662266
'shmop_write' => ['effects' => [SideEffects::IO_PROCESS, SideEffects::NONDETERMINISTIC]],
22672267
'show_source' => ['effects' => [SideEffects::IO_FILES, SideEffects::IO_OUTPUT, SideEffects::NONDETERMINISTIC]],
2268-
'shuffle' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL, SideEffects::WRITES_ARGS . '($array, $array_arg)']],
2268+
'shuffle' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL_RANDOM, SideEffects::WRITES_ARGS . '($array, $array_arg)']],
22692269
'similar_text' => ['effects' => [], 'pureUnlessParameterPassedParameters' => ['percent' => true]],
22702270
'simplexml_import_dom' => ['effects' => []],
22712271
'sin' => ['effects' => []],
@@ -2421,7 +2421,7 @@
24212421
'spl_object_id' => ['effects' => []],
24222422
'sprintf' => ['effects' => []],
24232423
'sqrt' => ['effects' => []],
2424-
'srand' => ['effects' => [SideEffects::WRITES_GLOBAL]],
2424+
'srand' => ['effects' => [SideEffects::WRITES_GLOBAL_RANDOM]],
24252425
'sscanf' => ['effects' => [], 'pureUnlessParameterPassedParameters' => ['vars' => true, 'war' => true]],
24262426
'stat' => ['effects' => [SideEffects::READS_GLOBAL_STAT_CACHE]],
24272427
'str_contains' => ['effects' => []],
@@ -2434,7 +2434,7 @@
24342434
'str_repeat' => ['effects' => []],
24352435
'str_replace' => ['effects' => [], 'pureUnlessParameterPassedParameters' => ['count' => true, 'replace_count' => true]],
24362436
'str_rot13' => ['effects' => []],
2437-
'str_shuffle' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL]],
2437+
'str_shuffle' => ['effects' => [SideEffects::NONDETERMINISTIC_RANDOM, SideEffects::WRITES_GLOBAL_RANDOM]],
24382438
'str_split' => ['effects' => []],
24392439
'str_starts_with' => ['effects' => []],
24402440
'str_word_count' => ['effects' => []],

‎src/Reflection/SideEffects.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ final class SideEffects
6969
public const WRITES_GLOBAL_SESSION = 'writes-global.session';
7070
public const READS_GLOBAL_SESSION = 'reads-global.session';
7171

72+
/**
73+
* The state of the seeded random number generator, which mt_srand() and
74+
* srand() seed and mt_rand(), rand(), shuffle(), str_shuffle() and
75+
* array_rand() advance. No call that reads it is remembered, so writing it
76+
* forgets nothing; it keeps a call that only advances it, such as rand(),
77+
* on its own line from being reported as having no effect.
78+
*/
79+
public const WRITES_GLOBAL_RANDOM = 'writes-global.random';
80+
7281
/**
7382
* The last error of an extension, which its error getters read. Writing it
7483
* is allowed in pure functions and makes a call on its own line useful.

‎tests/PHPStan/Analyser/nsrt/side-effects-builtin-areas.php‎

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public function sleepAndOutputKeepGlobalState(): void
1919
}
2020
}
2121

22-
public function seedingWritesGlobalState(): void
22+
public function seedingWritesOnlyTheRandomState(): void
2323
{
2424
if (ini_get('precision') === '14') {
2525
mt_srand(1);
26-
assertType('numeric-string', ini_get('precision'));
26+
assertType("'14'", ini_get('precision'));
2727
}
2828
if (error_reporting() === 0) {
2929
error_reporting(E_ALL);
@@ -67,6 +67,159 @@ public function randomBytesKeepsGlobalState(string $path): bool
6767
return true;
6868
}
6969

70+
/**
71+
* they write only the state of the seeded random number generator
72+
*
73+
* @param list<int> $a
74+
* @param non-empty-list<string> $list
75+
*/
76+
public function seededRandomKeepsOtherGlobalState(string $case, string $file, array $a, array $list): void
77+
{
78+
if ($case === 'mt_rand') {
79+
if (ini_get('x') === false) {
80+
return;
81+
}
82+
if (!is_file($file)) {
83+
return;
84+
}
85+
if (!isset($_GET['q']) || !is_string($_GET['q'])) {
86+
return;
87+
}
88+
89+
mt_rand();
90+
91+
assertType('string', ini_get('x'));
92+
assertType('true', is_file($file));
93+
assertType('string', $_GET['q']);
94+
}
95+
96+
if ($case === 'rand') {
97+
if (ini_get('x') === false) {
98+
return;
99+
}
100+
if (!is_file($file)) {
101+
return;
102+
}
103+
if (!isset($_GET['q']) || !is_string($_GET['q'])) {
104+
return;
105+
}
106+
107+
rand();
108+
109+
assertType('string', ini_get('x'));
110+
assertType('true', is_file($file));
111+
assertType('string', $_GET['q']);
112+
}
113+
114+
if ($case === 'mt_srand') {
115+
if (ini_get('x') === false) {
116+
return;
117+
}
118+
if (!is_file($file)) {
119+
return;
120+
}
121+
if (!isset($_GET['q']) || !is_string($_GET['q'])) {
122+
return;
123+
}
124+
125+
mt_srand(1);
126+
127+
assertType('string', ini_get('x'));
128+
assertType('true', is_file($file));
129+
assertType('string', $_GET['q']);
130+
}
131+
132+
if ($case === 'srand') {
133+
if (ini_get('x') === false) {
134+
return;
135+
}
136+
if (!is_file($file)) {
137+
return;
138+
}
139+
if (!isset($_GET['q']) || !is_string($_GET['q'])) {
140+
return;
141+
}
142+
143+
srand(1);
144+
145+
assertType('string', ini_get('x'));
146+
assertType('true', is_file($file));
147+
assertType('string', $_GET['q']);
148+
}
149+
150+
if ($case === 'shuffle') {
151+
if (ini_get('x') === false) {
152+
return;
153+
}
154+
if (!is_file($file)) {
155+
return;
156+
}
157+
if (!isset($_GET['q']) || !is_string($_GET['q'])) {
158+
return;
159+
}
160+
161+
shuffle($a);
162+
163+
assertType('string', ini_get('x'));
164+
assertType('true', is_file($file));
165+
assertType('string', $_GET['q']);
166+
}
167+
168+
if ($case === 'str_shuffle') {
169+
if (ini_get('x') === false) {
170+
return;
171+
}
172+
if (!is_file($file)) {
173+
return;
174+
}
175+
if (!isset($_GET['q']) || !is_string($_GET['q'])) {
176+
return;
177+
}
178+
179+
str_shuffle('ab');
180+
181+
assertType('string', ini_get('x'));
182+
assertType('true', is_file($file));
183+
assertType('string', $_GET['q']);
184+
}
185+
186+
if ($case === 'array_rand') {
187+
if (ini_get('x') === false) {
188+
return;
189+
}
190+
if (!is_file($file)) {
191+
return;
192+
}
193+
if (!isset($_GET['q']) || !is_string($_GET['q'])) {
194+
return;
195+
}
196+
197+
array_rand($list);
198+
199+
assertType('string', ini_get('x'));
200+
assertType('true', is_file($file));
201+
assertType('string', $_GET['q']);
202+
}
203+
204+
if ($case === 'lcg_value') {
205+
if (ini_get('x') === false) {
206+
return;
207+
}
208+
if (!is_file($file)) {
209+
return;
210+
}
211+
if (!isset($_GET['q']) || !is_string($_GET['q'])) {
212+
return;
213+
}
214+
215+
lcg_value();
216+
217+
assertType('string', ini_get('x'));
218+
assertType('true', is_file($file));
219+
assertType('string', $_GET['q']);
220+
}
221+
}
222+
70223
public function randomResultsAreNotRemembered(): void
71224
{
72225
if (str_shuffle('ab') === 'ab') {

‎tests/PHPStan/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRuleTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,14 @@ public function testWarns(): void
272272
]);
273273
}
274274

275+
public function testSeededRandom(): void
276+
{
277+
$this->analyse([__DIR__ . '/data/function-call-seeded-random.php'], [
278+
[
279+
'Call to function lcg_value() on a separate line has no effect.',
280+
21,
281+
],
282+
]);
283+
}
284+
275285
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace FunctionCallSeededRandom;
4+
5+
/**
6+
* @param list<int> $a
7+
* @param non-empty-list<string> $list
8+
*/
9+
function doFoo(array $a, array $list): void
10+
{
11+
// they seed or advance the seeded random number generator
12+
mt_rand();
13+
rand();
14+
mt_srand(1);
15+
srand(1);
16+
shuffle($a);
17+
str_shuffle('ab');
18+
array_rand($list);
19+
20+
// it has a generator of its own that mt_srand() and srand() do not seed
21+
lcg_value();
22+
}

‎tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,4 +735,18 @@ public function testWeakPurityObjects(): void
735735
]);
736736
}
737737

738+
public function testRandom(): void
739+
{
740+
$this->analyse([__DIR__ . '/data/pure-random.php'], [
741+
[
742+
'Impure call to function mt_rand() in pure function PureRandom\\seededRandom().',
743+
8,
744+
],
745+
[
746+
'Impure call to function lcg_value() in pure function PureRandom\\unseededRandom().',
747+
14,
748+
],
749+
]);
750+
}
751+
738752
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PureRandom;
4+
5+
/** @phpstan-pure */
6+
function seededRandom(): int
7+
{
8+
return mt_rand();
9+
}
10+
11+
/** @phpstan-pure */
12+
function unseededRandom(): float
13+
{
14+
return lcg_value();
15+
}

0 commit comments

Comments
 (0)