Conditions | 45 |
Paths | 30 |
Total Lines | 200 |
Code Lines | 127 |
Lines | 50 |
Ratio | 25 % |
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 |
||
154 | public function parse(Parser $parser, TokensList $list) |
||
155 | { |
||
156 | ++$list->idx; // Skipping `DELETE`. |
||
157 | |||
158 | // parse any options if provided |
||
159 | $this->options = OptionsArray::parse( |
||
160 | $parser, |
||
161 | $list, |
||
162 | static::$OPTIONS |
||
163 | ); |
||
164 | ++$list->idx; |
||
165 | |||
166 | /** |
||
167 | * The state of the parser. |
||
168 | * |
||
169 | * Below are the states of the parser. |
||
170 | * |
||
171 | * 0 ---------------------------------[ FROM ]----------------------------------> 2 |
||
172 | * 0 ------------------------------[ table[.*] ]--------------------------------> 1 |
||
173 | * 1 ---------------------------------[ FROM ]----------------------------------> 2 |
||
174 | * 2 --------------------------------[ USING ]----------------------------------> 3 |
||
175 | * 2 --------------------------------[ WHERE ]----------------------------------> 4 |
||
176 | * 2 --------------------------------[ ORDER ]----------------------------------> 5 |
||
177 | * 2 --------------------------------[ LIMIT ]----------------------------------> 6 |
||
178 | * |
||
179 | * @var int $state |
||
180 | */ |
||
181 | $state = 0; |
||
182 | |||
183 | /** |
||
184 | * If the query is multi-table or not |
||
185 | * |
||
186 | * @var bool $multiTable |
||
187 | */ |
||
188 | $multiTable = false; |
||
189 | |||
190 | for (; $list->idx < $list->count; ++$list->idx) { |
||
191 | /** |
||
192 | * Token parsed at this moment. |
||
193 | * |
||
194 | * @var Token $token |
||
195 | */ |
||
196 | $token = $list->tokens[$list->idx]; |
||
197 | |||
198 | // End of statement. |
||
199 | if ($token->type === Token::TYPE_DELIMITER) { |
||
200 | break; |
||
201 | } |
||
202 | |||
203 | // Skipping whitespaces and comments. |
||
204 | if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
||
205 | continue; |
||
206 | } |
||
207 | |||
208 | if ($state === 0) { |
||
209 | if ($token->type === Token::TYPE_KEYWORD |
||
210 | && $token->value !== 'FROM' |
||
211 | ) { |
||
212 | $parser->error(__('Unexpected keyword.'), $token); |
||
213 | break; |
||
214 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
215 | && $token->value === 'FROM' |
||
216 | ) { |
||
217 | ++$list->idx; // Skip 'FROM' |
||
218 | $this->from = ExpressionArray::parse($parser, $list); |
||
219 | $state = 2; |
||
220 | } else { |
||
221 | $this->columns = ExpressionArray::parse($parser, $list); |
||
222 | $state = 1; |
||
223 | } |
||
224 | View Code Duplication | } elseif ($state === 1) { |
|
225 | if ($token->type === Token::TYPE_KEYWORD |
||
226 | && $token->value !== 'FROM' |
||
227 | ) { |
||
228 | $parser->error(__('Unexpected keyword.'), $token); |
||
229 | break; |
||
230 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
231 | && $token->value === 'FROM' |
||
232 | ) { |
||
233 | ++$list->idx; // Skip 'FROM' |
||
234 | $this->from = ExpressionArray::parse($parser, $list); |
||
235 | $state = 2; |
||
236 | } else { |
||
237 | $parser->error(__('Unexpected token.'), $token); |
||
238 | break; |
||
239 | } |
||
240 | } elseif ($state === 2) { |
||
241 | if ($token->type === Token::TYPE_KEYWORD |
||
242 | && $token->value === 'USING' |
||
243 | ) { |
||
244 | ++$list->idx; // Skip 'USING' |
||
245 | $this->using = ExpressionArray::parse($parser, $list); |
||
246 | $state = 3; |
||
247 | |||
248 | $multiTable = true; |
||
249 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
250 | && $token->value === 'WHERE' |
||
251 | ) { |
||
252 | ++$list->idx; // Skip 'WHERE' |
||
253 | $this->where = Condition::parse($parser, $list); |
||
254 | $state = 4; |
||
255 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
256 | && $token->value === 'ORDER BY' |
||
257 | ) { |
||
258 | ++$list->idx; // Skip 'ORDER BY' |
||
259 | $this->order = OrderKeyword::parse($parser, $list); |
||
260 | $state = 5; |
||
261 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
262 | && $token->value === 'LIMIT' |
||
263 | ) { |
||
264 | ++$list->idx; // Skip 'LIMIT' |
||
265 | $this->limit = Limit::parse($parser, $list); |
||
266 | $state = 6; |
||
267 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
268 | $parser->error(__('Unexpected keyword.'), $token); |
||
269 | break; |
||
270 | } else { |
||
271 | $parser->error(__('Unexpected token.'), $token); |
||
272 | break; |
||
273 | } |
||
274 | View Code Duplication | } elseif ($state === 3) { |
|
275 | if ($token->type === Token::TYPE_KEYWORD |
||
276 | && $token->value === 'WHERE' |
||
277 | ) { |
||
278 | ++$list->idx; // Skip 'WHERE' |
||
279 | $this->where = Condition::parse($parser, $list); |
||
280 | $state = 4; |
||
281 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
282 | $parser->error(__('Unexpected keyword.'), $token); |
||
283 | break; |
||
284 | } else { |
||
285 | $parser->error(__('Unexpected token.'), $token); |
||
286 | break; |
||
287 | } |
||
288 | } elseif ($state === 4) { |
||
289 | View Code Duplication | if ($multiTable === true |
|
290 | && $token->type === Token::TYPE_KEYWORD |
||
291 | ) { |
||
292 | $parser->error( |
||
293 | __('This type of clause is not valid in Multi-table queries.'), |
||
294 | $token |
||
295 | ); |
||
296 | break; |
||
297 | } |
||
298 | |||
299 | if ($token->type === Token::TYPE_KEYWORD |
||
300 | && $token->value === 'ORDER BY' |
||
301 | ) { |
||
302 | ++$list->idx; // Skip 'ORDER BY' |
||
303 | $this->order = OrderKeyword::parse($parser, $list); |
||
304 | $state = 5; |
||
305 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
306 | && $token->value === 'LIMIT' |
||
307 | ) { |
||
308 | ++$list->idx; // Skip 'LIMIT' |
||
309 | $this->limit = Limit::parse($parser, $list); |
||
310 | $state = 6; |
||
311 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
312 | $parser->error(__('Unexpected keyword.'), $token); |
||
313 | break; |
||
314 | } else { |
||
315 | $parser->error(__('Unexpected token.'), $token); |
||
316 | break; |
||
317 | } |
||
318 | } elseif ($state === 5) { |
||
319 | View Code Duplication | if ($multiTable === true |
|
320 | && $token->type === Token::TYPE_KEYWORD |
||
321 | ) { |
||
322 | $parser->error( |
||
323 | __('This type of clause is not valid in Multi-table queries.'), |
||
324 | $token |
||
325 | ); |
||
326 | break; |
||
327 | } |
||
328 | |||
329 | if ($token->type === Token::TYPE_KEYWORD |
||
330 | && $token->value === 'LIMIT' |
||
331 | ) { |
||
332 | ++$list->idx; // Skip 'LIMIT' |
||
333 | $this->limit = Limit::parse($parser, $list); |
||
334 | $state = 6; |
||
335 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
336 | $parser->error(__('Unexpected keyword.'), $token); |
||
337 | break; |
||
338 | } else { |
||
339 | break; |
||
340 | } |
||
341 | } |
||
342 | } |
||
343 | |||
344 | if ($state >= 2) { |
||
345 | foreach ($this->from as $from_expr) { |
||
346 | $from_expr->database = $from_expr->table; |
||
347 | $from_expr->table = $from_expr->column; |
||
348 | $from_expr->column = null; |
||
349 | } |
||
350 | } |
||
351 | |||
352 | --$list->idx; |
||
353 | } |
||
354 | } |
||
355 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: