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