| Conditions | 13 |
| Paths | 12 |
| Total Lines | 74 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 109 | public function parse(array &$tokens): void |
||
| 110 | { |
||
| 111 | $source = $this->parser->getLoader()->read($this->templateName); |
||
| 112 | $mainTokens = $this->parser->tokenize($source); |
||
| 113 | |||
| 114 | $lexerExtends = new Lexer( |
||
| 115 | '/^' . Token::BLOCK_OPEN |
||
| 116 | . '\s*extends (.*)?' |
||
| 117 | . Token::BLOCK_CLOSE . '$/' |
||
| 118 | ); |
||
| 119 | |||
| 120 | $match = null; |
||
| 121 | foreach ($mainTokens as $mainToken) { |
||
| 122 | if ($lexerExtends->match($mainToken)) { |
||
| 123 | $match = $lexerExtends->getStringMatch(1); |
||
| 124 | break; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $result = []; |
||
| 129 | if ($match !== null) { |
||
| 130 | $result = array_merge($mainTokens, $tokens); |
||
| 131 | } else { |
||
| 132 | $childrenTokens = $this->findBlocks($tokens); |
||
| 133 | |||
| 134 | $lexerBlockStart = new Lexer( |
||
| 135 | '/^' . Token::BLOCK_OPEN |
||
| 136 | . '\s*block (\w+)\s*(.*)?' |
||
| 137 | . Token::BLOCK_CLOSE . '$/' |
||
| 138 | ); |
||
| 139 | |||
| 140 | $lexerBlockEnd = new Lexer( |
||
| 141 | '/^' . Token::BLOCK_OPEN |
||
| 142 | . '\s*endblock\s*?' |
||
| 143 | . Token::BLOCK_CLOSE . '$/' |
||
| 144 | ); |
||
| 145 | |||
| 146 | $name = null; |
||
|
|
|||
| 147 | $keep = false; |
||
| 148 | $count = count($mainTokens); |
||
| 149 | for ($i = 0; $i < $count; $i++) { |
||
| 150 | if ($lexerBlockStart->match($mainTokens[$i])) { |
||
| 151 | $name = $lexerBlockStart->getStringMatch(1); |
||
| 152 | if (isset($childrenTokens[$name])) { |
||
| 153 | $keep = true; |
||
| 154 | array_push($result, $mainTokens[$i]); |
||
| 155 | foreach ($childrenTokens[$name] as $item) { |
||
| 156 | array_push($result, $item); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | if (!$keep) { |
||
| 162 | array_push($result, $mainTokens[$i]); |
||
| 163 | } |
||
| 164 | |||
| 165 | if ($lexerBlockEnd->match($mainTokens[$i]) && $keep) { |
||
| 166 | $keep = false; |
||
| 167 | array_push($result, $mainTokens[$i]); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | $cache = $this->parser->getTemplate()->getCache(); |
||
| 173 | $this->hash = md5($this->templateName); |
||
| 174 | |||
| 175 | /** @var Document|false $document */ |
||
| 176 | $document = $cache->read($this->hash, true); |
||
| 177 | |||
| 178 | if ($document === false || ($document->hasIncludes())) { |
||
| 179 | $this->document = new Document($result, $this->parser); |
||
| 180 | $cache->write($this->hash, $this->document, true); |
||
| 181 | } else { |
||
| 182 | $this->document = $document; |
||
| 183 | } |
||
| 256 |