Conditions | 11 |
Paths | 37 |
Total Lines | 48 |
Code Lines | 39 |
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 |
||
37 | public function setSettingAction(Request $request) |
||
38 | { |
||
39 | $data = json_decode($request->request->get('data'), true); |
||
40 | $manager = $this->getSettingsManager(); |
||
41 | $parser = new Parser(); |
||
42 | $value = $data['setting_value']; |
||
43 | $type = $data['setting_type']; |
||
44 | $name = htmlentities($data['setting_name']); |
||
45 | $profiles = $data['setting_profiles']; |
||
46 | $description = htmlentities($data['setting_description']); |
||
|
|||
47 | $response = []; |
||
48 | $response['error'] = ''; |
||
49 | |||
50 | switch ($type) { |
||
51 | case 'Boolean': |
||
52 | $value == 'true' ? $value = true : $value = false; |
||
53 | break; |
||
54 | case 'Default': |
||
55 | $value = htmlentities($value); |
||
56 | break; |
||
57 | case 'Object': |
||
58 | try { |
||
59 | $value = $parser->parse($value); |
||
60 | } catch (\Exception $e) { |
||
61 | $response['error'] = 'Passed setting value is not correct yaml'; |
||
62 | $response['code'] = 406; |
||
63 | return new JsonResponse(json_encode($response)); |
||
64 | } |
||
65 | break; |
||
66 | case 'Array': |
||
67 | foreach ($value as $key => $item) { |
||
68 | $value[$key] = htmlentities($item); |
||
69 | } |
||
70 | break; |
||
71 | } |
||
72 | try { |
||
73 | foreach ($profiles as $profile) { |
||
74 | $manager->set($name, $value, $profile); |
||
75 | } |
||
76 | } catch (\Exception $e) { |
||
77 | $response['error'] = 'Insertion failed: '.$e->getMessage(); |
||
78 | $response['code'] = 406; |
||
79 | } |
||
80 | if (!isset($response['code'])) { |
||
81 | $response['code'] = 201; |
||
82 | } |
||
83 | return new JsonResponse(json_encode($response)); |
||
84 | } |
||
85 | |||
193 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.