MBMon::_fans()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 0
dl 14
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * mbmon 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.mbmon.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * getting information from mbmon
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 MBMon 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 tcp, command or data access
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
        switch (defined('PSI_SENSOR_MBMON_ACCESS')?strtolower(PSI_SENSOR_MBMON_ACCESS):'command') {
42
        case 'tcp':
43
            $fp = fsockopen("localhost", 411, $errno, $errstr, 5);
44
            if ($fp) {
45
                $lines = "";
46
                while (!feof($fp)) {
47
                    $lines .= fread($fp, 1024);
48
                }
49
                $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
50
            } else {
51
                $this->error->addError("fsockopen()", $errno." ".$errstr);
52
            }
53
            break;
54
        case 'command':
55
            CommonFunctions::executeProgram('mbmon', '-c 1 -r', $lines, PSI_DEBUG);
56
            $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
57
            break;
58
        case 'data':
59
            if (CommonFunctions::rfts(APP_ROOT.'/data/mbmon.txt', $lines)) {
60
                $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
61
            }
62
            break;
63
        default:
64
            $this->error->addConfigError('__construct()', 'PSI_SENSOR_MBMON_ACCESS');
65
            break;
66
        }
67
    }
68
69
    /**
70
     * get temperature information
71
     *
72
     * @return void
73
     */
74 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...
75
    {
76
        foreach ($this->_lines as $line) {
77
            if (preg_match('/^(TEMP\d*)\s*:\s*(.*)$/D', $line, $data)) {
78
                if ($data[2] <> '0') {
79
                    $dev = new SensorDevice();
80
                    $dev->setName($data[1]);
81
//                    $dev->setMax(70);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
82
                    if ($data[2] < 250) {
83
                        $dev->setValue($data[2]);
84
                    }
85
                    $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...
86
                }
87
            }
88
        }
89
    }
90
91
    /**
92
     * get fan information
93
     *
94
     * @return void
95
     */
96 View Code Duplication
    private function _fans()
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...
97
    {
98
        foreach ($this->_lines as $line) {
99
            if (preg_match('/^(FAN\d*)\s*:\s*(.*)$/D', $line, $data)) {
100
                if ($data[2] <> '0') {
101
                    $dev = new SensorDevice();
102
                    $dev->setName($data[1]);
103
                    $dev->setValue($data[2]);
104
//                    $dev->setMax(3000);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
105
                    $this->mbinfo->setMbFan($dev);
106
                }
107
            }
108
        }
109
    }
110
111
    /**
112
     * get voltage information
113
     *
114
     * @return void
115
     */
116 View Code Duplication
    private function _voltage()
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...
117
    {
118
        foreach ($this->_lines as $line) {
119
            if (preg_match('/^(V.*)\s*:\s*(.*)$/D', $line, $data)) {
120
                if ($data[2] <> '+0.00') {
121
                    $dev = new SensorDevice();
122
                    $dev->setName($data[1]);
123
                    $dev->setValue($data[2]);
124
                    $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...
125
                }
126
            }
127
        }
128
    }
129
130
    /**
131
     * get the information
132
     *
133
     * @see PSI_Interface_Sensor::build()
134
     *
135
     * @return void
136
     */
137
    public function build()
138
    {
139
        $this->_temperature();
140
        $this->_voltage();
141
        $this->_fans();
142
    }
143
}
144