Repository navigation
Expand file tree
/
Copy pathVariableFlow.php
More file actions
191 lines (163 loc) · 5.89 KB
/
Copy pathVariableFlow.php
File metadata and controls
191 lines (163 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PHPStan\Node\Variable\VariableWrite;
use PHPStan\Type\Type;
use function count;
use function in_array;
/**
* Immutable source execution fragment, composed with expression and statement
* results. Liveness is resolved at the body boundary, independently of types.
*/
abstract class VariableFlow
{
public const SEQUENCE = 'sequence';
public const CHOICE = 'choice';
public const LOOP = 'loop';
public const TRY_CATCH = 'try';
public const SWITCH = 'switch';
public const READ = 'read';
public const WRITE = 'write';
public const DEFINE = 'define';
public const DISCARD = 'discard';
public const ESCAPE = 'escape';
public const MENTION = 'mention';
public const READ_ALL = 'readAll';
public const MENTION_ALL = 'mentionAll';
public const OPAQUE = 'opaque';
public const DEAD = 'dead';
public const RETURN = 'return';
public const BREAK = 'break';
public const CONTINUE = 'continue';
public const THROW = 'throw';
public const STOP = 'stop';
public const ARROW = 'arrow';
public const LOOP_STATEMENT = 'loopStatement';
/** @param self::* $kind */
protected function __construct(public readonly string $kind)
{
}
public static function sequence(?self ...$flows): ?self
{
$nonEmpty = [];
foreach ($flows as $flow) {
if ($flow === null) {
continue;
}
$nonEmpty[] = $flow;
}
if (count($nonEmpty) === 0) {
return null;
}
if (count($nonEmpty) === 1) {
return $nonEmpty[0];
}
return new VariableSequenceFlow(self::SEQUENCE, $nonEmpty);
}
/** @no-named-arguments */
public static function choice(?self ...$branches): ?self
{
if (count($branches) === 0) {
return null;
}
if (count($branches) === 1 || (count($branches) === 2 && $branches[0] === $branches[1])) {
return $branches[0];
}
return new VariableSequenceFlow(self::CHOICE, $branches);
}
public static function arrow(ArrowFunction $arrow, ?self $body, ?self $outputs): self
{
return new VariableControlFlow(self::ARROW, [$body, $outputs], arrow: $arrow);
}
/** @param int|string|null $offset */
public static function read(string $name, ?int $targetId = null, bool $container = false, $offset = null): ?self
{
if ($name === 'this' || in_array($name, Scope::SUPERGLOBAL_VARIABLES, true)) {
return null;
}
return new VariableAccessFlow(self::READ, $name, targetId: $targetId, container: $container, offset: $offset);
}
public static function conditional(?self $condition, ?self $if, ?self $else, ?bool $truthy): ?self
{
if ($truthy === true) {
$branch = self::sequence($if, self::dead($else));
} elseif ($truthy === false) {
$branch = self::sequence(self::dead($if), $else);
} else {
$branch = self::choice($if, $else);
}
return self::sequence($condition, $branch);
}
/** @param list<array{self|null, self|null, bool}> $cases */
public static function switch(?self $condition, array $cases, bool $exhaustive): self
{
return new VariableControlFlow(self::SWITCH, [$condition], canExit: !$exhaustive, cases: $cases);
}
public static function write(VariableWrite $write, ?Type $redundantType = null): self
{
return new VariableAccessFlow($write->getParentId() === null ? self::WRITE : self::DEFINE, $write->getVariableName(), $write, $redundantType);
}
public static function discard(VariableWrite $write): self
{
return new VariableAccessFlow(self::DISCARD, $write->getVariableName(), $write);
}
/** The enclosing expression consumes a write's inputs, without reading its target. */
public static function inputs(int $writeId, ?int $targetId): self
{
return new VariableInputFlow($writeId, $targetId);
}
public static function escape(string $name): self
{
return new VariableAccessFlow(self::ESCAPE, $name);
}
public static function mention(string $name): self
{
return new VariableAccessFlow(self::MENTION, $name);
}
/** @param self::READ_ALL|self::MENTION_ALL|self::OPAQUE $kind */
public static function all(string $kind): self
{
return new VariableControlFlow($kind);
}
/** @param self::RETURN|self::BREAK|self::CONTINUE|self::STOP $kind */
public static function exit(string $kind, int $level = 1, ?string $name = null): self
{
return new VariableControlFlow($kind, name: $name, level: $level);
}
public static function throwing(Type $type, bool $canContinue, bool $canContainAnyThrowable = false): self
{
return new VariableControlFlow(self::THROW, type: $type, canExit: $canContinue, canContainAnyThrowable: $canContainAnyThrowable);
}
public static function dead(?self $flow): ?self
{
return $flow === null ? null : new VariableControlFlow(self::DEAD, [$flow]);
}
public static function loop(?self $condition, ?self $body, ?self $update, bool $atLeastOnce, bool $canExit, bool $canRepeat = true): self
{
return new VariableControlFlow(self::LOOP, [$condition, $body, $update], atLeastOnce: $atLeastOnce, canExit: $canExit, canRepeat: $canRepeat);
}
/**
* A loop statement whose head binds variables - a foreach key/value or
* a for-loop initial assignment. The bindings are checked for reusing a
* variable that is assigned before the statement and read after it;
* the statement's own writes (the bindings and a for-loop update) are
* the only assignments allowed in between.
*
* @param list<VariableWrite> $bindings
* @param list<VariableWrite> $ownWrites
*/
public static function loopStatement(Foreach_|For_ $stmt, ?self $flow, array $bindings, array $ownWrites): ?self
{
if ($bindings === []) {
return $flow;
}
return new VariableControlFlow(self::LOOP_STATEMENT, [$flow], stmt: $stmt, bindings: $bindings, ownWrites: $ownWrites);
}
/** @param list<array{Type, self|null}> $catches */
public static function tryCatch(?self $body, array $catches, ?self $finally): self
{
return new VariableControlFlow(self::TRY_CATCH, [$body, $finally], catches: $catches);
}
}