| Conditions | 10 |
| Paths | 18 |
| Total Lines | 23 |
| Code Lines | 15 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 13 |
| CRAP Score | 10 |
| Changes | 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 |
||
| 9 | 10 | public function normalize($rule) |
|
| 10 | { |
||
| 11 | 10 | while (is_callable($rule) || (is_object($rule) && $rule instanceof Rule)) { |
|
| 12 | 5 | if (is_callable($rule)) { |
|
| 13 | 4 | $rule = call_user_func($rule); |
|
| 14 | } else { |
||
| 15 | 4 | $rule = $rule->rules(); |
|
| 16 | } |
||
| 17 | } |
||
| 18 | // string -> array |
||
| 19 | 10 | if (!is_array($rule)) { |
|
| 20 | 9 | $rule = [$rule]; |
|
| 21 | } |
||
| 22 | 10 | $normalized = []; |
|
| 23 | 10 | foreach ($rule as $key => $value) { |
|
| 24 | 10 | if (is_int($key) || $key === '' || $key === null) { |
|
| 25 | 10 | $normalized[''] = array_merge($normalized[''] ?? [], (array) $value); |
|
| 26 | } else { |
||
| 27 | 10 | $normalized[$key] = $this->normalize($value); |
|
| 28 | } |
||
| 29 | } |
||
| 30 | 10 | return $normalized; |
|
| 31 | } |
||
| 32 | } |
||
| 33 |