Conditions | 16 |
Paths | 88 |
Total Lines | 55 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 2 |
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 |
||
26 | public function validateSettingForm(Request $request) |
||
27 | { |
||
28 | $parser = new Parser(); |
||
29 | $return = []; |
||
30 | $return['error'] = ''; |
||
31 | $return['name'] = $request->request->get('settingName'); |
||
32 | $return['type'] = $request->request->get('settingType'); |
||
33 | $return['description'] = $request->request->get('settingDescription'); |
||
34 | $profiles = $request->request->get('settingProfiles'); |
||
35 | |||
36 | is_string($profiles) ? $return['profiles'] = [$profiles] : $return['profiles'] = $profiles; |
||
37 | |||
38 | if ($return['name'] == '') { |
||
39 | $return['error'] = 'You must set a name to the setting. '; |
||
40 | } |
||
41 | if (count($return['profiles']) == 0) { |
||
42 | $return['error'] = $return['error'].'At least 1 profile has to be set. '; |
||
43 | } |
||
44 | switch ($return['type']) { |
||
45 | case 'bool': |
||
46 | $request->request->get('setting-boolean') == 'true' ? |
||
47 | $return['value'] = true : |
||
48 | $return['value'] = false; |
||
49 | break; |
||
50 | case 'string': |
||
51 | $return['value'] = $request->request->get('setting-default'); |
||
52 | if ($return['value'] == '') { |
||
53 | $return['error'] = $return['error'].'You must set a value to the setting. '; |
||
54 | } |
||
55 | break; |
||
56 | case 'object': |
||
57 | try { |
||
58 | $return['value'] = json_encode($parser->parse($request->request->get('setting-object'))); |
||
59 | } catch (\Exception $e) { |
||
60 | $return['error'] = $return['error'].'Passed setting value does not contain valid yaml. '; |
||
61 | } |
||
62 | if ($return['value'] == '') { |
||
63 | $return['error'] = $return['error'].'You must set a value to the setting. '; |
||
64 | } |
||
65 | break; |
||
66 | case 'array': |
||
67 | $return['value'] = []; |
||
68 | foreach ($request->request->all() as $key => $item) { |
||
69 | if (preg_match('/setting-array_[0-9]*/', $key)) { |
||
70 | $return['value'][] = $item; |
||
71 | } |
||
72 | } |
||
73 | if (count($return['value']) == 0 || $return['value'][0] == '') { |
||
74 | $return['error'] = $return['error'].'You must set a value to the setting. '; |
||
75 | } |
||
76 | break; |
||
77 | } |
||
78 | |||
79 | return $return; |
||
80 | } |
||
81 | |||
114 |