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