| Total Complexity | 67 |
| Total Lines | 368 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DelimiterStack often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DelimiterStack, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | final class DelimiterStack |
||
| 28 | { |
||
| 29 | /** @psalm-readonly-allow-private-mutation */ |
||
| 30 | private ?DelimiterInterface $top = null; |
||
| 31 | |||
| 32 | /** @psalm-readonly-allow-private-mutation */ |
||
| 33 | private ?Bracket $brackets = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @deprecated This property will be removed in 3.0 once all delimiters MUST have an index/position |
||
| 37 | * |
||
| 38 | * @var \SplObjectStorage<DelimiterInterface, int>|\WeakMap<DelimiterInterface, int> |
||
| 39 | */ |
||
| 40 | private $missingIndexCache; |
||
| 41 | |||
| 42 | |||
| 43 | private int $remainingDelimiters = 0; |
||
| 44 | |||
| 45 | public function __construct(int $maximumStackSize = PHP_INT_MAX) |
||
| 46 | { |
||
| 47 | $this->remainingDelimiters = $maximumStackSize; |
||
| 48 | |||
| 49 | if (\PHP_VERSION_ID >= 80000) { |
||
| 50 | /** @psalm-suppress PropertyTypeCoercion */ |
||
| 51 | $this->missingIndexCache = new \WeakMap(); // @phpstan-ignore-line |
||
| 52 | } else { |
||
| 53 | $this->missingIndexCache = new \SplObjectStorage(); // @phpstan-ignore-line |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | public function push(DelimiterInterface $newDelimiter): void |
||
| 58 | { |
||
| 59 | if ($this->remainingDelimiters-- <= 0) { |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | $newDelimiter->setPrevious($this->top); |
||
| 64 | |||
| 65 | if ($this->top !== null) { |
||
| 66 | $this->top->setNext($newDelimiter); |
||
| 67 | } |
||
| 68 | |||
| 69 | $this->top = $newDelimiter; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @internal |
||
| 74 | */ |
||
| 75 | public function addBracket(Node $node, int $index, bool $image): void |
||
| 76 | { |
||
| 77 | if ($this->brackets !== null) { |
||
| 78 | $this->brackets->setHasNext(true); |
||
| 79 | } |
||
| 80 | |||
| 81 | $this->brackets = new Bracket($node, $this->brackets, $index, $image); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @psalm-immutable |
||
| 86 | */ |
||
| 87 | public function getLastBracket(): ?Bracket |
||
| 88 | { |
||
| 89 | return $this->brackets; |
||
| 90 | } |
||
| 91 | |||
| 92 | private function findEarliest(int $stackBottom): ?DelimiterInterface |
||
| 93 | { |
||
| 94 | // Move back to first relevant delim. |
||
| 95 | $delimiter = $this->top; |
||
| 96 | $lastChecked = null; |
||
| 97 | |||
| 98 | while ($delimiter !== null && self::getIndex($delimiter) > $stackBottom) { |
||
| 99 | $lastChecked = $delimiter; |
||
| 100 | $delimiter = $delimiter->getPrevious(); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $lastChecked; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @internal |
||
| 108 | */ |
||
| 109 | public function removeBracket(): void |
||
| 110 | { |
||
| 111 | if ($this->brackets === null) { |
||
| 112 | return; |
||
| 113 | } |
||
| 114 | |||
| 115 | $this->brackets = $this->brackets->getPrevious(); |
||
| 116 | |||
| 117 | if ($this->brackets !== null) { |
||
| 118 | $this->brackets->setHasNext(false); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | public function removeDelimiter(DelimiterInterface $delimiter): void |
||
| 123 | { |
||
| 124 | if ($delimiter->getPrevious() !== null) { |
||
| 125 | /** @psalm-suppress PossiblyNullReference */ |
||
| 126 | $delimiter->getPrevious()->setNext($delimiter->getNext()); |
||
| 127 | } |
||
| 128 | |||
| 129 | if ($delimiter->getNext() === null) { |
||
| 130 | // top of stack |
||
| 131 | $this->top = $delimiter->getPrevious(); |
||
| 132 | } else { |
||
| 133 | /** @psalm-suppress PossiblyNullReference */ |
||
| 134 | $delimiter->getNext()->setPrevious($delimiter->getPrevious()); |
||
| 135 | } |
||
| 136 | |||
| 137 | // Nullify all references from the removed delimiter to other delimiters. |
||
| 138 | // All references to this particular delimiter in the linked list should be gone, |
||
| 139 | // but it's possible we're still hanging on to other references to things that |
||
| 140 | // have been (or soon will be) removed, which may interfere with efficient |
||
| 141 | // garbage collection by the PHP runtime. |
||
| 142 | // Explicitly releasing these references should help to avoid possible |
||
| 143 | // segfaults like in https://bugs.php.net/bug.php?id=68606. |
||
| 144 | $delimiter->setPrevious(null); |
||
| 145 | $delimiter->setNext(null); |
||
| 146 | |||
| 147 | // TODO: Remove the line below once PHP 7.4 support is dropped, as WeakMap won't hold onto the reference, making this unnecessary |
||
| 148 | unset($this->missingIndexCache[$delimiter]); |
||
| 149 | } |
||
| 150 | |||
| 151 | private function removeDelimiterAndNode(DelimiterInterface $delimiter): void |
||
| 152 | { |
||
| 153 | $delimiter->getInlineNode()->detach(); |
||
| 154 | $this->removeDelimiter($delimiter); |
||
| 155 | } |
||
| 156 | |||
| 157 | private function removeDelimitersBetween(DelimiterInterface $opener, DelimiterInterface $closer): void |
||
| 158 | { |
||
| 159 | $delimiter = $closer->getPrevious(); |
||
| 160 | $openerPosition = self::getIndex($opener); |
||
| 161 | while ($delimiter !== null && self::getIndex($delimiter) > $openerPosition) { |
||
| 162 | $previous = $delimiter->getPrevious(); |
||
| 163 | $this->removeDelimiter($delimiter); |
||
| 164 | $delimiter = $previous; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param DelimiterInterface|int|null $stackBottom |
||
| 170 | */ |
||
| 171 | public function removeAll($stackBottom = null): void |
||
| 172 | { |
||
| 173 | $stackBottomPosition = \is_int($stackBottom) ? $stackBottom : self::getIndex($stackBottom); |
||
| 174 | |||
| 175 | while ($this->top && $this->getIndex($this->top) > $stackBottomPosition) { |
||
| 176 | $this->removeDelimiter($this->top); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @deprecated This method is no longer used internally and will be removed in 3.0 |
||
| 182 | */ |
||
| 183 | public function removeEarlierMatches(string $character): void |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @internal |
||
| 197 | */ |
||
| 198 | public function deactivateLinkOpeners(): void |
||
| 199 | { |
||
| 200 | $opener = $this->brackets; |
||
| 201 | while ($opener !== null && $opener->isActive()) { |
||
| 202 | $opener->setActive(false); |
||
| 203 | $opener = $opener->getPrevious(); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @deprecated This method is no longer used internally and will be removed in 3.0 |
||
| 209 | * |
||
| 210 | * @param string|string[] $characters |
||
| 211 | */ |
||
| 212 | public function searchByCharacter($characters): ?DelimiterInterface |
||
| 213 | { |
||
| 214 | if (! \is_array($characters)) { |
||
| 215 | $characters = [$characters]; |
||
| 216 | } |
||
| 217 | |||
| 218 | $opener = $this->top; |
||
| 219 | while ($opener !== null) { |
||
| 220 | if (\in_array($opener->getChar(), $characters, true)) { |
||
| 221 | break; |
||
| 222 | } |
||
| 223 | |||
| 224 | $opener = $opener->getPrevious(); |
||
| 225 | } |
||
| 226 | |||
| 227 | return $opener; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param DelimiterInterface|int|null $stackBottom |
||
| 232 | * |
||
| 233 | * @todo change $stackBottom to an int in 3.0 |
||
| 234 | */ |
||
| 235 | public function processDelimiters($stackBottom, DelimiterProcessorCollection $processors): void |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @internal |
||
| 337 | */ |
||
| 338 | public function __destruct() |
||
| 339 | { |
||
| 340 | while ($this->top) { |
||
| 341 | $this->removeDelimiter($this->top); |
||
| 342 | } |
||
| 343 | |||
| 344 | while ($this->brackets) { |
||
| 345 | $this->removeBracket(); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @deprecated This method will be dropped in 3.0 once all delimiters MUST have an index/position |
||
| 351 | */ |
||
| 352 | private function getIndex(?DelimiterInterface $delimiter): int |
||
| 395 | } |
||
| 396 | } |
||
| 397 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..