Conditions | 10 |
Paths | 18 |
Total Lines | 27 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Tests | 16 |
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 | 12 | public function normalize($rule): array |
|
10 | { |
||
11 | 12 | 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 | 12 | if (!is_array($rule)) { |
|
20 | 11 | $rule = [$rule]; |
|
21 | } |
||
22 | 12 | $normalized = [[], []]; |
|
23 | 12 | foreach ($rule as $key => $value) { |
|
24 | 12 | if (is_int($key) || $key === '' || $key === null) { |
|
25 | 12 | $normalized[0] = array_merge($normalized[0] ?? [], (array) $value); |
|
26 | } else { |
||
27 | 9 | $target = TargetName::parse($key); |
|
28 | 9 | $normalized[1][] = [ |
|
29 | 9 | [$target->getName(), $target->getIterator(), $target->isOptional(), ], |
|
30 | 12 | $this->normalize($value), |
|
31 | ]; |
||
32 | } |
||
33 | } |
||
34 | 12 | return $normalized; |
|
35 | } |
||
36 | } |
||
37 |