| 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: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | #[ReferencedByTurboExtension(key: 'variableWritesNode')] |
| 26: | final class VariableWritesNode extends NodeAbstract implements VirtualNode |
| 27: | { |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | public function __construct( |
| 42: | private Node\FunctionLike $functionLike, |
| 43: | private array $writes, |
| 44: | private array $readWriteIds, |
| 45: | private array $usedWriteIds, |
| 46: | private array $coveredWriteIds, |
| 47: | private array $overwrittenWriteIds, |
| 48: | private array $readVariableNames, |
| 49: | private array $redundantWriteTypes, |
| 50: | private array $referencedVariableNames, |
| 51: | private array $untrackedVariableNames, |
| 52: | private array $variableOverwritingLoops, |
| 53: | private bool $opaque, |
| 54: | private bool $allVariableNamesReferenced, |
| 55: | ) |
| 56: | { |
| 57: | parent::__construct($functionLike->getAttributes()); |
| 58: | } |
| 59: | |
| 60: | public function getFunctionLike(): Node\FunctionLike |
| 61: | { |
| 62: | return $this->functionLike; |
| 63: | } |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | public function getWrites(): array |
| 69: | { |
| 70: | return $this->writes; |
| 71: | } |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | public function getWriteForNode(Node\Expr\Variable $variable): ?VariableWrite |
| 78: | { |
| 79: | foreach ($this->writes as $write) { |
| 80: | if ($write->getNode() === $variable) { |
| 81: | return $write; |
| 82: | } |
| 83: | } |
| 84: | |
| 85: | return null; |
| 86: | } |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public function areAllVariableNamesReferenced(): bool |
| 93: | { |
| 94: | return $this->allVariableNamesReferenced; |
| 95: | } |
| 96: | |
| 97: | |
| 98: | public function isUsed(VariableWrite $write): bool |
| 99: | { |
| 100: | return isset($this->usedWriteIds[$write->getId()]); |
| 101: | } |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | public function flowsIntoNeverReadWrite(VariableWrite $write): bool |
| 108: | { |
| 109: | return isset($this->coveredWriteIds[$write->getId()]); |
| 110: | } |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | public function isOverwritten(VariableWrite $write): bool |
| 118: | { |
| 119: | return isset($this->overwrittenWriteIds[$write->getId()]); |
| 120: | } |
| 121: | |
| 122: | |
| 123: | public function isRead(VariableWrite $write): bool |
| 124: | { |
| 125: | return isset($this->readWriteIds[$write->getId()]); |
| 126: | } |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | public function isVariableEverRead(string $variableName): bool |
| 133: | { |
| 134: | return isset($this->readVariableNames[$variableName]); |
| 135: | } |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | public function getRedundantType(VariableWrite $write): ?Type |
| 142: | { |
| 143: | return $this->redundantWriteTypes[$write->getId()] ?? null; |
| 144: | } |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | public function getVariableOverwritingLoop(VariableWrite $write): ?Node\Stmt |
| 157: | { |
| 158: | return $this->variableOverwritingLoops[$write->getId()] ?? null; |
| 159: | } |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | public function isVariableReferenced(string $variableName): bool |
| 168: | { |
| 169: | return $this->allVariableNamesReferenced |
| 170: | || $this->opaque |
| 171: | || isset($this->referencedVariableNames[$variableName]); |
| 172: | } |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | public function isUntracked(string $variableName): bool |
| 179: | { |
| 180: | return isset($this->untrackedVariableNames[$variableName]); |
| 181: | } |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | public function isOpaque(): bool |
| 187: | { |
| 188: | return $this->opaque; |
| 189: | } |
| 190: | |
| 191: | #[Override] |
| 192: | public function getType(): string |
| 193: | { |
| 194: | return 'PHPStan_Node_VariableWritesNode'; |
| 195: | } |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | #[Override] |
| 201: | public function getSubNodeNames(): array |
| 202: | { |
| 203: | return []; |
| 204: | } |
| 205: | |
| 206: | } |
| 207: | |