PowerSoftPlus::_info()   F
last analyzed

Complexity

Conditions 16
Paths 1537

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 31
nc 1537
nop 0
dl 0
loc 51
rs 2.8561
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * PowerSoftPlus class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_UPS
9
 * @author    Mieczyslaw Nalewaj <[email protected]>
10
 * @copyright 2014 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
12
 * @version   SVN: $Id: class.powersoftplus.inc.php 661 2014-06-13 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
*/
15
/**
16
 * getting ups information from powersoftplus program
17
 *
18
 * @category  PHP
19
 * @package   PSI_UPS
20
 * @author    Mieczyslaw Nalewaj <[email protected]>
21
 * @copyright 2014 phpSysInfo
22
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
23
 * @version   Release: 3.0
24
 * @link      http://phpsysinfo.sourceforge.net
25
*/
26
class PowerSoftPlus extends UPS
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
27
{
28
    /**
29
     * internal storage for all gathered data
30
     *
31
     * @var Array
32
     */
33
    private $_output = array();
34
35
    /**
36
     * get all information from all configured ups in phpsysinfo.ini and store output in internal array
37
     */
38 View Code Duplication
    public function __construct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
39
    {
40
        parent::__construct();
41
        CommonFunctions::executeProgram('powersoftplus', '-p', $temp);
42
        if (! empty($temp)) {
43
            $this->_output[] = $temp;
44
        }
45
    }
46
47
    /**
48
     * parse the input and store data in resultset for xml generation
49
     *
50
     * @return Void
51
     */
52
    private function _info()
53
    {
54
        foreach ($this->_output as $ups) {
55
56
            $dev = new UPSDevice();
57
58
            // General info
59
            $dev->setName("EVER");
60
            $dev->setMode("PowerSoftPlus");
61
            $maxpwr = 0;
62
            $load = null;
63
            if (preg_match('/^Identifier: UPS Model\s*:\s*(.*)$/m', $ups, $data)) {
64
                $dev->setModel(trim($data[1]));
65
                if (preg_match('/\s(\d*)[^\d]*$/', trim($data[1]), $number)) {
66
                    $maxpwr=$number[1]*0.65;
67
                }
68
            }
69
            if (preg_match('/^Current UPS state\s*:\s*(.*)$/m', $ups, $data)) {
70
                $dev->setStatus(trim($data[1]));
71
            }
72
            if (preg_match('/^Output load\s*:\s*(.*)\s\[\%\]$/m', $ups, $data)) {
73
               $load = trim($data[1]);
74
            }
75
            //wrong Output load issue
76
            if (($load == 0) && ($maxpwr != 0) && preg_match('/^Effective power\s*:\s*(.*)\s\[W\]$/m', $ups, $data)) {
77
                $load = 100.0*trim($data[1])/$maxpwr;
78
            }
79
            if ($load != null) {
80
                $dev->setLoad($load);
81
            }
82
            // Battery
83
            if (preg_match('/^Battery voltage\s*:\s*(.*)\s\[Volt\]$/m', $ups, $data)) {
84
                $dev->setBatteryVoltage(trim($data[1]));
0 ignored issues
show
Documentation introduced by
trim($data[1]) is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
            }
86
            if (preg_match('/^Battery state\s*:\s*(.*)$/m', $ups, $data)) {
87
                if (preg_match('/^At full capacity$/', trim($data[1]))) {
88
                    $dev->setBatterCharge(100);
89
                } elseif (preg_match('/^(Discharged)|(Depleted)$/', trim($data[1]))) {
90
                    $dev->setBatterCharge(0);
91
                }
92
            }
93
            // Line
94
            if (preg_match('/^Input voltage\s*:\s*(.*)\s\[Volt\]$/m', $ups, $data)) {
95
                $dev->setLineVoltage(trim($data[1]));
96
            }
97
            if (preg_match('/^Input frequency\s*:\s*(.*)\s\[Hz\]$/m', $ups, $data)) {
98
                $dev->setLineFrequency(trim($data[1]));
99
            }
100
            $this->upsinfo->setUpsDevices($dev);
101
        }
102
    }
103
104
    /**
105
     * get the information
106
     *
107
     * @see PSI_Interface_UPS::build()
108
     *
109
     * @return Void
110
     */
111
    public function build()
112
    {
113
        $this->_info();
114
    }
115
}
116