1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Node;
4:
5: use Override;
6: use PhpParser\Node;
7: use PhpParser\Node\Stmt\For_;
8: use PhpParser\Node\Stmt\Foreach_;
9: use PhpParser\NodeAbstract;
10: use PHPStan\Node\Variable\VariableWrite;
11: use PHPStan\Turbo\ReferencedByTurboExtension;
12: use PHPStan\Type\Type;
13:
14: /**
15: * All local-variable write sites of a function-like body, with the set of
16: * those whose written value was read on some path afterwards, and the set of
17: * variable names the body mentions at all.
18: *
19: * Emitted right after the body's ReturnStatementsNode, with the scope inside
20: * the function-like. Arrow functions have no node of their own - their writes
21: * belong to the enclosing function-like.
22: *
23: * @api
24: */
25: #[ReferencedByTurboExtension(key: 'variableWritesNode')]
26: final class VariableWritesNode extends NodeAbstract implements VirtualNode
27: {
28:
29: /**
30: * @param list<VariableWrite> $writes
31: * @param array<int, true> $readWriteIds
32: * @param array<int, true> $usedWriteIds
33: * @param array<int, true> $coveredWriteIds
34: * @param array<int, true> $overwrittenWriteIds
35: * @param array<string, true> $readVariableNames
36: * @param array<int, Type> $redundantWriteTypes
37: * @param array<string, true> $referencedVariableNames
38: * @param array<string, true> $untrackedVariableNames
39: * @param array<int, Foreach_|For_> $variableOverwritingLoops
40: * @param array<string, int> $observingAccessCounts
41: */
42: public function __construct(
43: private Node\FunctionLike $functionLike,
44: private array $writes,
45: private array $readWriteIds,
46: private array $usedWriteIds,
47: private array $coveredWriteIds,
48: private array $overwrittenWriteIds,
49: private array $readVariableNames,
50: private array $redundantWriteTypes,
51: private array $referencedVariableNames,
52: private array $untrackedVariableNames,
53: private array $variableOverwritingLoops,
54: private bool $opaque,
55: private bool $allVariableNamesReferenced,
56: private array $observingAccessCounts = [],
57: )
58: {
59: parent::__construct($functionLike->getAttributes());
60: }
61:
62: public function getFunctionLike(): Node\FunctionLike
63: {
64: return $this->functionLike;
65: }
66:
67: /**
68: * @return list<VariableWrite>
69: */
70: public function getWrites(): array
71: {
72: return $this->writes;
73: }
74:
75: /**
76: * The write whose target is this exact node (a parameter's or closure
77: * use's variable), if it is tracked.
78: */
79: public function getWriteForNode(Node\Expr\Variable $variable): ?VariableWrite
80: {
81: foreach ($this->writes as $write) {
82: if ($write->getNode() === $variable) {
83: return $write;
84: }
85: }
86:
87: return null;
88: }
89:
90: /**
91: * Whether a construct that can observe every variable by name without
92: * reading its current value (func_get_args()) appears in the body.
93: */
94: public function areAllVariableNamesReferenced(): bool
95: {
96: return $this->allVariableNamesReferenced;
97: }
98:
99: /** Whether the value reaches an observable use, directly or through another write. */
100: public function isUsed(VariableWrite $write): bool
101: {
102: return isset($this->usedWriteIds[$write->getId()]);
103: }
104:
105: /**
106: * Whether the value flows into a write that is never read at all - that
107: * write is the one to report, this one only feeds it.
108: */
109: public function flowsIntoNeverReadWrite(VariableWrite $write): bool
110: {
111: return isset($this->coveredWriteIds[$write->getId()]);
112: }
113:
114: /**
115: * Whether some path from the write reaches another write replacing the
116: * variable (or the written offset) - for a write that is never read, the
117: * value is replaced before anything looks at it.
118: */
119: public function isOverwritten(VariableWrite $write): bool
120: {
121: return isset($this->overwrittenWriteIds[$write->getId()]);
122: }
123:
124: /** Whether some path from the write reaches a read of the written value. */
125: public function isRead(VariableWrite $write): bool
126: {
127: return isset($this->readWriteIds[$write->getId()]);
128: }
129:
130: /**
131: * Whether the variable name appears at a read site anywhere in the body,
132: * regardless of which writes the read observed.
133: */
134: public function isVariableEverRead(string $variableName): bool
135: {
136: return isset($this->readVariableNames[$variableName]);
137: }
138:
139: /**
140: * How many sites in the body read the variable or let it escape (pass it
141: * by reference, bind a reference to it, capture it by reference, declare
142: * it global or static).
143: */
144: public function getObservingAccessCount(string $variableName): int
145: {
146: return $this->observingAccessCounts[$variableName] ?? 0;
147: }
148:
149: /**
150: * The type of the assigned value when the write assigns the value the
151: * variable provably already has, null otherwise.
152: */
153: public function getRedundantType(VariableWrite $write): ?Type
154: {
155: return $this->redundantWriteTypes[$write->getId()] ?? null;
156: }
157:
158: /**
159: * The loop statement that binds this write in its head - a foreach key
160: * or value variable, a for-loop initial assignment - when the variable
161: * was assigned before the loop and is read after it with no assignment
162: * in between other than the loop's own bindings and updates: the loop
163: * takes over a variable still in use, rather than a spent loop variable.
164: * Null for every other write.
165: *
166: * @return Foreach_|For_|null
167: */
168: public function getVariableOverwritingLoop(VariableWrite $write): ?Node\Stmt
169: {
170: return $this->variableOverwritingLoops[$write->getId()] ?? null;
171: }
172:
173: /**
174: * Whether the body mentions the variable at all: a read, a write, a
175: * statement naming it (global, static, a reference alias), or a construct
176: * that can observe every variable (eval, include, a dynamic compact() or
177: * $$name, func_get_args()).
178: */
179: public function isVariableReferenced(string $variableName): bool
180: {
181: return $this->allVariableNamesReferenced
182: || $this->opaque
183: || isset($this->referencedVariableNames[$variableName]);
184: }
185:
186: /**
187: * Variables whose writes escape the body (by-ref parameters and uses,
188: * global/static variables, reference aliases) - every write counts as used.
189: */
190: public function isUntracked(string $variableName): bool
191: {
192: return isset($this->untrackedVariableNames[$variableName]);
193: }
194:
195: /**
196: * The body contains a construct (goto) that defeats reaching-write tracking.
197: */
198: public function isOpaque(): bool
199: {
200: return $this->opaque;
201: }
202:
203: #[Override]
204: public function getType(): string
205: {
206: return 'PHPStan_Node_VariableWritesNode';
207: }
208:
209: /**
210: * @return string[]
211: */
212: #[Override]
213: public function getSubNodeNames(): array
214: {
215: return [];
216: }
217:
218: }
219: