Conditions | 11 |
Paths | 14 |
Total Lines | 58 |
Code Lines | 34 |
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 |
||
18 | public function addElements($elements, $parent = null) { |
||
19 | $parent = $parent?: $this; |
||
20 | |||
21 | foreach ($elements as $name => $desc) { |
||
22 | $name = $desc['name']?? $name; |
||
23 | |||
24 | $this->addControlRules($name, $desc['rules']?? []); |
||
25 | |||
26 | switch ($desc['type']?? 'field') { |
||
27 | case 'field': |
||
28 | $desc = is_string($desc)? [ |
||
29 | 'decorator' => [$desc] |
||
30 | ]: $desc; |
||
31 | |||
32 | $field = $parent->addControl($name, $desc['decorator']?? [], $desc['options']?? []); |
||
33 | |||
34 | if ($default = $desc['default']) { |
||
35 | $field->set($default); |
||
36 | } |
||
37 | |||
38 | if ($desc['display']?? false) { |
||
39 | $this->addControlDisplayRules([$name => $desc['display']]); |
||
40 | } |
||
41 | break; |
||
42 | |||
43 | case 'group': |
||
44 | $seed = $desc['seed']?? [$name]; |
||
45 | |||
46 | $group = $parent->addGroup($seed); |
||
47 | |||
48 | $this->addElements($desc['elements'], $group); |
||
49 | |||
50 | if ($desc['display']?? false) { |
||
51 | $this->addGroupDisplayRules([$name => $desc['display']]); |
||
52 | } |
||
53 | break; |
||
54 | |||
55 | case 'header': |
||
56 | $seed = $desc['seed']?? [$name]; |
||
57 | |||
58 | $parent->addHeader($seed); |
||
|
|||
59 | break; |
||
60 | |||
61 | case 'view': |
||
62 | $seed = $desc['seed']?? [Label::class, $name]; |
||
63 | |||
64 | $region = $desc['region']?? null; |
||
65 | |||
66 | $this->add($seed, $region); |
||
67 | break; |
||
68 | |||
69 | default: |
||
70 | ; |
||
71 | break; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | return $this; |
||
76 | } |
||
140 | } |