FreeIPMI   B
last analyzed

Complexity

Total Complexity 44

Size/Duplication

Total Lines 152
Duplicated Lines 42.76 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 65
loc 152
rs 8.3396
c 0
b 0
f 0
wmc 44
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 18 18 5
B _temperature() 14 14 7
B _voltage() 0 15 8
B _fans() 5 18 9
B _power() 14 14 7
B _current() 14 14 7
A build() 0 8 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like FreeIPMI often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FreeIPMI, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * freeipmi sensor class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_Sensor
9
 * @author    Michael Cramer <[email protected]>
10
 * @copyright 2009 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
12
 * @version   SVN: $Id: class.freeipmi.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * getting information from ipmi-sensors
17
 *
18
 * @category  PHP
19
 * @package   PSI_Sensor
20
 * @author    Michael Cramer <[email protected]>
21
 * @copyright 2009 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 FreeIPMI extends Sensors
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
     * content to parse
30
     *
31
     * @var array
32
     */
33
    private $_lines = array();
34
35
    /**
36
     * fill the private content var through command or data access
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
        switch (defined('PSI_SENSOR_FREEIPMI_ACCESS')?strtolower(PSI_SENSOR_FREEIPMI_ACCESS):'command') {
42
        case 'command':
43
            CommonFunctions::executeProgram('ipmi-sensors', '--output-sensor-thresholds', $lines);
44
            $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
45
            break;
46
        case 'data':
47
            if (CommonFunctions::rfts(APP_ROOT.'/data/freeipmi.txt', $lines)) {
48
                $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
49
            }
50
            break;
51
        default:
52
            $this->error->addConfigError('__construct()', 'PSI_SENSOR_FREEIPMI_ACCESS');
53
            break;
54
        }
55
    }
56
57
    /**
58
     * get temperature information
59
     *
60
     * @return void
61
     */
62 View Code Duplication
    private function _temperature()
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...
63
    {
64
        foreach ($this->_lines as $line) {
65
            $buffer = preg_split("/\s*\|\s*/", $line);
66
            if ($buffer[2] == "Temperature" && $buffer[11] != "N/A" && $buffer[4] == "C") {
67
                $dev = new SensorDevice();
68
                $dev->setName($buffer[1]);
69
                $dev->setValue($buffer[3]);
70
                if ($buffer[9] != "N/A") $dev->setMax($buffer[9]);
71
                if ($buffer[11] != "'OK'") $dev->setEvent(trim($buffer[11], "'"));
72
                $this->mbinfo->setMbTemp($dev);
0 ignored issues
show
Documentation introduced by
$dev is of type object<SensorDevice>, but the function expects a object<Sensor>.

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...
73
            }
74
        }
75
    }
76
77
    /**
78
     * get voltage information
79
     *
80
     * @return void
81
     */
82
    private function _voltage()
83
    {
84
        foreach ($this->_lines as $line) {
85
            $buffer = preg_split("/\s*\|\s*/", $line);
86
            if ($buffer[2] == "Voltage" && $buffer[11] != "N/A" && $buffer[4] == "V") {
87
                $dev = new SensorDevice();
88
                $dev->setName($buffer[1]);
89
                $dev->setValue($buffer[3]);
90
                if ($buffer[6] != "N/A") $dev->setMin($buffer[6]);
91
                if ($buffer[9] != "N/A") $dev->setMax($buffer[9]);
92
                if ($buffer[11] != "'OK'") $dev->setEvent(trim($buffer[11], "'"));
93
                $this->mbinfo->setMbVolt($dev);
0 ignored issues
show
Documentation introduced by
$dev is of type object<SensorDevice>, but the function expects a object<Sensor>.

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...
94
            }
95
        }
96
    }
97
98
    /**
99
     * get fan information
100
     *
101
     * @return void
102
     */
103
    private function _fans()
104
    {
105
        foreach ($this->_lines as $line) {
106
            $buffer = preg_split("/\s*\|\s*/", $line);
107
            if ($buffer[2] == "Fan" && $buffer[11] != "N/A" && $buffer[4] == "RPM") {
108
                $dev = new SensorDevice();
109
                $dev->setName($buffer[1]);
110
                $dev->setValue($buffer[3]);
111 View Code Duplication
                if ($buffer[6] != "N/A") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
112
                    $dev->setMin($buffer[6]);
113
                } elseif (($buffer[9] != "N/A") && ($buffer[9]<$buffer[3])) { //max instead min issue
114
                    $dev->setMin($buffer[9]);
115
                }
116
                if ($buffer[11] != "'OK'") $dev->setEvent(trim($buffer[11], "'"));
117
                $this->mbinfo->setMbFan($dev);
118
            }
119
        }
120
    }
121
122
    /**
123
     * get power information
124
     *
125
     * @return void
126
     */
127 View Code Duplication
    private function _power()
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...
128
    {
129
        foreach ($this->_lines as $line) {
130
            $buffer = preg_split("/\s*\|\s*/", $line);
131
            if ($buffer[2] == "Current" && $buffer[11] != "N/A" && $buffer[4] == "W") {
132
                $dev = new SensorDevice();
133
                $dev->setName($buffer[1]);
134
                $dev->setValue($buffer[3]);
135
                if ($buffer[9] != "N/A") $dev->setMax($buffer[9]);
136
                if ($buffer[11] != "'OK'") $dev->setEvent(trim($buffer[11], "'"));
137
                $this->mbinfo->setMbPower($dev);
0 ignored issues
show
Documentation introduced by
$dev is of type object<SensorDevice>, but the function expects a object<Sensor>.

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...
138
            }
139
        }
140
    }
141
142
    /**
143
     * get current information
144
     *
145
     * @return void
146
     */
147 View Code Duplication
    private function _current()
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...
148
    {
149
        foreach ($this->_lines as $line) {
150
            $buffer = preg_split("/\s*\|\s*/", $line);
151
            if ($buffer[2] == "Current" && $buffer[11] != "N/A" && $buffer[4] == "A") {
152
                $dev = new SensorDevice();
153
                $dev->setName($buffer[1]);
154
                $dev->setValue($buffer[3]);
155
                if ($buffer[9] != "N/A") $dev->setMax($buffer[9]);
156
                if ($buffer[11] != "'OK'") $dev->setEvent(trim($buffer[11], "'"));
157
                $this->mbinfo->setMbCurrent($dev);
0 ignored issues
show
Documentation introduced by
$dev is of type object<SensorDevice>, but the function expects a object<Sensor>.

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...
158
            }
159
        }
160
    }
161
162
    /**
163
     * get the information
164
     *
165
     * @see PSI_Interface_Sensor::build()
166
     *
167
     * @return Void
168
     */
169
    public function build()
170
    {
171
        $this->_temperature();
172
        $this->_voltage();
173
        $this->_fans();
174
        $this->_power();
175
        $this->_current();
176
    }
177
}
178