LMSensors::_power()   F
last analyzed

Complexity

Conditions 16
Paths 550

Size

Total Lines 58
Code Lines 34

Duplication

Lines 58
Ratio 100 %

Importance

Changes 0
Metric Value
cc 16
eloc 34
nc 550
nop 0
dl 58
loc 58
rs 3.8793
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
 * lmsensor 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.lmsensors.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * getting information from lmsensor
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 LMSensors 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
    public function __construct()
39
    {
40
        parent::__construct();
41
        switch (defined('PSI_SENSOR_LMSENSORS_ACCESS')?strtolower(PSI_SENSOR_LMSENSORS_ACCESS):'command') {
42 View Code Duplication
        case 'command':
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...
43
            if (CommonFunctions::executeProgram("sensors", "", $lines)) {
44
                // Martijn Stolk: Dirty fix for misinterpreted output of sensors,
45
                // where info could come on next line when the label is too long.
46
                $lines = str_replace(":\n", ":", $lines);
47
                $lines = str_replace("\n\n", "\n", $lines);
48
                $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
49
            }
50
            break;
51 View Code Duplication
        case 'data':
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...
52
            if (CommonFunctions::rfts(APP_ROOT.'/data/lmsensors.txt', $lines)) {
53
                $lines = str_replace(":\n", ":", $lines);
54
                $lines = str_replace("\n\n", "\n", $lines);
55
                $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
56
            }
57
            break;
58
        default:
59
            $this->error->addConfigError('__construct()', 'PSI_SENSOR_LMSENSORS_ACCESS');
60
            break;
61
        }
62
    }
63
64
    /**
65
     * get temperature information
66
     *
67
     * @return void
68
     */
69
    private function _temperature()
70
    {
71
        $ar_buf = array();
72
        foreach ($this->_lines as $line) {
73
            $data = array();
74
            if (preg_match("/(.*):(.*)\((.*)=(.*),(.*)=(.*)\)(.*)/", $line, $data)) {
75
                ;
76
            } elseif (preg_match("/(.*):(.*)\((.*)=(.*)\)(.*)/", $line, $data)) {
77
                ;
78
            } else {
79
                preg_match("/(.*):(.*)/", $line, $data);
80
            }
81
            if (count($data) > 1) {
82
                $temp = substr(trim($data[2]), -1);
83
                switch ($temp) {
84
                case "C":
85
//                case "F":
86
                    array_push($ar_buf, $line);
87
                }
88
            }
89
        }
90
        foreach ($ar_buf as $line) {
91
            $data = array();
92 View Code Duplication
            if (preg_match("/(.*):(.*).C[ ]*\((.*)=(.*).C,(.*)=(.*).C\)(.*)\)/", $line, $data)) {
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...
93
                ;
94
            } elseif (preg_match("/(.*):(.*).C[ ]*\((.*)=(.*).C,(.*)=(.*).C\)(.*)/", $line, $data)) {
95
                ;
96
            } elseif (preg_match("/(.*):(.*).C[ ]*\((.*)=(.*).C\)(.*)/", $line, $data)) {
97
                ;
98
            } elseif (preg_match("/(.*):(.*).C[ \t]+/", $line, $data)) {
99
                ;
100
            } else {
101
                preg_match("/(.*):(.*).C$/", $line, $data);
102
            }
103 View Code Duplication
            foreach ($data as $key=>$value) {
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...
104
                if (preg_match("/^\+?(-?[0-9\.]+).?$/", trim($value), $newvalue)) {
105
                    $data[$key] = 0+trim($newvalue[1]);
106
                } else {
107
                    $data[$key] = trim($value);
108
                }
109
            }
110
            $dev = new SensorDevice();
111
112
            if (strlen($data[1]) == 4) {
113
                if ($data[1][0] == "T") {
114
115
                    if ($data[1][1] == "A") {
116
                        $data[1] = $data[1] . " Ambient";
117
                    } elseif ($data[1][1] == "C") {
118
                        $data[1] = $data[1] . " CPU";
119
                    } elseif ($data[1][1] == "G") {
120
                        $data[1] = $data[1] . " GPU";
121
                    } elseif ($data[1][1] == "H") {
122
                        $data[1] = $data[1] . " Harddisk";
123
                    } elseif ($data[1][1] == "L") {
124
                        $data[1] = $data[1] . " LCD";
125
                    } elseif ($data[1][1] == "O") {
126
                        $data[1] = $data[1] . " ODD";
127
                    } elseif ($data[1][1] == "B") {
128
                        $data[1] = $data[1] . " Battery";
129
                    }
130
131
                    if ($data[1][3] == "H") {
132
                        $data[1] = $data[1] . " Heatsink";
133
                    } elseif ($data[1][3] == "P") {
134
                        $data[1] = $data[1] . " Proximity";
135
                    } elseif ($data[1][3] == "D") {
136
                        $data[1] = $data[1] . " Die";
137
                    }
138
                }
139
            }
140
141
            $dev->setName($data[1]);
142
            $dev->setValue($data[2]);
143
144
            if (isset($data[6]) && $data[2] <= $data[6]) {
145
                  $dev->setMax(max($data[4], $data[6]));
146
            } elseif (isset($data[4]) && $data[2] <= $data[4]) {
147
                   $dev->setMax($data[4]);
148
            }
149
            if (preg_match("/\sALARM(\s*)$/", $line)) {
150
                $dev->setEvent("Alarm");
151
            }
152
            $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...
153
        }
154
    }
