|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Analyser; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Stmt; |
| 8 | +use function array_pop; |
| 9 | +use function in_array; |
| 10 | +use function is_array; |
| 11 | +use function is_string; |
| 12 | + |
| 13 | +/** |
| 14 | + * The variables a loop can write while its scope converges. |
| 15 | + * |
| 16 | + * A variable the loop never writes enters every iteration with its value from |
| 17 | + * before the loop, so its type differs between convergence passes only through |
| 18 | + * narrowing by the loop's conditions. Widening such a variable loses its type |
| 19 | + * for nothing. |
| 20 | + */ |
| 21 | +final class LoopWrittenVariableNames |
| 22 | +{ |
| 23 | + |
| 24 | + private const SYNTACTIC_NAMES_ATTRIBUTE = 'phpstanLoopWrittenVariableNames'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Assignments, increments, destructuring, foreach bindings, catch, static, |
| 28 | + * global, unset and by-reference closure uses are found in the loop's AST. |
| 29 | + * Whether an argument is passed by reference is known only from the called |
| 30 | + * function's reflection, so those writes are read off the variable flow of |
| 31 | + * the convergence pass. |
| 32 | + * |
| 33 | + * @return array<string, true>|null null when the loop can write variables whose names are not known |
| 34 | + */ |
| 35 | + public static function collect(Node $loop, ?VariableFlow $passFlow): ?array |
| 36 | + { |
| 37 | + $names = self::getSyntacticNames($loop); |
| 38 | + if ($names === null) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + $flows = [$passFlow]; |
| 43 | + while ($flows !== []) { |
| 44 | + $flow = array_pop($flows); |
| 45 | + if ($flow instanceof VariableAccessFlow) { |
| 46 | + if (in_array($flow->kind, [VariableFlow::WRITE, VariableFlow::DEFINE, VariableFlow::DISCARD, VariableFlow::ESCAPE], true)) { |
| 47 | + $names[$flow->name] = true; |
| 48 | + } |
| 49 | + continue; |
| 50 | + } |
| 51 | + if ($flow instanceof VariableSequenceFlow) { |
| 52 | + foreach ($flow->children as $child) { |
| 53 | + $flows[] = $child; |
| 54 | + } |
| 55 | + continue; |
| 56 | + } |
| 57 | + if (!$flow instanceof VariableControlFlow) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + foreach ($flow->children as $child) { |
| 62 | + $flows[] = $child; |
| 63 | + } |
| 64 | + foreach ($flow->catches as [, $catchFlow]) { |
| 65 | + $flows[] = $catchFlow; |
| 66 | + } |
| 67 | + foreach ($flow->cases as [$caseCondition, $caseBody]) { |
| 68 | + $flows[] = $caseCondition; |
| 69 | + $flows[] = $caseBody; |
| 70 | + } |
| 71 | + foreach ($flow->bindings as $write) { |
| 72 | + $names[$write->getVariableName()] = true; |
| 73 | + } |
| 74 | + foreach ($flow->ownWrites as $write) { |
| 75 | + $names[$write->getVariableName()] = true; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return $names; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return array<string, true>|null |
| 84 | + */ |
| 85 | + private static function getSyntacticNames(Node $loop): ?array |
| 86 | + { |
| 87 | + $cached = $loop->getAttribute(self::SYNTACTIC_NAMES_ATTRIBUTE); |
| 88 | + if (is_array($cached)) { |
| 89 | + return $cached; |
| 90 | + } |
| 91 | + if ($cached === false) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + $names = self::findSyntacticNames($loop); |
| 96 | + $loop->setAttribute(self::SYNTACTIC_NAMES_ATTRIBUTE, $names ?? false); |
| 97 | + |
| 98 | + return $names; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return array<string, true>|null |
| 103 | + */ |
| 104 | + private static function findSyntacticNames(Node $loop): ?array |
| 105 | + { |
| 106 | + $names = []; |
| 107 | + $nodes = [$loop]; |
| 108 | + while ($nodes !== []) { |
| 109 | + $node = array_pop($nodes); |
| 110 | + if ($node instanceof Stmt\Function_ || $node instanceof Stmt\ClassLike) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + if ( |
| 114 | + ($node instanceof Expr\Variable && !is_string($node->name)) |
| 115 | + || $node instanceof Expr\Include_ |
| 116 | + || $node instanceof Expr\Eval_ |
| 117 | + || ( |
| 118 | + $node instanceof Expr\FuncCall |
| 119 | + && $node->name instanceof Node\Name |
| 120 | + && in_array($node->name->toLowerString(), ['extract', 'parse_str'], true) |
| 121 | + ) |
| 122 | + ) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + $targets = []; |
| 127 | + if ($node instanceof Expr\Assign || $node instanceof Expr\AssignOp) { |
| 128 | + $targets[] = $node->var; |
| 129 | + } elseif ($node instanceof Expr\AssignRef) { |
| 130 | + $targets[] = $node->var; |
| 131 | + $targets[] = $node->expr; |
| 132 | + } elseif ($node instanceof Expr\PreInc || $node instanceof Expr\PreDec || $node instanceof Expr\PostInc || $node instanceof Expr\PostDec) { |
| 133 | + $targets[] = $node->var; |
| 134 | + } elseif ($node instanceof Stmt\Foreach_) { |
| 135 | + if ($node->byRef) { |
| 136 | + $targets[] = $node->expr; |
| 137 | + } |
| 138 | + if ($node->keyVar !== null) { |
| 139 | + $targets[] = $node->keyVar; |
| 140 | + } |
| 141 | + $targets[] = $node->valueVar; |
| 142 | + } elseif ($node instanceof Stmt\Catch_) { |
| 143 | + if ($node->var !== null) { |
| 144 | + $targets[] = $node->var; |
| 145 | + } |
| 146 | + } elseif ($node instanceof Stmt\Static_) { |
| 147 | + foreach ($node->vars as $staticVar) { |
| 148 | + $targets[] = $staticVar->var; |
| 149 | + } |
| 150 | + } elseif ($node instanceof Stmt\Global_ || $node instanceof Stmt\Unset_) { |
| 151 | + foreach ($node->vars as $var) { |
| 152 | + $targets[] = $var; |
| 153 | + } |
| 154 | + } elseif ($node instanceof Expr\Closure) { |
| 155 | + foreach ($node->uses as $use) { |
| 156 | + if (!$use->byRef) { |
| 157 | + continue; |
| 158 | + } |
| 159 | + $targets[] = $use->var; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + foreach ($targets as $target) { |
| 164 | + $targetNames = self::getTargetNames($target); |
| 165 | + if ($targetNames === null) { |
| 166 | + return null; |
| 167 | + } |
| 168 | + foreach ($targetNames as $targetName) { |
| 169 | + $names[$targetName] = true; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + foreach ($node->getSubNodeNames() as $subNodeName) { |
| 174 | + $subNode = $node->$subNodeName; |
| 175 | + if ($subNode instanceof Node) { |
| 176 | + $nodes[] = $subNode; |
| 177 | + continue; |
| 178 | + } |
| 179 | + if (!is_array($subNode)) { |
| 180 | + continue; |
| 181 | + } |
| 182 | + foreach ($subNode as $subNodeItem) { |
| 183 | + if (!$subNodeItem instanceof Node) { |
| 184 | + continue; |
| 185 | + } |
| 186 | + $nodes[] = $subNodeItem; |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return $names; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * @return list<string>|null |
| 196 | + */ |
| 197 | + private static function getTargetNames(Expr $target): ?array |
| 198 | + { |
| 199 | + while ($target instanceof Expr\ArrayDimFetch || $target instanceof Expr\PropertyFetch || $target instanceof Expr\NullsafePropertyFetch) { |
| 200 | + $target = $target->var; |
| 201 | + } |
| 202 | + if ($target instanceof Expr\Variable) { |
| 203 | + return is_string($target->name) ? [$target->name] : null; |
| 204 | + } |
| 205 | + if (!$target instanceof Expr\List_ && !$target instanceof Expr\Array_) { |
| 206 | + return []; |
| 207 | + } |
| 208 | + |
| 209 | + $names = []; |
| 210 | + foreach ($target->items as $item) { |
| 211 | + if ($item === null) { |
| 212 | + continue; |
| 213 | + } |
| 214 | + $itemNames = self::getTargetNames($item->value); |
| 215 | + if ($itemNames === null) { |
| 216 | + return null; |
| 217 | + } |
| 218 | + foreach ($itemNames as $itemName) { |
| 219 | + $names[] = $itemName; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return $names; |
| 224 | + } |
| 225 | + |
| 226 | +} |
0 commit comments