ThermalZone::_temperature()   C
last analyzed

Complexity

Conditions 30
Paths 87

Size

Total Lines 69
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 30
eloc 47
nc 87
nop 0
dl 0
loc 69
rs 5.4959
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
 * Thermal Zone 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.ohm.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * getting information from Thermal Zone WMI class
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 ThermalZone 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
     * holds the COM object that we pull all the WMI data from
30
     *
31
     * @var Object
32
     */
33
    private $_buf = array();
34
35
    /**
36
     * fill the private content var
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
        if (PSI_OS == 'WINNT') {
42
            $_wmi = null;
43
            // don't set this params for local connection, it will not work
44
            $strHostname = '';
45
            $strUser = '';
46
            $strPassword = '';
47
            try {
48
                // initialize the wmi object
49
                $objLocator = new COM('WbemScripting.SWbemLocator');
0 ignored issues
show
Unused Code introduced by
The call to com::__construct() has too many arguments starting with 'WbemScripting.SWbemLocator'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
50
                if ($strHostname == "") {
51
                    $_wmi = $objLocator->ConnectServer($strHostname, 'root\WMI');
52
53
                } else {
54
                    $_wmi = $objLocator->ConnectServer($strHostname, 'root\WMI', $strHostname.'\\'.$strUser, $strPassword);
55
                }
56
            } catch (Exception $e) {
57
                $this->error->addError("WMI connect error", "PhpSysInfo can not connect to the WMI interface for ThermalZone data.");
58
            }
59
            if ($_wmi) {
60
                $this->_buf = CommonFunctions::getWMI($_wmi, 'MSAcpi_ThermalZoneTemperature', array('InstanceName', 'CriticalTripPoint', 'CurrentTemperature'));
0 ignored issues
show
Documentation Bug introduced by
It seems like \CommonFunctions::getWMI... 'CurrentTemperature')) of type array is incompatible with the declared type object of property $_buf.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
            }
62
        }
63
    }
64
65
    /**
66
     * get temperature information
67
     *
68
     * @return void
69
     */
70
    private function _temperature()
71
    {
72
        if (PSI_OS == 'WINNT') {
73
            if ($this->_buf) foreach ($this->_buf as $buffer) {
74
                if (isset($buffer['CurrentTemperature']) && (($value = ($buffer['CurrentTemperature'] - 2732)/10) > -100)) {
75
                    $dev = new SensorDevice();
76
                    if (isset($buffer['InstanceName']) && preg_match("/([^\\\\ ]+)$/", $buffer['InstanceName'], $outbuf)) {
77
                        $dev->setName('ThermalZone '.$outbuf[1]);
78
                    } else {
79
                        $dev->setName('ThermalZone THM0_0');
80
                    }
81
                    $dev->setValue($value);
82
                    if (isset($buffer['CriticalTripPoint']) && (($maxvalue = ($buffer['CriticalTripPoint'] - 2732)/10) > 0)) {
83
                        $dev->setMax($maxvalue);
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
        } else {
89
            $notwas = true;
90
            foreach (glob('/sys/class/thermal/thermal_zone*/') as $thermalzone) {
91
                $thermalzonetemp = $thermalzone.'temp';
92
                $temp = null;
93
                if (CommonFunctions::rfts($thermalzonetemp, $temp, 0, 4096, false) && !is_null($temp) && (trim($temp) != "")) {
94
                    if ($temp >= 1000) {
95
                        $temp = $temp / 1000;
96
                    }
97
98
                    if ($temp > -40) {
99
                        $dev = new SensorDevice();
100
                        $dev->setValue($temp);
101
102
                        $temp_type = null;
103
                        if (CommonFunctions::rfts($thermalzone.'type', $temp_type, 0, 4096, false) && !is_null($temp_type) && (trim($temp_type) != "")) {
104
                            $dev->setName($temp_type);
105
                        } else {
106
                            $dev->setName("ThermalZone");
107
                        }
108
109
                        $temp_max = null;
110
                        if (CommonFunctions::rfts($thermalzone.'trip_point_0_temp', $temp_max, 0, 4096, false) && !is_null($temp_max) && (trim($temp_max) != "") && ($temp_max > 0)) {
111
                            if ($temp_max >= 1000) {
112
                                $temp_max = $temp_max / 1000;
113
                            }
114
                            $dev->setMax($temp_max);
115
                        }
116
117
                        $notwas = false;
118
                        $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...
119
                    }
120
                }
121
            }
122
            if ($notwas) {
123
                foreach (glob('/proc/acpi/thermal_zone/TH*/temperature') as $thermalzone) {
124
                    $temp = null;
125
                    if (CommonFunctions::rfts($thermalzone, $temp, 1, 4096, false) && !is_null($temp) && (trim($temp) != "")) {
126
                        $dev = new SensorDevice();
127
                        if (preg_match("/^\/proc\/acpi\/thermal_zone\/(.+)\/temperature$/", $thermalzone, $name)) {
128
                           $dev->setName("ThermalZone ".$name[1]);
129
                        } else {
130
                            $dev->setName("ThermalZone");
131
                        }
132
                        $dev->setValue(trim(substr($temp, 23, 4)));
133
                        $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...
134
                    }
135
                }
136
            }
137
        }
138
    }
139
140
    /**
141
     * get the information
142
     *
143
     * @see PSI_Interface_Sensor::build()
144
     *
145
     * @return Void
146
     */
147
    public function build()
148
    {
149
      $this->_temperature();
150
    }
151
}
152