155
156
    /**
157
     * get fan information
158
     *
159
     * @return void
160
     */
161
    private function _fans()
162
    {
163
        $ar_buf = array();
164
        foreach ($this->_lines as $line) {
165
            $data = array();
166
            if (preg_match("/(.*):(.*)\((.*)=(.*),(.*)=(.*)\)(.*)/", $line, $data)) {
167
                ;
168
            } elseif (preg_match("/(.*):(.*)\((.*)=(.*)\)(.*)/", $line, $data)) {
169
                ;
170
            } else {
171
                preg_match("/(.*):(.*)/", $line, $data);
172
            }
173
            if (count($data) > 1) {
174
                $temp = substr(trim($data[2]), -4);
175
                switch ($temp) {
176
                case " RPM":
177
                    array_push($ar_buf, $line);
178
                }
179
            }
180
        }
181
        foreach ($ar_buf as $line) {
182
            $data = array();
183 View Code Duplication
            if (preg_match("/(.*):(.*) RPM[ ]*\((.*)=(.*) RPM,(.*)=(.*)\)(.*)\)/", $line, $data)) {
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...
184
                ;
185
            } elseif (preg_match("/(.*):(.*) RPM[ ]*\((.*)=(.*) RPM,(.*)=(.*)\)(.*)/", $line, $data)) {
186
                ;
187
            } elseif (preg_match("/(.*):(.*) RPM[ ]*\((.*)=(.*) RPM\)(.*)/", $line, $data)) {
188
                ;
189
            } elseif (preg_match("/(.*):(.*) RPM[ \t]+/", $line, $data)) {
190
                ;
191
            } else {
192
                preg_match("/(.*):(.*) RPM$/", $line, $data);
193
            }
194
            $dev = new SensorDevice();
195
            $dev->setName(trim($data[1]));
196
            $dev->setValue(trim($data[2]));
197
            if (isset($data[4])) {
198
                $dev->setMin(trim($data[4]));
199
            }
200
            if (preg_match("/\sALARM(\s*)$/", $line)) {
201
                $dev->setEvent("Alarm");
202
            }
203
            $this->mbinfo->setMbFan($dev);
204
        }
205
    }
206
207
    /**
208
     * get voltage information
209
     *
210
     * @return void
211
     */
