SpeedFan::__construct()   C
last analyzed

Complexity

Conditions 14
Paths 19

Size

Total Lines 35
Code Lines 24

Duplication

Lines 26
Ratio 74.29 %

Importance

Changes 0
Metric Value
cc 14
eloc 24
nc 19
nop 0
dl 26
loc 35
rs 5.0864
c 0
b 0
f 0

How to fix   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
 * speedfan 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.qtssnmp.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * getting hardware temperature information through snmpwalk
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 SpeedFan 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
     * variable, which holds the content of the command
31
     * @var array
32
     */
33
    private $_filecontent = array();
34
35
    public function __construct()
36
    {
37
        parent::__construct();
38
        switch (defined('PSI_SENSOR_SPEEDFAN_ACCESS')?strtolower(PSI_SENSOR_SPEEDFAN_ACCESS):'command') {
39 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...
40
            if (CommonFunctions::executeProgram("SpeedFanGet.exe", "", $buffer, PSI_DEBUG) && (strlen($buffer) > 0)) {
41
                if (preg_match("/^Temperatures:\s+(.+)$/m", $buffer, $out)) {
42
                    $this->_filecontent["temp"] = $out[1];
43
                }
44
                if (preg_match("/^Fans:\s+(.+)$/m", $buffer, $out)) {
45
                    $this->_filecontent["fans"] = $out[1];
46
                }
47
                if (preg_match("/^Voltages:\s+(.+)$/m", $buffer, $out)) {
48
                    $this->_filecontent["volt"] = $out[1];
49
                }
50
            }
51
            break;
52 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...
53
            if (CommonFunctions::rfts(APP_ROOT.'/data/speedfan.txt', $buffer) && (strlen($buffer) > 0)) {
54
                if (preg_match("/^Temperatures:\s+(.+)$/m", $buffer, $out)) {
55
                    $this->_filecontent["temp"] = $out[1];
56
                }
57
                if (preg_match("/^Fans:\s+(.+)$/m", $buffer, $out)) {
58
                    $this->_filecontent["fans"] = $out[1];
59
                }
60
                if (preg_match("/^Voltages:\s+(.+)$/m", $buffer, $out)) {
61
                    $this->_filecontent["volt"] = $out[1];
62
                }
63
            }
64
            break;
65
        default:
66
            $this->error->addConfigError('__construct()', 'PSI_SENSOR_SPEEDFAN_ACCESS');
67
            break;
68
        }
69
    }
70
71
    /**
72
     * get temperature information
73
     *
74
     * @return void
75
     */
76 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...
77
    {
78
        if (isset($this->_filecontent["temp"]) && (trim($this->_filecontent["temp"]) !== "")) {
79
            $values = preg_split("/ /", trim($this->_filecontent["temp"]));
80
            foreach ($values as $id=>$value) {
81
                $dev = new SensorDevice();
82
                $dev->setName("temp".$id);
83
                $dev->setValue($value);
84
                $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...
85
            }
86
        }
87
    }
88
89
    /**
90
     * get fan information
91
     *
92
     * @return void
93
     */
94 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...
95
    {
96
        if (isset($this->_filecontent["fans"]) && (trim($this->_filecontent["fans"]) !== "")) {
97
            $values = preg_split("/ /", trim($this->_filecontent["fans"]));
98
            foreach ($values as $id=>$value) {
99
                $dev = new SensorDevice();
100
                $dev->setName("fan".$id);
101
                $dev->setValue($value);
102
                $this->mbinfo->setMbFan($dev);
103
            }
104
        }
105
    }
106
107
    /**
108
     * get voltage information
109
     *
110
     * @return void
111
     */
112 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...
113
    {
114
        if (isset($this->_filecontent["volt"]) && (trim($this->_filecontent["volt"]) !== "")) {
115
            $values = preg_split("/ /", trim($this->_filecontent["volt"]));
116
            foreach ($values as $id=>$value) {
117
                $dev = new SensorDevice();
118
                $dev->setName("in".$id);
119
                $dev->setValue($value);
120
                $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...
121
            }
122
        }
123
    }
124
125
    /**
126
     * get the information
127
     *
128
     * @see PSI_Interface_Sensor::build()
129
     *
130
     * @return Void
131
     */
132
    public function build()
133
    {
134
        $this->_temperature();
135
        $this->_fans();
136
        $this->_voltage();
137
    }
138
}
139