Conditions | 44 |
Paths | > 20000 |
Total Lines | 127 |
Code Lines | 100 |
Lines | 66 |
Ratio | 51.97 % |
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 |
||
147 | private function _info() |
||
148 | { |
||
149 | if (empty($this->_output)) { |
||
150 | return; |
||
151 | } |
||
152 | foreach ($this->_output as $result) { |
||
153 | $dev = new UPSDevice(); |
||
154 | $status = ""; |
||
155 | $status2 = ""; |
||
156 | $status3 = ""; |
||
157 | $dev->setMode("SNMP"); |
||
158 | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.1\.1\.2\.0 = STRING:\s(.*)/m', $result, $data)) { |
||
159 | $dev->setName(trim($data[1], "\" \r\t")); |
||
160 | } |
||
161 | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.1\.1\.1\.0 = STRING:\s(.*)/m', $result, $data)) { |
||
162 | $dev->setModel(trim($data[1], "\" \r\t")); |
||
163 | } |
||
164 | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.4\.1\.1\.0 = INTEGER:\s(.*)/m', $result, $data)) { |
||
165 | switch (trim($data[1])) { |
||
166 | case 1: $status = "Unknown"; |
||
167 | break; |
||
168 | case 2: $status = "On Line"; |
||
169 | break; |
||
170 | case 3: $status = "On Battery"; |
||
171 | break; |
||
172 | case 4: $status = "On Smart Boost"; |
||
173 | break; |
||
174 | case 5: $status = "Timed Sleeping"; |
||
175 | break; |
||
176 | case 6: $status = "Software Bypass"; |
||
177 | break; |
||
178 | case 7: $status = "Off"; |
||
179 | break; |
||
180 | case 8: $status = "Rebooting"; |
||
181 | break; |
||
182 | case 9: $status = "Switched Bypass"; |
||
183 | break; |
||
184 | case 10:$status = "Hardware Failure Bypass"; |
||
185 | break; |
||
186 | case 11:$status = "Sleeping Until Power Returns"; |
||
187 | break; |
||
188 | case 12:$status = "On Smart Trim"; |
||
189 | break; |
||
190 | default: $status = "Unknown state (".trim($data[1]).")"; |
||
191 | break; |
||
192 | } |
||
193 | } |
||
194 | View Code Duplication | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.1\.1\.0 = INTEGER:\s(.*)/m', $result, $data)) { |
|
195 | $batstat = ""; |
||
196 | switch (trim($data[1])) { |
||
197 | case 1: $batstat = "Battery Unknown"; |
||
198 | break; |
||
199 | case 2: break; |
||
200 | case 3: $batstat = "Battery Low"; |
||
201 | break; |
||
202 | default: $batstat = "Battery Unknown (".trim($data[1]).")"; |
||
203 | break; |
||
204 | } |
||
205 | if ($batstat !== "") { |
||
206 | if ($status !== "") { |
||
207 | $status .= ", ".$batstat; |
||
208 | } else { |
||
209 | $status = $batstat; |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | View Code Duplication | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.2\.4\.0 = INTEGER:\s(.*)/m', $result, $data)) { |
|
214 | $batstat = ""; |
||
215 | switch (trim($data[1])) { |
||
216 | case 1: break; |
||
217 | case 2: $batstat = "Replace Battery"; |
||
218 | break; |
||
219 | default: $batstat = "Replace Battery (".trim($data[1]).")"; |
||
220 | break; |
||
221 | } |
||
222 | if ($batstat !== "") { |
||
223 | if ($status !== "") { |
||
224 | $status .= ", ".$batstat; |
||
225 | } else { |
||
226 | $status = $batstat; |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | if ($status !== "") { |
||
231 | $dev->setStatus(trim($status)); |
||
232 | } |
||
233 | |||
234 | View Code Duplication | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.3\.3\.1\.0 = Gauge32:\s(.*)/m', $result, $data)) { |
|
235 | $dev->setLineVoltage(trim($data[1])/10); |
||
236 | } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.3\.2\.1\.0 = Gauge32:\s(.*)/m', $result, $data)) { |
||
237 | $dev->setLineVoltage(trim($data[1])); |
||
238 | } |
||
239 | View Code Duplication | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.4\.3\.3\.0 = Gauge32:\s(.*)/m', $result, $data)) { |
|
240 | $dev->setLoad(trim($data[1])/10); |
||
241 | } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.4\.2\.3\.0 = Gauge32:\s(.*)/m', $result, $data)) { |
||
242 | $dev->setLoad(trim($data[1])); |
||
243 | } |
||
244 | View Code Duplication | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.3\.4\.0 = INTEGER:\s(.*)/m', $result, $data)) { |
|
245 | $dev->setBatteryVoltage(trim($data[1])/10); |
||
246 | } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.2\.8\.0 = INTEGER:\s(.*)/m', $result, $data)) { |
||
247 | $dev->setBatteryVoltage(trim($data[1])); |
||
248 | } |
||
249 | View Code Duplication | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.3\.1\.0 = Gauge32:\s(.*)/m', $result, $data)) { |
|
250 | $dev->setBatterCharge(trim($data[1])/10); |
||
251 | } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.2\.1\.0 = Gauge32:\s(.*)/m', $result, $data)) { |
||
252 | $dev->setBatterCharge(trim($data[1])); |
||
253 | } |
||
254 | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.2\.3\.0 = Timeticks:\s\((\d*)\)/m', $result, $data)) { |
||
255 | $dev->setTimeLeft(trim($data[1])/6000); |
||
256 | } |
||
257 | View Code Duplication | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.3\.2\.0 = Gauge32:\s(.*)/m', $result, $data)) { |
|
258 | $dev->setTemperatur(trim($data[1])/10); |
||
259 | } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.2\.2\.0 = Gauge32:\s(.*)/m', $result, $data)) { |
||
260 | $dev->setTemperatur(trim($data[1])); |
||
261 | } |
||
262 | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.1\.3\.0 = STRING:\s(.*)/m', $result, $data)) { |
||
263 | $dev->setBatteryDate(trim($data[1], "\" \r\t")); |
||
264 | } |
||
265 | View Code Duplication | if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.3\.3\.4\.0 = Gauge32:\s(.*)/m', $result, $data)) { |
|
266 | $dev->setLineFrequency(trim($data[1])/10); |
||
267 | } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.3\.2\.4\.0 = Gauge32:\s(.*)/m', $result, $data)) { |
||
268 | $dev->setLineFrequency(trim($data[1])); |
||
269 | } |
||
270 | |||
271 | $this->upsinfo->setUpsDevices($dev); |
||
272 | } |
||
273 | } |
||
274 | |||
287 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.