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 last error of an extension, which its error getters read. Writing it
67: * is allowed in pure functions and makes a call on its own line useful.
68: */
69: public const WRITES_GLOBAL_ERROR = 'writes-global.error';
70: public const WRITES_GLOBAL_ERROR_JSON = 'writes-global.error.json';
71: public const READS_GLOBAL_ERROR_JSON = 'reads-global.error.json';
72: public const WRITES_GLOBAL_ERROR_PREG = 'writes-global.error.preg';
73: public const READS_GLOBAL_ERROR_PREG = 'reads-global.error.preg';
74: public const WRITES_GLOBAL_ERROR_OPENSSL = 'writes-global.error.openssl';
75: public const READS_GLOBAL_ERROR_OPENSSL = 'reads-global.error.openssl';
76: public const WRITES_GLOBAL_ERROR_INTL = 'writes-global.error.intl';
77: public const READS_GLOBAL_ERROR_INTL = 'reads-global.error.intl';
78:
79: /**
80: * @param list<string> $effects
81: * @param array<string, TrinaryLogic> $calledParameters
82: * @param array<string, TrinaryLogic> $writtenParameters
83: */
84: private function __construct(
85: private array $effects,
86: private TrinaryLogic $others,
87: private array $calledParameters,
88: private array $writtenParameters,
89: private bool $readsWrittenArguments,
90: )
91: {
92: }
93:
94: public static function none(): self
95: {
96: return new self([], TrinaryLogic::createNo(), [], [], false);
97: }
98:
99: public static function all(): self
100: {
101: return new self([], TrinaryLogic::createYes(), [], [], false);
102: }
103:
104: public static function unknown(): self
105: {
106: return new self([], TrinaryLogic::createMaybe(), [], [], false);
107: }
108:
109: /**
110: * $readsWrittenArguments says that a written parameter is passed by
111: * reference and also read, like array_shift()'s $array: the call changes
112: * what its next call returns.
113: *
114: * @param list<string> $effects
115: * @param array<string, TrinaryLogic> $calledParameters
116: * @param array<string, TrinaryLogic> $writtenParameters
117: */
118: public static function create(array $effects, TrinaryLogic $others, array $calledParameters = [], array $writtenParameters = [], bool $readsWrittenArguments = false): self
119: {
120: return new self($effects, $others, $calledParameters, $writtenParameters, $readsWrittenArguments);
121: }
122:
123: /** Maybe when only the called and written parameters can bring effects in. */
124: public function hasAny(): TrinaryLogic
125: {
126: if ($this->effects !== []) {
127: return TrinaryLogic::createYes();
128: }
129:
130: if ($this->others->no() && ($this->calledParameters !== [] || $this->writtenParameters !== [])) {
131: return TrinaryLogic::createMaybe();
132: }
133:
134: return $this->others;
135: }
136:
137: /**
138: * Whether the call has the effect, or one of its sub-effects: has('io') is
139: * Yes for io.files. A listed parent effect makes a sub-effect Maybe.
140: */
141: public function has(string $effect): TrinaryLogic
142: {
143: $result = $this->others;
144: foreach ($this->effects as $listed) {
145: $listed = self::baseName($listed);
146: if ($listed === $effect || str_starts_with($listed, $effect . '.')) {
147: return TrinaryLogic::createYes();
148: }
149: if (!str_starts_with($effect, $listed . '.')) {
150: continue;
151: }
152:
153: $result = $result->or(TrinaryLogic::createMaybe());
154: }
155:
156: return $result;
157: }
158:
159: /**
160: * Whether calling it again with the same arguments gives the same result.
161: * A result that depends on global state is deterministic until the state
162: * is written. A call that writes an argument it reads is not: the next call
163: * gets the written value. The callables passed to the called parameters do
164: * not count: a call only knows its own effects.
165: */
166: public function isDeterministic(): TrinaryLogic
167: {
168: if ($this->readsWrittenArguments) {
169: return TrinaryLogic::createNo();
170: }
171:
172: return $this->has(self::NONDETERMINISTIC)->negate();
173: }
174:
175: /**
176: * Whether every effect of the call is one of $effects or a sub-effect of
177: * one: hasOnly(['nondeterministic']) is Yes for nondeterministic.time. A
178: * listed parent of one of them, or unlisted effects that are only possible,
179: * make it Maybe. The written parameters count as writes-args. The callables
180: * passed to the called parameters do not count: a call only knows its own
181: * effects.
182: *
183: * @param list<string> $effects
184: */
185: public function hasOnly(array $effects): TrinaryLogic
186: {
187: if ($this->others->yes()) {
188: return TrinaryLogic::createNo();
189: }
190:
191: $result = $this->others->negate();
192: foreach ($this->effects as $listed) {
193: $listed = self::baseName($listed);
194: $listedResult = TrinaryLogic::createNo();
195: foreach ($effects as $effect) {
196: if ($listed === $effect || str_starts_with($listed, $effect . '.')) {
197: $listedResult = TrinaryLogic::createYes();
198: break;
199: }
200: if (!str_starts_with($effect, $listed . '.')) {
201: continue;
202: }
203:
204: $listedResult = TrinaryLogic::createMaybe();
205: }
206:
207: $result = $result->and($listedResult);
208: }
209:
210: if (!in_array(self::WRITES_ARGS, $effects, true)) {
211: foreach ($this->writtenParameters as $written) {
212: $result = $result->and($written->negate());
213: }
214: }
215:
216: return $result;
217: }
218:
219: /**
220: * The listed names, for messages, the reflection golden test, and telling
221: * a listed effect from one only possible through unlisted effects.
222: *
223: * @return list<string>
224: */
225: public function getEffectNames(): array
226: {
227: return $this->effects;
228: }
229:
230: /**
231: * The listed sub-effects of $effect without its prefix, such as
232: * ['stat-cache'] for writes-global.stat-cache: the named parts of the
233: * global state a call writes or reads. Null when $effect itself is listed,
234: * [] when neither is. Unlisted effects do not count.
235: *
236: * @return list<string>|null
237: */
238: public function getListedSubEffects(string $effect): ?array
239: {
240: $subEffects = [];
241: foreach ($this->effects as $listed) {
242: $listed = self::baseName($listed);
243: if ($listed === $effect) {
244: return null;
245: }
246: if (!str_starts_with($listed, $effect . '.')) {
247: continue;
248: }
249:
250: $subEffects[] = substr($listed, strlen($effect) + 1);
251: }
252:
253: return $subEffects;
254: }
255:
256: /**
257: * @return array<string, TrinaryLogic>
258: */
259: public function getCalledParameters(): array
260: {
261: return $this->calledParameters;
262: }
263:
264: /**
265: * @return array<string, TrinaryLogic>
266: */
267: public function getWrittenParameters(): array
268: {
269: return $this->writtenParameters;
270: }
271:
272: /**
273: * Whether the call writes to the argument passed to the parameter. A
274: * listed writes-args without parameters writes to every argument;
275: * writes-args($a, $b) writes only to those, which are written parameters.
276: */
277: public function writesParameter(string $parameterName): TrinaryLogic
278: {
279: if (isset($this->writtenParameters[$parameterName])) {
280: return $this->writtenParameters[$parameterName];
281: }
282:
283: return in_array(self::WRITES_ARGS, $this->effects, true) ? TrinaryLogic::createYes() : $this->others;
284: }
285:
286: /** The name of a listed effect without its parameters: writes-args for writes-args($a). */
287: private static function baseName(string $listed): string
288: {
289: $position = strpos($listed, '(');
290:
291: return $position === false ? $listed : substr($listed, 0, $position);
292: }
293:
294: }
295: