| Conditions | 13 | 
| Paths | 12 | 
| Total Lines | 75 | 
| 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 | ||
| 110 | public function parse(&$tokens): void | ||
| 111 |     { | ||
| 112 | $source = $this->parser->getLoader()->read($this->templateName); | ||
| 113 | $mainTokens = $this->parser->tokenize($source); | ||
| 114 | |||
| 115 | $lexerExtends = new Lexer( | ||
| 116 | '/^' . Token::BLOCK_OPEN | ||
| 117 | . '\s*extends (.*)?' | ||
| 118 | . Token::BLOCK_CLOSE . '$/' | ||
| 119 | ); | ||
| 120 | |||
| 121 | $match = null; | ||
| 122 |         foreach ($mainTokens as $mainToken) { | ||
| 123 |             if ($lexerExtends->match($mainToken)) { | ||
| 124 | $match = $lexerExtends->getStringMatch(1); | ||
| 125 | break; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | $result = []; | ||
| 130 |         if ($match !== null) { | ||
| 131 | $result = array_merge($mainTokens, $tokens); | ||
| 132 |         } else { | ||
| 133 | $childrenTokens = $this->findBlocks($tokens); | ||
| 134 | |||
| 135 | $lexerBlockStart = new Lexer( | ||
| 136 | '/^' . Token::BLOCK_OPEN | ||
| 137 | . '\s*block (\w+)\s*(.*)?' | ||
| 138 | . Token::BLOCK_CLOSE . '$/' | ||
| 139 | ); | ||
| 140 | |||
| 141 | $lexerBlockEnd = new Lexer( | ||
| 142 | '/^' . Token::BLOCK_OPEN | ||
| 143 | . '\s*endblock\s*?' | ||
| 144 | . Token::BLOCK_CLOSE . '$/' | ||
| 145 | ); | ||
| 146 | |||
| 147 | $name = null; | ||
|  | |||
| 148 | $keep = false; | ||
| 149 | $count = count($mainTokens); | ||
| 150 |             for ($i = 0; $i < $count; $i++) { | ||
| 151 |                 if ($lexerBlockStart->match($mainTokens[$i])) { | ||
| 152 | $name = $lexerBlockStart->getStringMatch(1); | ||
| 153 |                     if (isset($childrenTokens[$name])) { | ||
| 154 | $keep = true; | ||
| 155 | array_push($result, $mainTokens[$i]); | ||
| 156 |                         foreach ($childrenTokens[$name] as $item) { | ||
| 157 | array_push($result, $item); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 |                 if (!$keep) { | ||
| 163 | array_push($result, $mainTokens[$i]); | ||
| 164 | } | ||
| 165 | |||
| 166 |                 if ($lexerBlockEnd->match($mainTokens[$i]) && $keep) { | ||
| 167 | $keep = false; | ||
| 168 | array_push($result, $mainTokens[$i]); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | $cache = $this->parser->getTemplate()->getCache(); | ||
| 174 | |||
| 175 | $this->hash = md5($this->templateName); | ||
| 176 | |||
| 177 | /** @var Document|false $document */ | ||
| 178 | $document = $cache->read($this->hash, true); | ||
| 179 | |||
| 180 |         if ($document === false || ($document->hasIncludes())) { | ||
| 181 | $this->document = new Document($result, $this->parser); | ||
| 182 | $cache->write($this->hash, $this->document, true); | ||
| 183 |         } else { | ||
| 184 | $this->document = $document; | ||
| 185 | } | ||
| 258 |