Conditions | 21 |
Paths | 38 |
Total Lines | 80 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
151 | protected function tokenizeExpression($expression) |
||
152 | { |
||
153 | $stream = parent::tokenize($expression); |
||
154 | $tokens = array(); |
||
155 | $previousWasDot = false; |
||
156 | $ignorePrimaryExpressions = array_flip(['null', 'NULL', 'false', 'FALSE', 'true', 'TRUE']); |
||
157 | while (!$stream->isEOF()) { |
||
158 | /* @var \Symfony\Component\ExpressionLanguage\Token $token */ |
||
159 | $token = $stream->current; |
||
160 | $stream->next(); |
||
161 | if ($token->type === Token::NAME_TYPE && !$previousWasDot) { |
||
162 | if (array_key_exists($token->value, $ignorePrimaryExpressions)) { |
||
163 | $tokens[] = $token; |
||
164 | continue; |
||
165 | } |
||
166 | $isTest = false; |
||
167 | if ($stream->current->test(Token::PUNCTUATION_TYPE, '(')) { |
||
168 | $tokens[] = $token; |
||
169 | $tokens[] = $stream->current; |
||
170 | $stream->next(); |
||
171 | if ($token->value === 'isset' || $token->value === 'empty') { |
||
172 | $isTest = true; |
||
173 | $token = $stream->current; |
||
174 | if ($token->type !== Token::NAME_TYPE) { |
||
175 | throw new SyntaxError('Expected name', $token->cursor); |
||
176 | } |
||
177 | $stream->next(); |
||
178 | } else { |
||
179 | $tokens[] = new Token(Token::STRING_TYPE, $token->value, $token->cursor); |
||
180 | $token->value = 'call'; |
||
181 | if (!$stream->current->test(Token::PUNCTUATION_TYPE, ')')) { |
||
182 | $tokens[] = new Token(Token::PUNCTUATION_TYPE, ',', $token->cursor); |
||
183 | } |
||
184 | continue; |
||
185 | } |
||
186 | } |
||
187 | $names = array($token->value); |
||
188 | $isFunctionCall = false; |
||
189 | while (!$stream->isEOF() && $stream->current->type === Token::PUNCTUATION_TYPE && $stream->current->value === '.') { |
||
190 | $stream->next(); |
||
191 | $nameToken = $stream->current; |
||
192 | $stream->next(); |
||
193 | // Operators like "not" and "matches" are valid method or property names - others not |
||
194 | if ($nameToken->type !== Token::NAME_TYPE |
||
195 | && ($nameToken->type !== Token::OPERATOR_TYPE || !preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/A', $nameToken->value)) |
||
196 | ) { |
||
197 | throw new SyntaxError('Expected name', $nameToken->cursor); |
||
198 | } |
||
199 | if ($stream->current->test(Token::PUNCTUATION_TYPE, '(')) { |
||
200 | $isFunctionCall = true; |
||
201 | } else { |
||
202 | $names[] = $nameToken->value; |
||
203 | } |
||
204 | } |
||
205 | if ($isTest) { |
||
206 | if ($isFunctionCall) { |
||
207 | throw new SyntaxError('Can\'t use function return value in write context', $stream->current->cursor); |
||
208 | } |
||
209 | if (!$stream->current->test(Token::PUNCTUATION_TYPE, ')')) { |
||
210 | throw new SyntaxError('Expected )', $stream->current->cursor); |
||
211 | } |
||
212 | $tokens[] = new Token(Token::STRING_TYPE, implode('.', $names), $token->cursor); |
||
213 | } else { |
||
214 | $tokens[] = new Token(Token::NAME_TYPE, 'get', $token->cursor); |
||
215 | $tokens[] = new Token(Token::PUNCTUATION_TYPE, '(', $token->cursor); |
||
216 | $tokens[] = new Token(Token::STRING_TYPE, implode('.', $names), $token->cursor); |
||
217 | $tokens[] = new Token(Token::PUNCTUATION_TYPE, ')', $token->cursor); |
||
218 | if ($isFunctionCall) { |
||
219 | $tokens[] = new Token(Token::PUNCTUATION_TYPE, '.', $nameToken->cursor - strlen($nameToken->value)); |
||
220 | $tokens[] = $nameToken; |
||
221 | } |
||
222 | } |
||
223 | } else { |
||
224 | $tokens[] = $token; |
||
225 | $previousWasDot = $token->test(Token::PUNCTUATION_TYPE, '.'); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | return $tokens; |
||
230 | } |
||
231 | } |
||
234 |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.