212
    private function _voltage()
213
    {
214
        $ar_buf = array();
215
        foreach ($this->_lines as $line) {
216
            $data = array();
217
            if (preg_match("/(.*):(.*)\((.*)=(.*),(.*)=(.*)\)(.*)/", $line, $data)) {
218
                ;
219
            } elseif (preg_match("/(.*):(.*)\(/", $line, $data)) {
220
                ;
221
            } else {
222
                preg_match("/(.*):(.*)/", $line, $data);
223
            }
224
            if (count($data) > 1) {
225
                $temp = substr(trim($data[2]), -2);
226
                switch ($temp) {
227
                case " V":
228
                    array_push($ar_buf, $line);
229
                }
230
            }
231
        }
232
        foreach ($ar_buf as $line) {
233
            $data = array();
234 View Code Duplication
            if (preg_match("/(.*):(.*) V[ ]*\((.*)=(.*) V,(.*)=(.*) V\)(.*)\)/", $line, $data)) {
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...
235
                ;
236
            } elseif (preg_match("/(.*):(.*) V[ ]*\((.*)=(.*) V,(.*)=(.*) V\)(.*)/", $line, $data)) {
237
                ;
238
            } elseif (preg_match("/(.*):(.*) V[ \t]+/", $line, $data)) {
239
                ;
240
            } else {
241
                preg_match("/(.*):(.*) V$/", $line, $data);
242
            }
243 View Code Duplication
            foreach ($data as $key=>$value) {
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...
244
                if (preg_match("/^\+?(-?[0-9\.]+)$/", trim($value), $newvalue)) {
245
                    $data[$key] = 0+trim($newvalue[1]);
246
                } else {
247
                    $data[$key] = trim($value);
248
                }
249
            }
250
            if (isset($data[1])) {
251
                $dev = new SensorDevice();
252
                $dev->setName($data[1]);
253
                $dev->setValue($data[2]);
254
                if (isset($data[4])) {
255
                    $dev->setMin($data[4]);
256
                }
257
                if (isset($data[6])) {
258
                    $dev->setMax($data[6]);
259
                }
260
                if (preg_match("/\sALARM(\s*)$/", $line)) {
261
                    $dev->setEvent("Alarm");
262
                }
263
                $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...
264
            }
265
        }
266
    }
267
268
    /**
269
     * get power information
270
     *
271
     * @return void
272
     */
273 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...
274
    {
275
        $ar_buf = array();
276
        foreach ($this->_lines as $line) {
277
            $data = array();
278
            if (preg_match("/(.*):(.*)\((.*)=(.*),(.*)=(.*)\)(.*)/", $line, $data)) {
279
                ;
280
            } elseif (preg_match("/(.*):(.*)\((.*)=(.*)\)(.*)/", $line, $data)) {
281
                ;
282
            } else {
283
                preg_match("/(.*):(.*)/", $line, $data);
284
            }
285
            if (count($data) > 1) {
286
                $temp = substr(trim($data[2]), -2);
287
                switch ($temp) {
288
                case " W":
289
                    array_push($ar_buf, $line);
290
                }
291
            }
292
        }
293
        foreach ($ar_buf as $line) {
294
            $data = array();
295
/* not tested yet
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
296
            if (preg_match("/(.*):(.*) W[ ]*\((.*)=(.*) W,(.*)=(.*) W\)(.*)\)/", $line, $data)) {
297
                ;
298
            } elseif (preg_match("/(.*):(.*) W[ ]*\((.*)=(.*) W,(.*)=(.*) W\)(.*)/", $line, $data)) {
299
                ;
300
            } else
301
*/
302
            if (preg_match("/(.*):(.*) W[ ]*\((.*)=(.*) W\)(.*)/", $line, $data)) {
303
                ;
304
            } elseif (preg_match("/(.*):(.*) W[ \t]+/", $line, $data)) {
305
                ;
306
            } else {
307
                preg_match("/(.*):(.*) W$/", $line, $data);
308
            }
309
            foreach ($data as $key=>$value) {
310
                if (preg_match("/^\+?([0-9\.]+).?$/", trim($value), $newvalue)) {
311
                    $data[$key] = trim($newvalue[1]);
312
                } else {
313
                    $data[$key] = trim($value);
314
                }
315
            }
316
            $dev = new SensorDevice();
317
            $dev->setName($data[1]);
318
            $dev->setValue($data[2]);
319
320
            if (isset($data[6]) && $data[2] <= $data[6]) {
321
                  $dev->setMax(max($data[4], $data[6]));
322
            } elseif (isset($data[4]) && $data[2] <= $data[4]) {
323
                   $dev->setMax($data[4]);
324
            }
325
            if (preg_match("/\sALARM(\s*)$/", $line)) {
326
                $dev->setEvent("Alarm");
327
            }
328
            $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...
329
        }
330
    }
