|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Analyser; |
| 4 | + |
| 5 | +use PhpParser\Node\Scalar\Int_; |
| 6 | +use PhpParser\Node\Stmt; |
| 7 | +use function array_map; |
| 8 | + |
| 9 | +final class InternalStatementResult |
| 10 | +{ |
| 11 | + |
| 12 | + /** |
| 13 | + * @param StatementExitPoint[] $exitPoints |
| 14 | + * @param InternalThrowPoint[] $throwPoints |
| 15 | + * @param ImpurePoint[] $impurePoints |
| 16 | + * @param InternalEndStatementResult[] $endStatements |
| 17 | + */ |
| 18 | + public function __construct( |
| 19 | + private MutatingScope $scope, |
| 20 | + private bool $hasYield, |
| 21 | + private bool $isAlwaysTerminating, |
| 22 | + private array $exitPoints, |
| 23 | + private array $throwPoints, |
| 24 | + private array $impurePoints, |
| 25 | + private array $endStatements = [], |
| 26 | + ) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + public function toPublic(): StatementResult |
| 31 | + { |
| 32 | + return new StatementResult( |
| 33 | + $this->scope, |
| 34 | + $this->hasYield, |
| 35 | + $this->isAlwaysTerminating, |
| 36 | + $this->exitPoints, |
| 37 | + array_map(static fn ($throwPoint) => $throwPoint->toPublic(), $this->throwPoints), |
| 38 | + $this->impurePoints, |
| 39 | + array_map(static fn ($endStatement) => $endStatement->toPublic(), $this->endStatements), |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function getScope(): MutatingScope |
| 44 | + { |
| 45 | + return $this->scope; |
| 46 | + } |
| 47 | + |
| 48 | + public function hasYield(): bool |
| 49 | + { |
| 50 | + return $this->hasYield; |
| 51 | + } |
| 52 | + |
| 53 | + public function isAlwaysTerminating(): bool |
| 54 | + { |
| 55 | + return $this->isAlwaysTerminating; |
| 56 | + } |
| 57 | + |
| 58 | + public function filterOutLoopExitPoints(): self |
| 59 | + { |
| 60 | + if (!$this->isAlwaysTerminating) { |
| 61 | + return $this; |
| 62 | + } |
| 63 | + |
| 64 | + foreach ($this->exitPoints as $exitPoint) { |
| 65 | + $statement = $exitPoint->getStatement(); |
| 66 | + if (!$statement instanceof Stmt\Break_ && !$statement instanceof Stmt\Continue_) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + $num = $statement->num; |
| 71 | + if (!$num instanceof Int_) { |
| 72 | + return new self($this->scope, $this->hasYield, false, $this->exitPoints, $this->throwPoints, $this->impurePoints); |
| 73 | + } |
| 74 | + |
| 75 | + if ($num->value !== 1) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + return new self($this->scope, $this->hasYield, false, $this->exitPoints, $this->throwPoints, $this->impurePoints); |
| 80 | + } |
| 81 | + |
| 82 | + return $this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return StatementExitPoint[] |
| 87 | + */ |
| 88 | + public function getExitPoints(): array |
| 89 | + { |
| 90 | + return $this->exitPoints; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param class-string<Stmt\Continue_>|class-string<Stmt\Break_> $stmtClass |
| 95 | + * @return list<StatementExitPoint> |
| 96 | + */ |
| 97 | + public function getExitPointsByType(string $stmtClass): array |
| 98 | + { |
| 99 | + $exitPoints = []; |
| 100 | + foreach ($this->exitPoints as $exitPoint) { |
| 101 | + $statement = $exitPoint->getStatement(); |
| 102 | + if (!$statement instanceof $stmtClass) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + $value = $statement->num; |
| 107 | + if ($value === null) { |
| 108 | + $exitPoints[] = $exitPoint; |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + if (!$value instanceof Int_) { |
| 113 | + $exitPoints[] = $exitPoint; |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + $value = $value->value; |
| 118 | + if ($value !== 1) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + $exitPoints[] = $exitPoint; |
| 123 | + } |
| 124 | + |
| 125 | + return $exitPoints; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @return list<StatementExitPoint> |
| 130 | + */ |
| 131 | + public function getExitPointsForOuterLoop(): array |
| 132 | + { |
| 133 | + $exitPoints = []; |
| 134 | + foreach ($this->exitPoints as $exitPoint) { |
| 135 | + $statement = $exitPoint->getStatement(); |
| 136 | + if (!$statement instanceof Stmt\Continue_ && !$statement instanceof Stmt\Break_) { |
| 137 | + $exitPoints[] = $exitPoint; |
| 138 | + continue; |
| 139 | + } |
| 140 | + if ($statement->num === null) { |
| 141 | + continue; |
| 142 | + } |
| 143 | + if (!$statement->num instanceof Int_) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + $value = $statement->num->value; |
| 147 | + if ($value === 1) { |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + $newNode = null; |
| 152 | + if ($value > 2) { |
| 153 | + $newNode = new Int_($value - 1); |
| 154 | + } |
| 155 | + if ($statement instanceof Stmt\Continue_) { |
| 156 | + $newStatement = new Stmt\Continue_($newNode); |
| 157 | + } else { |
| 158 | + $newStatement = new Stmt\Break_($newNode); |
| 159 | + } |
| 160 | + |
| 161 | + $exitPoints[] = new StatementExitPoint($newStatement, $exitPoint->getScope()); |
| 162 | + } |
| 163 | + |
| 164 | + return $exitPoints; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @return InternalThrowPoint[] |
| 169 | + */ |
| 170 | + public function getThrowPoints(): array |
| 171 | + { |
| 172 | + return $this->throwPoints; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @return ImpurePoint[] |
| 177 | + */ |
| 178 | + public function getImpurePoints(): array |
| 179 | + { |
| 180 | + return $this->impurePoints; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Top-level StatementResult represents the state of the code |
| 185 | + * at the end of control flow statements like If_ or TryCatch. |
| 186 | + * |
| 187 | + * It shows how Scope etc. looks like after If_ no matter |
| 188 | + * which code branch was executed. |
| 189 | + * |
| 190 | + * For If_, "end statements" contain the state of the code |
| 191 | + * at the end of each branch - if, elseifs, else, including the last |
| 192 | + * statement node in each branch. |
| 193 | + * |
| 194 | + * For nested ifs, end statements try to contain the last non-control flow |
| 195 | + * statement like Return_ or Throw_, instead of If_, TryCatch, or Foreach_. |
| 196 | + * |
| 197 | + * @return InternalEndStatementResult[] |
| 198 | + */ |
| 199 | + public function getEndStatements(): array |
| 200 | + { |
| 201 | + return $this->endStatements; |
| 202 | + } |
| 203 | + |
| 204 | +} |
0 commit comments