Conditions | 12 |
Paths | 36 |
Total Lines | 38 |
Code Lines | 29 |
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 |
||
52 | public static function __callStatic(string $name, array $arguments): void |
||
53 | { |
||
54 | // Handle Exception-parameter |
||
55 | $exception = AssertionFailedException::class; |
||
56 | |||
57 | $last = end($arguments); |
||
58 | if (is_string($last) && class_exists($last) && is_subclass_of($last, Throwable::class)) { |
||
59 | $exception = $last; |
||
60 | array_pop($arguments); |
||
61 | } |
||
62 | |||
63 | try { |
||
64 | if (method_exists(static::class, $name)) { |
||
65 | call_user_func_array([static::class, $name], $arguments); |
||
66 | return; |
||
67 | } elseif (preg_match('/^nullOr(.*)$/i', $name, $matches)) { |
||
68 | $method = lcfirst($matches[1]); |
||
69 | if (method_exists(static::class, $method)) { |
||
70 | call_user_func_array([static::class, 'nullOr'], [[static::class, $method], $arguments]); |
||
71 | } elseif (method_exists(BaseAssert::class, $method)) { |
||
72 | call_user_func_array([static::class, 'nullOr'], [[BaseAssert::class, $method], $arguments]); |
||
73 | } else { |
||
74 | throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $method)); |
||
75 | } |
||
76 | } elseif (preg_match('/^all(.*)$/i', $name, $matches)) { |
||
77 | $method = lcfirst($matches[1]); |
||
78 | if (method_exists(static::class, $method)) { |
||
79 | call_user_func_array([static::class, 'all'], [[static::class, $method], $arguments]); |
||
80 | } elseif (method_exists(BaseAssert::class, $method)) { |
||
81 | call_user_func_array([static::class, 'all'], [[BaseAssert::class, $method], $arguments]); |
||
82 | } else { |
||
83 | throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $method)); |
||
84 | } |
||
85 | } else { |
||
86 | throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $name)); |
||
87 | } |
||
88 | } catch (InvalidArgumentException $e) { |
||
89 | throw new $exception($e->getMessage()); |
||
90 | } |
||
172 |