| Conditions | 13 |
| Paths | 22 |
| Total Lines | 98 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 86 | public function parse(array &$tokens): void |
||
| 87 | { |
||
| 88 | $startRegex = '/^' . Token::BLOCK_OPEN . '/'; |
||
| 89 | $variableStartRegex = '/^' . Token::VARIABLE_OPEN . '/'; |
||
| 90 | $tagRegex = '/^' |
||
| 91 | . Token::BLOCK_OPEN |
||
| 92 | . Token::WHITESPACE_CONTROL |
||
| 93 | . '?\s*(\w+)\s*(.*?)' |
||
| 94 | . Token::WHITESPACE_CONTROL |
||
| 95 | . '?' |
||
| 96 | . Token::BLOCK_CLOSE |
||
| 97 | . '$/'; |
||
| 98 | |||
| 99 | $lexerStart = new Lexer($startRegex); |
||
| 100 | $lexerVariableStart = new Lexer($variableStartRegex); |
||
| 101 | $lexerTag = new Lexer($tagRegex); |
||
| 102 | |||
| 103 | $this->nodeList = []; |
||
| 104 | //Custom tags |
||
| 105 | $tags = $this->parser->getTemplate()->getTags(); |
||
| 106 | while (count($tokens) > 0) { |
||
| 107 | $token = (string) array_shift($tokens); |
||
| 108 | if ($lexerStart->match($token)) { |
||
| 109 | $this->whitespaceHandler($token); |
||
| 110 | if ($lexerTag->match($token)) { |
||
| 111 | // If we found the proper block |
||
| 112 | // delimitor just end parsing here |
||
| 113 | // and let the outer block proceed |
||
| 114 | if ($lexerTag->getStringMatch(1) === $this->blockDelimiter()) { |
||
| 115 | $this->endTag(); |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | $node = null; |
||
| 120 | if (array_key_exists($lexerTag->getStringMatch(1), $tags)) { |
||
| 121 | $tagNameClass = $tags[$lexerTag->getStringMatch(1)]; |
||
| 122 | if (is_string($tagNameClass)) { |
||
| 123 | $node = new $tagNameClass( |
||
| 124 | $lexerTag->getStringMatch(2), |
||
| 125 | $tokens, |
||
| 126 | $this->parser |
||
| 127 | ); |
||
| 128 | } else { |
||
| 129 | $node = $tagNameClass; |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | //check for core tags |
||
| 133 | $coreTagName = $lexerTag->getStringMatch(1); |
||
| 134 | $coreTagNameClass = 'Platine\\Template\\Tag\\' . ucwords($coreTagName) . 'Tag'; |
||
| 135 | if (class_exists($coreTagNameClass)) { |
||
| 136 | $node = new $coreTagNameClass( |
||
| 137 | $lexerTag->getStringMatch(2), |
||
| 138 | $tokens, |
||
| 139 | $this->parser |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($node !== null) { |
||
| 145 | if (!$node instanceof AbstractTag) { |
||
| 146 | throw new InvalidArgumentException(sprintf( |
||
| 147 | 'Tag class [%s] must extends base classes [%s] or [%s]', |
||
| 148 | get_class($node), |
||
| 149 | AbstractTag::class, |
||
| 150 | AbstractBlock::class |
||
| 151 | )); |
||
| 152 | } |
||
| 153 | |||
| 154 | $this->nodeList[] = $node; |
||
| 155 | |||
| 156 | if ($lexerTag->getStringMatch(1) === 'extends') { |
||
| 157 | return; |
||
| 158 | } |
||
| 159 | } else { |
||
| 160 | $this->unknownTag($lexerTag->getStringMatch(1), $lexerTag->getStringMatch(2), $tokens); |
||
| 161 | } |
||
| 162 | } else { |
||
| 163 | throw new ParseException(sprintf( |
||
| 164 | 'Tag [%s] was not properly terminated (won\'t match [%s])', |
||
| 165 | $token, |
||
| 166 | $lexerTag |
||
| 167 | )); |
||
| 168 | } |
||
| 169 | } elseif ($lexerVariableStart->match($token)) { |
||
| 170 | $this->whitespaceHandler($token); |
||
| 171 | $this->nodeList[] = $this->createVariable($token); |
||
| 172 | } else { |
||
| 173 | // This is neither a tag or a variable, proceed with an ltrim |
||
| 174 | if ($this->trimWhitespace) { |
||
| 175 | $token = ltrim($token); |
||
| 176 | } |
||
| 177 | |||
| 178 | $this->trimWhitespace = false; |
||
| 179 | $this->nodeList[] = $token; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | $this->assertMissingDelimiter(); |
||
| 184 | } |
||
| 336 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.