1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Reflection;
4:
5: use PHPStan\TrinaryLogic;
6: use PHPStan\Turbo\ShadowedByTurboExtension;
7: use function in_array;
8: use function str_starts_with;
9: use function strlen;
10: use function strpos;
11: use function substr;
12:
13: /**
14: * The side effects of a function or method, as returned by getSideEffects().
15: *
16: * A function has the named effects, such as io.output, and possibly any other
17: * effect as much as $others says: No when the list is known to be complete,
18: * Maybe when nothing is known, Yes when it has some effects that are not
19: * named.
20: *
21: * The called parameters are those flagged as pure unless the callable is
22: * impure: the call has the side effects of the callable passed there. The
23: * written parameters are those flagged as pure unless the parameter is passed,
24: * and those named by a listed writes-args($a, $b): the call writes to the
25: * argument passed there.
26: *
27: * @api
28: */
29: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/SideEffects.cpp')]
30: final class SideEffects
31: {
32:
33: public const WRITES_THIS = 'writes-this';
34: public const WRITES_ARGS = 'writes-args';
35: public const WRITES_GLOBAL = 'writes-global';
36: public const READS_GLOBAL = 'reads-global';
37: public const NONDETERMINISTIC = 'nondeterministic';
38: public const NONDETERMINISTIC_TIME = 'nondeterministic.time';
39: public const NONDETERMINISTIC_RANDOM = 'nondeterministic.random';
40: public const IO = 'io';
41: public const IO_OUTPUT = 'io.output';
42: public const IO_FILES = 'io.files';
43: public const IO_NETWORK = 'io.network';
44: public const IO_DB = 'io.db';
45: public const IO_PROCESS = 'io.process';
46: public const SLEEP = 'sleep';
47: public const EXIT = 'exit';
48: public const WARNS = 'warns';
49:
50: /**
51: * Named parts of global state. A call that writes one forgets only the
52: * remembered calls that read it, such as is_file($a) after clearstatcache().
53: */
54: public const WRITES_GLOBAL_STAT_CACHE = 'writes-global.stat-cache';
55: public const READS_GLOBAL_STAT_CACHE = 'reads-global.stat-cache';
56:
57: /**
58: * The settings of the mbstring extension, such as the internal encoding the
59: * mb_*() functions fall back to without an $encoding argument, and the
60: * position of mb_ereg_search().
61: */
62: public const WRITES_GLOBAL_MBSTRING = 'writes-global.mbstring';
63: public const READS_GLOBAL_MBSTRING = 'reads-global.mbstring';
64:
65: /**
66: * The state of the session, its status and id, which session_start() and
67: * the other session lifecycle calls change.
68: */
69: public const WRITES_GLOBAL_SESSION = 'writes-global.session';
70: public const READS_GLOBAL_SESSION = 'reads-global.session';
71:
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:
81: /**
82: * The last error of an extension, which its error getters read. Writing it
83: * is allowed in pure functions and makes a call on its own line useful.
84: */
85: public const WRITES_GLOBAL_ERROR = 'writes-global.error';
86: public const WRITES_GLOBAL_ERROR_JSON = 'writes-global.error.json';
87: public const READS_GLOBAL_ERROR_JSON = 'reads-global.error.json';
88: public const WRITES_GLOBAL_ERROR_PREG = 'writes-global.error.preg';
89: public const READS_GLOBAL_ERROR_PREG = 'reads-global.error.preg';
90: public const WRITES_GLOBAL_ERROR_OPENSSL = 'writes-global.error.openssl';
91: public const READS_GLOBAL_ERROR_OPENSSL = 'reads-global.error.openssl';
92: public const WRITES_GLOBAL_ERROR_INTL = 'writes-global.error.intl';
93: public const READS_GLOBAL_ERROR_INTL = 'reads-global.error.intl';
94:
95: /**
96: * @param list<string> $effects
97: * @param array<string, TrinaryLogic> $calledParameters
98: * @param array<string, TrinaryLogic> $writtenParameters
99: */
100: private function __construct(
101: private array $effects,
102: private TrinaryLogic $others,
103: private array $calledParameters,
104: private array $writtenParameters,
105: private bool $readsWrittenArguments,
106: )
107: {
108: }
109:
110: public static function none(): self
111: {
112: return new self([], TrinaryLogic::createNo(), [], [], false);
113: }
114:
115: public static function all(): self
116: {
117: return new self([], TrinaryLogic::createYes(), [], [], false);
118: }
119:
120: public static function unknown(): self
121: {
122: return new self([], TrinaryLogic::createMaybe(), [], [], false);
123: }
124:
125: /**
126: * $readsWrittenArguments says that a written parameter is passed by
127: * reference and also read, like array_shift()'s $array: the call changes
128: * what its next call returns.
129: *
130: * @param list<string> $effects
131: * @param array<string, TrinaryLogic> $calledParameters
132: * @param array<string, TrinaryLogic> $writtenParameters
133: */
134: public static function create(array $effects, TrinaryLogic $others, array $calledParameters = [], array $writtenParameters = [], bool $readsWrittenArguments = false): self
135: {
136: return new self($effects, $others, $calledParameters, $writtenParameters, $readsWrittenArguments);
137: }
138:
139: /** Maybe when only the called and written parameters can bring effects in. */
140: public function hasAny(): TrinaryLogic
141: {
142: if ($this->effects !== []) {
143: return TrinaryLogic::createYes();
144: }
145:
146: if ($this->others->no() && ($this->calledParameters !== [] || $this->writtenParameters !== [])) {
147: return TrinaryLogic::createMaybe();
148: }
149:
150: return $this->others;
151: }
152:
153: /**
154: * Whether the call has the effect, or one of its sub-effects: has('io') is
155: * Yes for io.files. A listed parent effect makes a sub-effect Maybe.
156: */
157: public function has(string $effect): TrinaryLogic
158: {
159: $result = $this->others;
160: foreach ($this->effects as $listed) {
161: $listed = self::baseName($listed);
162: if ($listed === $effect || str_starts_with($listed, $effect . '.')) {
163: return TrinaryLogic::createYes();
164: }
165: if (!str_starts_with($effect, $listed . '.')) {
166: continue;
167: }
168:
169: $result = $result->or(TrinaryLogic::createMaybe());
170: }
171:
172: return $result;
173: }
174:
175: /**
176: * Whether calling it again with the same arguments gives the same result.
177: * A result that depends on global state is deterministic until the state
178: * is written. A call that writes an argument it reads is not: the next call
179: * gets the written value. The callables passed to the called parameters do
180: * not count: a call only knows its own effects.
181: */
182: public function isDeterministic(): TrinaryLogic
183: {
184: if ($this->readsWrittenArguments) {
185: return TrinaryLogic::createNo();
186: }
187:
188: return $this->has(self::NONDETERMINISTIC)->negate();
189: }
190:
191: /**
192: * Whether every effect of the call is one of $effects or a sub-effect of
193: * one: hasOnly(['nondeterministic']) is Yes for nondeterministic.time. A
194: * listed parent of one of them, or unlisted effects that are only possible,
195: * make it Maybe. The written parameters count as writes-args. The callables
196: * passed to the called parameters do not count: a call only knows its own
197: * effects.
198: *
199: * @param list<string> $effects
200: */
201: public function hasOnly(array $effects): TrinaryLogic
202: {
203: if ($this->others->yes()) {
204: return TrinaryLogic::createNo();
205: }
206:
207: $result = $this->others->negate();
208: foreach ($this->effects as $listed) {
209: $listed = self::baseName($listed);
210: $listedResult = TrinaryLogic::createNo();
211: foreach ($effects as $effect) {
212: if ($listed === $effect || str_starts_with($listed, $effect . '.')) {
213: $listedResult = TrinaryLogic::createYes();
214: break;
215: }
216: if (!str_starts_with($effect, $listed . '.')) {
217: continue;
218: }
219:
220: $listedResult = TrinaryLogic::createMaybe();
221: }
222:
223: $result = $result->and($listedResult);
224: }
225:
226: if (!in_array(self::WRITES_ARGS, $effects, true)) {
227: foreach ($this->writtenParameters as $written) {
228: $result = $result->and($written->negate());
229: }
230: }
231:
232: return $result;
233: }
234:
235: /**
236: * The listed names, for messages, the reflection golden test, and telling
237: * a listed effect from one only possible through unlisted effects.
238: *
239: * @return list<string>
240: */
241: public function getEffectNames(): array
242: {
243: return $this->effects;
244: }
245:
246: /**
247: * The listed sub-effects of $effect without its prefix, such as
248: * ['stat-cache'] for writes-global.stat-cache: the named parts of the
249: * global state a call writes or reads. Null when $effect itself is listed,
250: * [] when neither is. Unlisted effects do not count.
251: *
252: * @return list<string>|null
253: */
254: public function getListedSubEffects(string $effect): ?array
255: {
256: $subEffects = [];
257: foreach ($this->effects as $listed) {
258: $listed = self::baseName($listed);
259: if ($listed === $effect) {
260: return null;
261: }
262: if (!str_starts_with($listed, $effect . '.')) {
263: continue;
264: }
265:
266: $subEffects[] = substr($listed, strlen($effect) + 1);
267: }
268:
269: return $subEffects;
270: }
271:
272: /**
273: * @return array<string, TrinaryLogic>
274: */
275: public function getCalledParameters(): array
276: {
277: return $this->calledParameters;
278: }
279:
280: /**
281: * @return array<string, TrinaryLogic>
282: */
283: public function getWrittenParameters(): array
284: {
285: return $this->writtenParameters;
286: }
287:
288: /**
289: * Whether the call writes to the argument passed to the parameter. A
290: * listed writes-args without parameters writes to every argument;
291: * writes-args($a, $b) writes only to those, which are written parameters.
292: */
293: public function writesParameter(string $parameterName): TrinaryLogic
294: {
295: if (isset($this->writtenParameters[$parameterName])) {
296: return $this->writtenParameters[$parameterName];
297: }
298:
299: return in_array(self::WRITES_ARGS, $this->effects, true) ? TrinaryLogic::createYes() : $this->others;
300: }
301:
302: /** The name of a listed effect without its parameters: writes-args for writes-args($a). */
303: private static function baseName(string $listed): string
304: {
305: $position = strpos($listed, '(');
306:
307: return $position === false ? $listed : substr($listed, 0, $position);
308: }
309:
310: }
311: