1 | <?php |
||
23 | final class DelimiterStack |
||
24 | { |
||
25 | /** |
||
26 | * @var DelimiterInterface|null |
||
27 | */ |
||
28 | private $top; |
||
29 | |||
30 | 1062 | public function push(DelimiterInterface $newDelimiter) |
|
31 | { |
||
32 | 1062 | $newDelimiter->setPrevious($this->top); |
|
33 | |||
34 | 1062 | if ($this->top !== null) { |
|
35 | 609 | $this->top->setNext($newDelimiter); |
|
36 | } |
||
37 | |||
38 | 1062 | $this->top = $newDelimiter; |
|
39 | 1062 | } |
|
40 | |||
41 | /** |
||
42 | * @param DelimiterInterface|null $stackBottom |
||
43 | * |
||
44 | * @return DelimiterInterface|null |
||
45 | */ |
||
46 | 2415 | private function findEarliest(DelimiterInterface $stackBottom = null): ?DelimiterInterface |
|
55 | |||
56 | /** |
||
57 | * @param DelimiterInterface $delimiter |
||
58 | */ |
||
59 | 1062 | public function removeDelimiter(DelimiterInterface $delimiter) |
|
60 | { |
||
61 | 1062 | if ($delimiter->getPrevious() !== null) { |
|
62 | 309 | $delimiter->getPrevious()->setNext($delimiter->getNext()); |
|
63 | } |
||
64 | |||
65 | 1062 | if ($delimiter->getNext() === null) { |
|
66 | // top of stack |
||
67 | 1062 | $this->top = $delimiter->getPrevious(); |
|
68 | } else { |
||
69 | 495 | $delimiter->getNext()->setPrevious($delimiter->getPrevious()); |
|
70 | } |
||
71 | 1062 | } |
|
72 | |||
73 | 504 | private function removeDelimiterAndNode(DelimiterInterface $delimiter) |
|
74 | { |
||
75 | 504 | $delimiter->getInlineNode()->detach(); |
|
76 | 504 | $this->removeDelimiter($delimiter); |
|
77 | 504 | } |
|
78 | |||
79 | 504 | private function removeDelimitersBetween(DelimiterInterface $opener, DelimiterInterface $closer) |
|
80 | { |
||
81 | 504 | $delimiter = $closer->getPrevious(); |
|
82 | 504 | while ($delimiter !== null && $delimiter !== $opener) { |
|
83 | 24 | $previous = $delimiter->getPrevious(); |
|
84 | 24 | $this->removeDelimiter($delimiter); |
|
85 | 24 | $delimiter = $previous; |
|
86 | } |
||
87 | 504 | } |
|
88 | |||
89 | /** |
||
90 | * @param DelimiterInterface|null $stackBottom |
||
91 | */ |
||
92 | 2415 | public function removeAll(DelimiterInterface $stackBottom = null) |
|
93 | { |
||
94 | 2415 | while ($this->top && $this->top !== $stackBottom) { |
|
95 | 516 | $this->removeDelimiter($this->top); |
|
96 | } |
||
97 | 2415 | } |
|
98 | |||
99 | /** |
||
100 | * @param string $character |
||
101 | */ |
||
102 | 303 | public function removeEarlierMatches(string $character) |
|
103 | { |
||
104 | 303 | $opener = $this->top; |
|
105 | 303 | while ($opener !== null) { |
|
106 | 57 | if ($opener->getChar() === $character) { |
|
107 | 24 | $opener->setActive(false); |
|
108 | } |
||
109 | |||
110 | 57 | $opener = $opener->getPrevious(); |
|
111 | } |
||
112 | 303 | } |
|
113 | |||
114 | /** |
||
115 | * @param string|string[] $characters |
||
116 | * |
||
117 | * @return DelimiterInterface|null |
||
118 | */ |
||
119 | 453 | public function searchByCharacter($characters): ?DelimiterInterface |
|
135 | |||
136 | 2415 | public function processDelimiters(?DelimiterInterface $stackBottom, DelimiterProcessorCollection $processors) |
|
137 | { |
||
138 | 2415 | $openersBottom = []; |
|
139 | |||
140 | // Find first closer above stackBottom |
||
141 | 2415 | $closer = $this->findEarliest($stackBottom); |
|
222 | } |
||
223 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: