Conditions | 29 |
Paths | 3587 |
Total Lines | 134 |
Code Lines | 70 |
Lines | 44 |
Ratio | 32.84 % |
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 |
||
33 | public function putAction(Request $request) |
||
34 | { |
||
35 | |||
36 | $channel = $request->get('ch'); |
||
37 | |||
38 | if (!$channel) { |
||
39 | return $this->sendResponse(false, ['message'=>'No channel is set']); |
||
40 | } |
||
41 | |||
42 | |||
43 | if (isset($this->channelMap[$channel])) { |
||
44 | $device = $this-> |
||
45 | getDoctrine()-> |
||
46 | getRepository('AppBundle:Device')-> |
||
47 | findOneBy(['alias'=>$this->channelMap[$channel]['name']]); |
||
48 | |||
49 | if (!$device) { |
||
50 | return $this->sendResponse( |
||
51 | false, |
||
52 | ['message'=>'Device "'.$this->channelMap[$channel]['name'].'" not found'] |
||
53 | ); |
||
54 | } |
||
55 | |||
56 | $actions = $device->getActions(); |
||
57 | |||
58 | |||
59 | $resultAction = null; |
||
60 | $changeSet = []; |
||
61 | |||
62 | |||
63 | if ($request->get('cmd') == 4) { |
||
64 | // reverse state, only by human |
||
65 | |||
66 | $oldState = $device->getState(); |
||
67 | $oldState = $oldState['state']; |
||
68 | |||
69 | if ($oldState == 'off') { |
||
70 | // turning on |
||
71 | $changeSet = ['state'=>'on']; |
||
72 | |||
73 | /** @var Action $action */ |
||
74 | foreach ($actions as $action) { |
||
75 | $args = json_decode($action->getArguments(), true); |
||
76 | if (isset($args['state']) && ($args['state'] == 'on')) { |
||
77 | $resultAction = $action; |
||
78 | break; |
||
79 | } |
||
80 | } |
||
81 | View Code Duplication | } else { |
|
|
|||
82 | // turning off |
||
83 | $changeSet = ['state'=>'off']; |
||
84 | /** @var Action $action */ |
||
85 | foreach ($actions as $action) { |
||
86 | $args = json_decode($action->getArguments(), true); |
||
87 | if (isset($args['state']) && ($args['state'] == 'off')) { |
||
88 | $resultAction = $action; |
||
89 | break; |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | View Code Duplication | if ($request->get('cmd') == 0) { |
|
96 | // turn off, only by pi |
||
97 | |||
98 | $changeSet = ['state'=>'off']; |
||
99 | /** @var Action $action */ |
||
100 | foreach ($actions as $action) { |
||
101 | $args = json_decode($action->getArguments(), true); |
||
102 | if (isset($args['state']) && ($args['state'] == 'off')) { |
||
103 | $resultAction = $action; |
||
104 | break; |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if ($request->get('cmd') == 6) { |
||
110 | // turn off, only by pi |
||
111 | |||
112 | $percent = round((($request->get('d0') - 34)/123)*100); |
||
113 | |||
114 | $changeSet = ['state'=>'on', 'percent'=>$percent]; |
||
115 | /** @var Action $action */ |
||
116 | foreach ($actions as $action) { |
||
117 | $args = json_decode($action->getArguments(), true); |
||
118 | if (isset($args['state']) && ($args['state'] == 'on')) { |
||
119 | $resultAction = $action; |
||
120 | break; |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | View Code Duplication | if ($request->get('cmd') == 2) { |
|
126 | // turn on, only by pi |
||
127 | |||
128 | $changeSet = ['state'=>'on']; |
||
129 | /** @var Action $action */ |
||
130 | foreach ($actions as $action) { |
||
131 | $args = json_decode($action->getArguments(), true); |
||
132 | if (isset($args['state']) && ($args['state'] == 'on')) { |
||
133 | $resultAction = $action; |
||
134 | break; |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | View Code Duplication | if (($changeSet['state'] == 'off') && !isset($changeSet['percent'])) { |
|
140 | $changeSet['percent'] = 0; |
||
141 | } |
||
142 | |||
143 | View Code Duplication | if (($changeSet['state'] == 'on') && !isset($changeSet['percent'])) { |
|
144 | $changeSet['percent'] = 100; // ? I don't really know if they restore their states |
||
145 | } |
||
146 | |||
147 | if (!$resultAction) { |
||
148 | return $this->sendResponse(true, ['message'=>'State was changed, but no action was found']); |
||
149 | } |
||
150 | |||
151 | $this->get('actions')->executeVirtual($resultAction, $this->channelMap[$channel]['source'], $changeSet); |
||
152 | |||
153 | $this->getDoctrine()->getManager()->flush(); |
||
154 | |||
155 | return $this->sendResponse( |
||
156 | true, |
||
157 | [ |
||
158 | 'message'=>'Executed successfully', |
||
159 | 'device'=>$device->getAlias(), |
||
160 | 'changeset'=>$changeSet |
||
161 | ] |
||
162 | ); |
||
163 | } else { |
||
164 | return $this->sendResponse(false, ['message'=>'Requested channel does not exist']); |
||
165 | } |
||
166 | } |
||
167 | |||
184 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.