Conditions | 107 |
Paths | 3921 |
Total Lines | 251 |
Code Lines | 204 |
Lines | 54 |
Ratio | 21.51 % |
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 |
||
247 | public function execute() |
||
248 | { |
||
249 | if (empty($this->_filecontent)) { |
||
250 | return; |
||
251 | } |
||
252 | foreach ($this->_filecontent['info'] as $roworig) { |
||
253 | $roworig = trim($roworig); |
||
254 | if (preg_match('/^[dD]esign capacity:\s*(.*) (.*)$/', $roworig, $data)) { |
||
255 | $bat['design_capacity'] = $data[1]; |
||
256 | View Code Duplication | if (!isset($bat['capacity_unit'])) { |
|
257 | $bat['capacity_unit'] = trim($data[2]); |
||
258 | } elseif ($bat['capacity_unit'] != trim($data[2])) { |
||
259 | $bat['capacity_unit'] = "???"; |
||
260 | } |
||
261 | } elseif (preg_match('/^[lL]ast full capacity:\s*(.*) (.*)$/', $roworig, $data)) { |
||
262 | $bat['full_capacity'] = $data[1]; |
||
263 | View Code Duplication | if (!isset($bat['capacity_unit'])) { |
|
264 | $bat['capacity_unit'] = trim($data[2]); |
||
265 | } elseif ($bat['capacity_unit'] != trim($data[2])) { |
||
266 | $bat['capacity_unit'] = "???"; |
||
267 | } |
||
268 | } elseif (preg_match('/^cycle count:\s*(.*)$/', $roworig, $data) && ($data[1]>0)) { |
||
269 | $bat['cycle_count'] = $data[1]; |
||
270 | } elseif (preg_match('/^[dD]esign voltage:\s*(.*) (.*)$/', $roworig, $data)) { |
||
271 | if ($data[2]=="mV") { // uV or mV detection |
||
272 | $bat['design_voltage'] = $data[1]; |
||
273 | } else { |
||
274 | $bat['design_voltage'] = round($data[1]/1000); |
||
275 | } |
||
276 | } elseif (preg_match('/^battery type:\s*(.*)$/', $roworig, $data)) { |
||
277 | $bat['battery_type'] = $data[1]; |
||
278 | |||
279 | } elseif (preg_match('/^POWER_SUPPLY_CYCLE_COUNT=(.*)$/', $roworig, $data) && ($data[1]>0)) { |
||
280 | $bat['cycle_count'] = $data[1]; |
||
281 | } elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_MIN_DESIGN=(.*)$/', $roworig, $data)) { |
||
282 | if ($data[1]<100000) { // uV or mV detection |
||
283 | $bat['design_voltage'] = $data[1]; |
||
284 | } else { |
||
285 | $bat['design_voltage'] = round($data[1]/1000); |
||
286 | } |
||
287 | } elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_MAX_DESIGN=(.*)$/', $roworig, $data)) { |
||
288 | if ($data[1]<100000) { // uV or mV detection |
||
289 | $bat['design_voltage_max'] = $data[1]; |
||
290 | } else { |
||
291 | $bat['design_voltage_max'] = round($data[1]/1000); |
||
292 | } |
||
293 | View Code Duplication | } elseif (preg_match('/^POWER_SUPPLY_ENERGY_FULL=(.*)$/', $roworig, $data)) { |
|
294 | $bat['full_capacity'] = ($data[1]/1000); |
||
295 | if (!isset($bat['capacity_unit'])) { |
||
296 | $bat['capacity_unit'] = "mWh"; |
||
297 | } elseif ($bat['capacity_unit'] != "mWh") { |
||
298 | $bat['capacity_unit'] = "???"; |
||
299 | } |
||
300 | } elseif (preg_match('/^POWER_SUPPLY_CHARGE_FULL=(.*)$/', $roworig, $data)) { |
||
301 | $bat['full_capacity'] = ($data[1]/1000); |
||
302 | if (!isset($bat['capacity_unit'])) { |
||
303 | $bat['capacity_unit'] = "mAh"; |
||
304 | } elseif ($bat['capacity_unit'] != "mAh") { |
||
305 | $bat['capacity_unit'] = "???"; |
||
306 | } |
||
307 | } elseif (preg_match('/^POWER_SUPPLY_ENERGY_NOW=(.*)$/', $roworig, $data)) { |
||
308 | if (!isset($bat['capacity_unit']) || ($bat['capacity_unit'] == "mWh")) { |
||
309 | $bat['capacity_unit'] = "mWh"; |
||
310 | $bat['remaining_capacity'] = ($data[1]/1000); |
||
311 | } |
||
312 | } elseif (preg_match('/^POWER_SUPPLY_CHARGE_NOW=(.*)$/', $roworig, $data)) { |
||
313 | if (!isset($bat['capacity_unit']) || ($bat['capacity_unit'] == "mAh")) { |
||
314 | $bat['capacity_unit'] = "mAh"; |
||
315 | $bat['remaining_capacity'] = ($data[1]/1000); |
||
316 | } |
||
317 | |||
318 | /* auxiary */ |
||
319 | View Code Duplication | } elseif (preg_match('/^POWER_SUPPLY_ENERGY_FULL_DESIGN=(.*)$/', $roworig, $data)) { |
|
320 | $bat['design_capacity'] = ($data[1]/1000); |
||
321 | if (!isset($bat['capacity_unit'])) { |
||
322 | $bat['capacity_unit'] = "mWh"; |
||
323 | } elseif ($bat['capacity_unit'] != "mWh") { |
||
324 | $bat['capacity_unit'] = "???"; |
||
325 | } |
||
326 | } elseif (preg_match('/^POWER_SUPPLY_CHARGE_FULL_DESIGN=(.*)$/', $roworig, $data)) { |
||
327 | $bat['design_capacity'] = ($data[1]/1000); |
||
328 | if (!isset($bat['capacity_unit'])) { |
||
329 | $bat['capacity_unit'] = "mAh"; |
||
330 | } elseif ($bat['capacity_unit'] != "mAh") { |
||
331 | $bat['capacity_unit'] = "???"; |
||
332 | } |
||
333 | } elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_NOW=(.*)$/', $roworig, $data)) { |
||
334 | if ($data[1]<100000) { // uV or mV detection |
||
335 | $bat['present_voltage'] = $data[1]; |
||
336 | } else { |
||
337 | $bat['present_voltage'] = round($data[1]/1000); |
||
338 | } |
||
339 | |||
340 | } elseif (preg_match('/^POWER_SUPPLY_CAPACITY=(.*)$/', $roworig, $data)) { |
||
341 | $bat['capacity'] = $data[1]; |
||
342 | } elseif (preg_match('/^POWER_SUPPLY_TEMP=(.*)$/', $roworig, $data)) { |
||
343 | $bat['battery_temperature'] = $data[1]/10; |
||
344 | } elseif (preg_match('/^POWER_SUPPLY_TECHNOLOGY=(.*)$/', $roworig, $data)) { |
||
345 | $bat['battery_type'] = $data[1]; |
||
346 | } elseif (preg_match('/^POWER_SUPPLY_STATUS=(.*)$/', $roworig, $data)) { |
||
347 | $bat['charging_state'] = $data[1]; |
||
348 | } elseif (preg_match('/^POWER_SUPPLY_HEALTH=(.*)$/', $roworig, $data)) { |
||
349 | $bat['battery_condition'] = $data[1]; |
||
350 | |||
351 | /* Darwin */ |
||
352 | } elseif (preg_match('/^"MaxCapacity"\s*=\s*(.*)$/', $roworig, $data)) { |
||
353 | $bat['full_capacity'] = $data[1]; |
||
354 | } elseif (preg_match('/^"CurrentCapacity"\s*=\s*(.*)$/', $roworig, $data)) { |
||
355 | $bat['remaining_capacity'] = $data[1]; |
||
356 | } elseif (preg_match('/^"Voltage"\s*=\s*(.*)$/', $roworig, $data)) { |
||
357 | $bat['present_voltage'] = $data[1]; |
||
358 | } elseif (preg_match('/^"BatteryType"\s*=\s*"(.*)"$/', $roworig, $data)) { |
||
359 | $bat['battery_type'] = $data[1]; |
||
360 | } elseif (preg_match('/^"Temperature"\s*=\s*(.*)$/', $roworig, $data)) { |
||
361 | if ($data[1]>0) $bat['battery_temperature'] = $data[1]/100; |
||
362 | } elseif (preg_match('/^"DesignCapacity"\s*=\s*(.*)$/', $roworig, $data)) { |
||
363 | $bat['design_capacity'] = $data[1]; |
||
364 | } elseif (preg_match('/^"CycleCount"\s*=\s*(.*)$/', $roworig, $data) && ($data[1]>0)) { |
||
365 | $bat['cycle_count'] = $data[1]; |
||
366 | /* auxiary */ |
||
367 | } elseif (preg_match('/^"FullyCharged"\s*=\s*Yes$/', $roworig, $data)) { |
||
368 | $bat['charging_state_f'] = true; |
||
369 | } elseif (preg_match('/^"IsCharging"\s*=\s*Yes$/', $roworig, $data)) { |
||
370 | $bat['charging_state_i'] = true; |
||
371 | } elseif (preg_match('/^"ExternalConnected"\s*=\s*Yes$/', $roworig, $data)) { |
||
372 | $bat['charging_state_e'] = true; |
||
373 | |||
374 | /* FreeBSD */ |
||
375 | } elseif (preg_match('/^Type:\s*(.*)$/', $roworig, $data)) { |
||
376 | $bat['battery_type'] = $data[1]; |
||
377 | } elseif (preg_match('/^State:\s*(.*)$/', $roworig, $data)) { |
||
378 | $bat['charging_state'] = $data[1]; |
||
379 | } elseif (preg_match('/^Present voltage:\s*(.*) (.*)$/', $roworig, $data)) { |
||
380 | if ($data[2]=="mV") { // uV or mV detection |
||
381 | $bat['present_voltage'] = $data[1]; |
||
382 | } else { |
||
383 | $bat['present_voltage'] = round($data[1]/1000); |
||
384 | } |
||
385 | } elseif (preg_match('/^Voltage:\s*(.*) (.*)$/', $roworig, $data)) { |
||
386 | if ($data[2]=="mV") { // uV or mV detection |
||
387 | $bat['present_voltage'] = $data[1]; |
||
388 | } else { |
||
389 | $bat['present_voltage'] = round($data[1]/1000); |
||
390 | } |
||
391 | } elseif (preg_match('/^Remaining capacity:\s*(.*)%$/', $roworig, $data)) { |
||
392 | $bat['capacity'] = $data[1]; |
||
393 | |||
394 | /* OpenBSD */ |
||
395 | } elseif (preg_match('/^hw.sensors.acpibat0.volt0=(.*) VDC \(voltage\)$/', $roworig, $data)) { |
||
396 | $bat['design_voltage'] = 1000*$data[1]; |
||
397 | } elseif (preg_match('/^hw.sensors.acpibat0.volt1=(.*) VDC \(current voltage\)$/', $roworig, $data)) { |
||
398 | $bat['present_voltage'] = 1000*$data[1]; |
||
399 | View Code Duplication | } elseif (preg_match('/^hw.sensors.acpibat0.watthour0=(.*) Wh \(last full capacity\)$/', $roworig, $data)) { |
|
400 | $bat['full_capacity'] = 1000*$data[1]; |
||
401 | if (!isset($bat['capacity_unit'])) { |
||
402 | $bat['capacity_unit'] = "mWh"; |
||
403 | } elseif ($bat['capacity_unit'] != "mWh") { |
||
404 | $bat['capacity_unit'] = "???"; |
||
405 | } |
||
406 | } elseif (preg_match('/^hw.sensors.acpibat0.watthour4=(.*) Wh \(design capacity\)$/', $roworig, $data)) { |
||
407 | $bat['design_capacity'] = 1000*$data[1]; |
||
408 | if (!isset($bat['capacity_unit'])) { |
||
409 | $bat['capacity_unit'] = "mWh"; |
||
410 | } elseif ($bat['capacity_unit'] != "mWh") { |
||
411 | $bat['capacity_unit'] = "???"; |
||
412 | } |
||
413 | View Code Duplication | } elseif (preg_match('/^hw.sensors.acpibat0.watthour3=(.*) Wh \(remaining capacity\)/', $roworig, $data)) { |
|
414 | $bat['remaining_capacity'] = 1000*$data[1]; |
||
415 | if (!isset($bat['capacity_unit'])) { |
||
416 | $bat['capacity_unit'] = "mWh"; |
||
417 | } elseif ($bat['capacity_unit'] != "mWh") { |
||
418 | $bat['capacity_unit'] = "???"; |
||
419 | } |
||
420 | } elseif (preg_match('/^hw.sensors.acpibat0.raw0=.* \((.*)\)/', $roworig, $data)) { |
||
421 | $bat['charging_state'] = $data[1]; |
||
422 | } |
||
423 | } |
||
424 | foreach ($this->_filecontent['state'] as $roworig) { |
||
425 | $roworig = trim($roworig); |
||
426 | if (preg_match('/^remaining capacity:\s*(.*) (.*)$/', $roworig, $data)) { |
||
427 | View Code Duplication | if (!isset($bat['capacity_unit']) || ($bat['capacity_unit'] == trim($data[2]))) { |
|
428 | $bat['capacity_unit'] = trim($data[2]); |
||
429 | $bat['remaining_capacity'] = $data[1]; |
||
430 | } |
||
431 | } elseif (preg_match('/^present voltage:\s*(.*) (.*)$/', $roworig, $data)) { |
||
432 | if ($data[2]=="mV") { // uV or mV detection |
||
433 | $bat['present_voltage'] = $data[1]; |
||
434 | } else { |
||
435 | $bat['present_voltage'] = round($data[1]/1000); |
||
436 | } |
||
437 | } elseif (preg_match('/^charging state:\s*(.*)$/', $roworig, $data)) { |
||
438 | $bat['charging_state'] = $data[1]; |
||
439 | |||
440 | } elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_MIN_DESIGN=(.*)$/', $roworig, $data)) { |
||
441 | if ($data[1]<100000) { // uV or mV detection |
||
442 | $bat['design_voltage'] = $data[1]; |
||
443 | } else { |
||
444 | $bat['design_voltage'] = round($data[1]/1000); |
||
445 | } |
||
446 | } elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_MAX_DESIGN=(.*)$/', $roworig, $data)) { |
||
447 | if ($data[1]<100000) { // uV or mV detection |
||
448 | $bat['design_voltage_max'] = $data[1]; |
||
449 | } else { |
||
450 | $bat['design_voltage_max'] = round($data[1]/1000); |
||
451 | } |
||
452 | View Code Duplication | } elseif (preg_match('/^POWER_SUPPLY_ENERGY_FULL=(.*)$/', $roworig, $data)) { |
|
453 | $bat['full_capacity'] = ($data[1]/1000); |
||
454 | if (!isset($bat['capacity_unit'])) { |
||
455 | $bat['capacity_unit'] = "mWh"; |
||
456 | } elseif ($bat['capacity_unit'] != "mWh") { |
||
457 | $bat['capacity_unit'] = "???"; |
||
458 | } |
||
459 | } elseif (preg_match('/^POWER_SUPPLY_CHARGE_FULL=(.*)$/', $roworig, $data)) { |
||
460 | $bat['full_capacity'] = ($data[1]/1000); |
||
461 | if (!isset($bat['capacity_unit'])) { |
||
462 | $bat['capacity_unit'] = "mAh"; |
||
463 | } elseif ($bat['capacity_unit'] != "mAh") { |
||
464 | $bat['capacity_unit'] = "???"; |
||
465 | } |
||
466 | } elseif (preg_match('/^POWER_SUPPLY_ENERGY_NOW=(.*)$/', $roworig, $data)) { |
||
467 | if (!isset($bat['capacity_unit']) || ($bat['capacity_unit'] == "mWh")) { |
||
468 | $bat['capacity_unit'] = "mWh"; |
||
469 | $bat['remaining_capacity'] = ($data[1]/1000); |
||
470 | } |
||
471 | } elseif (preg_match('/^POWER_SUPPLY_CHARGE_NOW=(.*)$/', $roworig, $data)) { |
||
472 | if (!isset($bat['capacity_unit']) || ($bat['capacity_unit'] == "mAh")) { |
||
473 | $bat['capacity_unit'] = "mAh"; |
||
474 | $bat['remaining_capacity'] = ($data[1]/1000); |
||
475 | } |
||
476 | } elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_NOW=(.*)$/', $roworig, $data)) { |
||
477 | if ($data[1]<100000) { // uV or mV detection |
||
478 | $bat['present_voltage'] = $data[1]; |
||
479 | } else { |
||
480 | $bat['present_voltage'] = round($data[1]/1000); |
||
481 | } |
||
482 | |||
483 | } elseif (preg_match('/^POWER_SUPPLY_CAPACITY=(.*)$/', $roworig, $data)) { |
||
484 | $bat['capacity'] = $data[1]; |
||
485 | } elseif (preg_match('/^POWER_SUPPLY_TEMP=(.*)$/', $roworig, $data)) { |
||
486 | $bat['battery_temperature'] = $data[1]/10; |
||
487 | } elseif (preg_match('/^POWER_SUPPLY_TECHNOLOGY=(.*)$/', $roworig, $data)) { |
||
488 | $bat['battery_type'] = $data[1]; |
||
489 | } elseif (preg_match('/^POWER_SUPPLY_STATUS=(.*)$/', $roworig, $data)) { |
||
490 | $bat['charging_state'] = $data[1]; |
||
491 | } elseif (preg_match('/^POWER_SUPPLY_HEALTH=(.*)$/', $roworig, $data)) { |
||
492 | $bat['battery_condition'] = $data[1]; |
||
493 | } |
||
494 | } |
||
495 | |||
496 | if (isset($bat)) $this->_result[0] = $bat; |
||
497 | } |
||
498 | |||
580 |
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.