Conditions | 30 |
Paths | 440 |
Total Lines | 177 |
Code Lines | 68 |
Lines | 31 |
Ratio | 17.51 % |
Changes | 6 | ||
Bugs | 1 | Features | 1 |
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 |
||
267 | protected function evaluate($context, $expression, $tokens, $silent) |
||
268 | { |
||
269 | $expression_path = []; |
||
270 | |||
271 | foreach ($tokens as $i => $part) |
||
272 | { |
||
273 | $identifier = $part[self::TOKEN_VALUE]; |
||
274 | |||
275 | $expression_path[] = $identifier; |
||
276 | |||
277 | switch ($part[self::TOKEN_TYPE]) |
||
278 | { |
||
279 | case self::TOKEN_TYPE_IDENTIFIER: |
||
280 | { |
||
281 | if (!is_array($context) && !is_object($context)) |
||
282 | { |
||
283 | throw new \InvalidArgumentException(\ICanBoogie\format |
||
284 | ( |
||
285 | 'Unexpected variable type: %type (%value) for %identifier in expression %expression, should be either an array or an object', [ |
||
286 | |||
287 | '%type' => gettype($context), |
||
288 | '%value' => $context, |
||
289 | '%identifier' => $identifier, |
||
290 | '%expression' => $expression |
||
291 | |||
292 | ] |
||
293 | )); |
||
294 | } |
||
295 | |||
296 | $exists = false; |
||
297 | $next_value = $this->extract_value($context, $identifier, $exists); |
||
298 | |||
299 | if (!$exists) |
||
300 | { |
||
301 | if ($silent) |
||
302 | { |
||
303 | return null; |
||
304 | } |
||
305 | |||
306 | throw new ReferenceError(\ICanBoogie\format('Reference to undefined property %path of expression %expression (defined: :keys) in: :value', [ |
||
307 | |||
308 | 'path' => implode('.', $expression_path), |
||
309 | 'expression' => $expression, |
||
310 | 'keys' => implode(', ', $context instanceof Context ? $context->keys() : array_keys((array) $context)), |
||
311 | 'value' => \ICanBoogie\dump($context) |
||
312 | |||
313 | ])); |
||
314 | } |
||
315 | |||
316 | $context = $next_value; |
||
317 | } |
||
318 | break; |
||
319 | |||
320 | case self::TOKEN_TYPE_FUNCTION: |
||
321 | { |
||
322 | $method = $identifier; |
||
323 | $args = $part[self::TOKEN_ARGS]; |
||
324 | $args_evaluate = $part[self::TOKEN_ARGS_EVALUATE]; |
||
325 | |||
326 | if ($args_evaluate) |
||
327 | { |
||
328 | $this->engine->error('we should evaluate %eval', [ '%eval' => $args_evaluate ]); |
||
329 | } |
||
330 | |||
331 | # |
||
332 | # if value is an object, we check if the object has the method |
||
333 | # |
||
334 | |||
335 | View Code Duplication | if (is_object($context) && method_exists($context, $method)) |
|
336 | { |
||
337 | $context = call_user_func_array([ $context, $method ], $args); |
||
338 | |||
339 | break; |
||
340 | } |
||
341 | |||
342 | # |
||
343 | # well, the object didn't have the method, |
||
344 | # we check internal functions |
||
345 | # |
||
346 | |||
347 | $callback = $this->engine->functions->find($method); |
||
348 | |||
349 | # |
||
350 | # if no internal function matches, we try string and array functions |
||
351 | # depending on the type of the value |
||
352 | # |
||
353 | |||
354 | if (!$callback) |
||
355 | { |
||
356 | if (is_string($context)) |
||
357 | { |
||
358 | View Code Duplication | if (function_exists('str' . $method)) |
|
359 | { |
||
360 | $callback = 'str' . $method; |
||
361 | } |
||
362 | else if (function_exists('str_' . $method)) |
||
363 | { |
||
364 | $callback = 'str_' . $method; |
||
365 | } |
||
366 | } |
||
367 | else if (is_array($context) || is_object($context)) |
||
368 | { |
||
369 | View Code Duplication | if (function_exists('ICanBoogie\array_' . $method)) |
|
370 | { |
||
371 | $callback = 'ICanBoogie\array_' . $method; |
||
372 | } |
||
373 | else if (function_exists('array_' . $method)) |
||
374 | { |
||
375 | $callback = 'array_' . $method; |
||
376 | } |
||
377 | } |
||
378 | } |
||
379 | |||
380 | # |
||
381 | # our last hope is to try the function "as is" |
||
382 | # |
||
383 | |||
384 | if (!$callback) |
||
385 | { |
||
386 | if (function_exists($method)) |
||
387 | { |
||
388 | $callback = $method; |
||
389 | } |
||
390 | } |
||
391 | |||
392 | View Code Duplication | if (!$callback) |
|
393 | { |
||
394 | if (is_object($context) && method_exists($context, '__call')) |
||
395 | { |
||
396 | $context = call_user_func_array([ $context, $method ], $args); |
||
397 | |||
398 | break; |
||
399 | } |
||
400 | } |
||
401 | |||
402 | # |
||
403 | # |
||
404 | # |
||
405 | |||
406 | if (!$callback) |
||
407 | { |
||
408 | throw new \Exception(\ICanBoogie\format('Unknown method %method for expression %expression.', [ |
||
409 | |||
410 | '%method' => $method, |
||
411 | '%expression' => $expression |
||
412 | |||
413 | ])); |
||
414 | } |
||
415 | |||
416 | # |
||
417 | # create evaluation |
||
418 | # |
||
419 | |||
420 | array_unshift($args, $context); |
||
421 | |||
422 | if (PHP_MAJOR_VERSION > 5 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 2)) |
||
423 | { |
||
424 | if ($callback == 'array_shift') |
||
425 | { |
||
426 | $context = array_shift($context); |
||
427 | } |
||
428 | else |
||
429 | { |
||
430 | $context = call_user_func_array($callback, $args); |
||
431 | } |
||
432 | } |
||
433 | else |
||
434 | { |
||
435 | $context = call_user_func_array($callback, $args); |
||
436 | } |
||
437 | } |
||
438 | break; |
||
439 | } |
||
440 | } |
||
441 | |||
442 | return $context; |
||
443 | } |
||
444 | |||
512 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: