Hwmon   D
last analyzed

Complexity

Total Complexity 96

Size/Duplication

Total Lines 230
Duplicated Lines 45.22 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 104
loc 230
rs 4.8717
c 0
b 0
f 0
wmc 96
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
D _temperature() 34 34 18
D _voltage() 35 35 18
D _fans() 0 38 21
D _power() 0 35 18
D _current() 35 35 18
A build() 0 13 3

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 Hwmon 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 Hwmon, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * hwmon 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.hwmon.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * getting hardware temperature information through sysctl
17
 *
18
 * @category  PHP
19
 * @package   PSI_Sensor
20
 * @author    Michael Cramer <[email protected]>
21
 * @author    William Johansson <[email protected]>
22
 * @copyright 2009 phpSysInfo
23
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
24
 * @version   Release: 3.0
25
 * @link      http://phpsysinfo.sourceforge.net
26
 */
27
class Hwmon 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...
28
{
29
    /**
30
     * get temperature information
31
     *
32
     * @return void
33
     */
34 View Code Duplication
    private function _temperature($hwpath)
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...
35
    {
36
       $sensor = glob($hwpath."temp*_input", GLOB_NOSORT);
37
       if (($total = count($sensor)) > 0) {
38
            $buf = "";
39
            for ($i = 0; $i < $total; $i++) if (CommonFunctions::rfts($sensor[$i], $buf, 1, 4096, false) && (trim($buf) != "")) {
40
                $dev = new SensorDevice();
41
                $dev->setValue(trim($buf)/1000);
42
                $label = preg_replace("/_input$/", "_label", $sensor[$i]);
43
                $crit = preg_replace("/_input$/", "_crit", $sensor[$i]);
44
                $max = preg_replace("/_input$/", "_max", $sensor[$i]);
45
                $crit_alarm = preg_replace("/_input$/", "_crit_alarm", $sensor[$i]);
46
                if (CommonFunctions::fileexists($label) && CommonFunctions::rfts($label, $buf, 1, 4096, false) && (trim($buf) != "")) {
47
                    $dev->setName(trim($buf));
48
                } else {
49
                    $labelname = trim(preg_replace("/_input$/", "", pathinfo($sensor[$i], PATHINFO_BASENAME)));
50
                    if ($labelname !== "") {
51
                        $dev->setName($labelname);
52
                    } else {
53
                        $dev->setName('unknown');
54
                    }
55
                }
56
                if (CommonFunctions::fileexists($crit) && CommonFunctions::rfts($crit, $buf, 1, 4096, false) && (trim($buf) != "")) {
57
                    $dev->setMax(trim($buf)/1000);
58
                    if (CommonFunctions::fileexists($crit_alarm) && CommonFunctions::rfts($crit_alarm, $buf, 1, 4096, false) && (trim($buf) === "1")) {
59
                        $dev->setEvent("Critical Alarm");
60
                    }
61
                } elseif (CommonFunctions::fileexists($max) && CommonFunctions::rfts($max, $buf, 1, 4096, false) && (trim($buf) != "")) {
62
                    $dev->setMax(trim($buf)/1000);
63
                }
64
                $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...
65
            }
66
        }
67
    }
68
69
    /**
70
     * get voltage information
71
     *
72
     * @return void
73
     */
74 View Code Duplication
    private function _voltage($hwpath)
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...
75
    {
76
       $sensor = glob($hwpath."in*_input", GLOB_NOSORT);
77
       if (($total = count($sensor)) > 0) {
78
            $buf = "";
79
            for ($i = 0; $i < $total; $i++) if (CommonFunctions::rfts($sensor[$i], $buf, 1, 4096, false) && (trim($buf) != "")) {
80
                $dev = new SensorDevice();
81
                $dev->setValue(trim($buf)/1000);
82
                $label = preg_replace("/_input$/", "_label", $sensor[$i]);
83
                $alarm = preg_replace("/_input$/", "_alarm", $sensor[$i]);
84
                $max = preg_replace("/_input$/", "_max", $sensor[$i]);
85
                $min = preg_replace("/_input$/", "_min", $sensor[$i]);
86
                if (CommonFunctions::fileexists($label) && CommonFunctions::rfts($label, $buf, 1, 4096, false) && (trim($buf) != "")) {
87
                    $dev->setName(trim($buf));
88
                } else {
89
                    $labelname = trim(preg_replace("/_input$/", "", pathinfo($sensor[$i], PATHINFO_BASENAME)));
90
                    if ($labelname !== "") {
91
                        $dev->setName($labelname);
92
                    } else {
93
                        $dev->setName('unknown');
94
                    }
95
                }
96
                if (CommonFunctions::fileexists($max) && CommonFunctions::rfts($max, $buf, 1, 4096, false) && (trim($buf) != "")) {
97
                    $dev->setMax(trim($buf)/1000);
98
                }
99
                if (CommonFunctions::fileexists($min) && CommonFunctions::rfts($min, $buf, 1, 4096, false) && (trim($buf) != "")) {
100
                    $dev->setMin(trim($buf)/1000);
101
                }
102
                if (CommonFunctions::fileexists($alarm) && CommonFunctions::rfts($alarm, $buf, 1, 4096, false) && (trim($buf) === "1")) {
103
                    $dev->setEvent("Alarm");
104
                }
105
                $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...
106
            }
107
        }
108
    }
109
110
    /**
111
     * get fan information
112
     *
113
     * @return void
114
     */
115
    private function _fans($hwpath)
116
    {
117
       $sensor = glob($hwpath."fan*_input", GLOB_NOSORT);
118
       if (($total = count($sensor)) > 0) {
119
            $buf = "";
120
            for ($i = 0; $i < $total; $i++) if (CommonFunctions::rfts($sensor[$i], $buf, 1, 4096, false) && (trim($buf) != "")) {
121
                $dev = new SensorDevice();
122
                $dev->setValue(trim($buf));
123
                $label = preg_replace("/_input$/", "_label", $sensor[$i]);
124
                $alarm = preg_replace("/_input$/", "_alarm", $sensor[$i]);
125
                $fullmax = preg_replace("/_input$/", "_full_speed", $sensor[$i]);
126
                $max = preg_replace("/_input$/", "_max", $sensor[$i]);
127
                $min = preg_replace("/_input$/", "_min", $sensor[$i]);
128
                if (CommonFunctions::fileexists($label) && CommonFunctions::rfts($label, $buf, 1, 4096, false) && (trim($buf) != "")) {
129
                    $dev->setName(trim($buf));
130
                } else {
131
                    $labelname = trim(preg_replace("/_input$/", "", pathinfo($sensor[$i], PATHINFO_BASENAME)));
132
                    if ($labelname !== "") {
133
                        $dev->setName($labelname);
134
                    } else {
135
                        $dev->setName('unknown');
136
                    }
137
                }
138
                if (CommonFunctions::fileexists($fullmax) && CommonFunctions::rfts($fullmax, $buf, 1, 4096, false) && (trim($buf) != "")) {
139
                    $dev->setMax(trim($buf));
140
                } elseif (CommonFunctions::fileexists($max) && CommonFunctions::rfts($max, $buf, 1, 4096, false) && (trim($buf) != "")) {
141
                    $dev->setMax(trim($buf));
142
                }
143
                if (CommonFunctions::fileexists($min) && CommonFunctions::rfts($min, $buf, 1, 4096, false) && (trim($buf) != "")) {
144
                    $dev->setMin(trim($buf));
145
                }
146
                if (CommonFunctions::fileexists($alarm) && CommonFunctions::rfts($alarm, $buf, 1, 4096, false) && (trim($buf) === "1")) {
147
                    $dev->setEvent("Alarm");
148
                }
149
                $this->mbinfo->setMbFan($dev);
150
            }
151
        }
152
    }
153
154
    /**
155
     * get power information
156
     *
157
     * @return void
158
     */
159
    private function _power($hwpath)
160
    {
161
       $sensor = glob($hwpath."power*_input", GLOB_NOSORT);
162
       if (($total = count($sensor)) > 0) {
163
            $buf = "";
164
            for ($i = 0; $i < $total; $i++) if (CommonFunctions::rfts($sensor[$i], $buf, 1, 4096, false) && (trim($buf) != "")) {
165
                $dev = new SensorDevice();
166
                $dev->setValue(trim($buf));
167
                $label = preg_replace("/_input$/", "_label", $sensor[$i]);
168
                $alarm = preg_replace("/_input$/", "_alarm", $sensor[$i]);
169
                $max = preg_replace("/_input$/", "_max", $sensor[$i]);
170
                $min = preg_replace("/_input$/", "_min", $sensor[$i]);
171
                if (CommonFunctions::fileexists($label) && CommonFunctions::rfts($label, $buf, 1, 4096, false) && (trim($buf) != "")) {
172
                    $dev->setName(trim($buf));
173
                } else {
174
                    $labelname = trim(preg_replace("/_input$/", "", pathinfo($sensor[$i], PATHINFO_BASENAME)));
175
                    if ($labelname !== "") {
176
                        $dev->setName($labelname);
177
                    } else {
178
                        $dev->setName('unknown');
179
                    }
180
                }
181
                if (CommonFunctions::fileexists($max) && CommonFunctions::rfts($max, $buf, 1, 4096, false) && (trim($buf) != "")) {
182
                    $dev->setMax(trim($buf));
183
                }
184
                if (CommonFunctions::fileexists($min) && CommonFunctions::rfts($min, $buf, 1, 4096, false) && (trim($buf) != "")) {
185
                    $dev->setMin(trim($buf));
186
                }
187
                if (CommonFunctions::fileexists($alarm) && CommonFunctions::rfts($alarm, $buf, 1, 4096, false) && (trim($buf) === "1")) {
188
                    $dev->setEvent("Alarm");
189
                }
190
                $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...
191
            }
192
        }
193
    }
194
195
    /**
196
     * get current information
197
     *
198
     * @return void
199
     */
200 View Code Duplication
    private function _current($hwpath)
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...
201
    {
202
       $sensor = glob($hwpath."curr*_input", GLOB_NOSORT);
203
       if (($total = count($sensor)) > 0) {
204
            $buf = "";
205
            for ($i = 0; $i < $total; $i++) if (CommonFunctions::rfts($sensor[$i], $buf, 1, 4096, false) && (trim($buf) != "")) {
206
                $dev = new SensorDevice();
207
                $dev->setValue(trim($buf)/1000);
208
                $label = preg_replace("/_input$/", "_label", $sensor[$i]);
209
                $alarm = preg_replace("/_input$/", "_alarm", $sensor[$i]);
210
                $max = preg_replace("/_input$/", "_max", $sensor[$i]);
211
                $min = preg_replace("/_input$/", "_min", $sensor[$i]);
212
                if (CommonFunctions::fileexists($label) && CommonFunctions::rfts($label, $buf, 1, 4096, false) && (trim($buf) != "")) {
213
                    $dev->setName(trim($buf));
214
                } else {
215
                    $labelname = trim(preg_replace("/_input$/", "", pathinfo($sensor[$i], PATHINFO_BASENAME)));
216
                    if ($labelname !== "") {
217
                        $dev->setName($labelname);
218
                    } else {
219
                        $dev->setName('unknown');
220
                    }
221
                }
222
                if (CommonFunctions::fileexists($max) && CommonFunctions::rfts($max, $buf, 1, 4096, false) && (trim($buf) != "")) {
223
                    $dev->setMax(trim($buf)/1000);
224
                }
225
                if (CommonFunctions::fileexists($min) && CommonFunctions::rfts($min, $buf, 1, 4096, false) && (trim($buf) != "")) {
226
                    $dev->setMin(trim($buf)/1000);
227
                }
228
                if (CommonFunctions::fileexists($alarm) && CommonFunctions::rfts($alarm, $buf, 1, 4096, false) && (trim($buf) === "1")) {
229
                    $dev->setEvent("Alarm");
230
                }
231
                $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...
232
            }
233
        }
234
    }
235
236
    /**
237
     * get the information
238
     *
239
     * @see PSI_Interface_Sensor::build()
240
     *
241
     * @return Void
242
     */
243
    public function build()
244
    {
245
       $hwpaths = glob("/sys/class/hwmon/hwmon*/device/", GLOB_NOSORT);
246
       if (($totalh = count($hwpaths)) > 0) {
247
            for ($h = 0; $h < $totalh; $h++) {
248
                $this->_temperature($hwpaths[$h]);
249
                $this->_voltage($hwpaths[$h]);
250
                $this->_fans($hwpaths[$h]);
251
                $this->_power($hwpaths[$h]);
252
                $this->_current($hwpaths[$h]);
253
            }
254
       }
255
    }
256
}
257