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