331
332
    /**
333
     * get current information
334
     *
335
     * @return void
336
     */
337 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...
338
    {
339
        $ar_buf = array();
340
        foreach ($this->_lines as $line) {
341
            $data = array();
342
            if (preg_match("/(.*):(.*)\((.*)=(.*),(.*)=(.*)\)(.*)/", $line, $data)) {
343
                ;
344
            } elseif (preg_match("/(.*):(.*)\((.*)=(.*)\)(.*)/", $line, $data)) {
345
                ;
346
            } else {
347
                preg_match("/(.*):(.*)/", $line, $data);
348
            }
349
            if (count($data) > 1) {
350
                $temp = substr(trim($data[2]), -2);
351
                switch ($temp) {
352
                case " A":
353
                    array_push($ar_buf, $line);
354
                }
355
            }
356
        }
357
        foreach ($ar_buf as $line) {
358
            $data = array();
359
/* not tested yet
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
360
            if (preg_match("/(.*):(.*) A[ ]*\((.*)=(.*) A,(.*)=(.*) A\)(.*)\)/", $line, $data)) {
361
                ;
362
            } elseif (preg_match("/(.*):(.*) A[ ]*\((.*)=(.*) A,(.*)=(.*) A\)(.*)/", $line, $data)) {
363
                ;
364
            } else
365
*/
366
            if (preg_match("/(.*):(.*) A[ ]*\((.*)=(.*) A\)(.*)/", $line, $data)) {
367
                ;
368
            } elseif (preg_match("/(.*):(.*) A[ \t]+/", $line, $data)) {
369
                ;
370
            } else {
371
                preg_match("/(.*):(.*) A$/", $line, $data);
372
            }
373
            foreach ($data as $key=>$value) {
374
                if (preg_match("/^\+?([0-9\.]+).?$/", trim($value), $newvalue)) {
375
                    $data[$key] = trim($newvalue[1]);
376
                } else {
377
                    $data[$key] = trim($value);
378
                }
379
            }
380
            $dev = new SensorDevice();
381
            $dev->setName($data[1]);
382
            $dev->setValue($data[2]);
383
384
            if (isset($data[6]) && $data[2] <= $data[6]) {
385
                  $dev->setMax(max($data[4], $data[6]));
386
            } elseif (isset($data[4]) && $data[2] <= $data[4]) {
387
                   $dev->setMax($data[4]);
388
            }
389
            if (preg_match("/\sALARM(\s*)$/", $line)) {
390
                $dev->setEvent("Alarm");
391
            }
392
            $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...
393
        }
394
    }
395
396
    /**
397
     * get the information
398
     *
399
     * @see PSI_Interface_Sensor::build()
400
     *
401
     * @return Void
402
     */
403
    public function build()
404
    {
405
        $this->_temperature();
406
        $this->_voltage();
407
        $this->_fans();
408
        $this->_power();
409
        $this->_current();
410
    }
411
}
412