HDDTemp::_temperature()   D
last analyzed

Complexity

Conditions 25
Paths 60

Size

Total Lines 88
Code Lines 61

Duplication

Lines 20
Ratio 22.73 %

Importance

Changes 0
Metric Value
cc 25
eloc 61
nc 60
nop 0
dl 20
loc 88
rs 4.6339
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
 * hddtemp 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.hddtemp.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * getting information from hddtemp
17
 *
18
 * @category  PHP
19
 * @package   PSI_Sensor
20
 * @author    Michael Cramer <[email protected]>
21
 * @author    T.A. van Roermund <[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 HDDTemp 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 the temperature information from hddtemp
31
     * access is available through tcp or command
32
     *
33
     * @return array temperatures in array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
34
     */
35
    private function _temperature()
36
    {
37
        $ar_buf = array();
38
        switch (defined('PSI_SENSOR_HDDTEMP_ACCESS')?strtolower(PSI_SENSOR_HDDTEMP_ACCESS):'command') {
39
        case 'tcp':
40
            $lines = '';
41
            // Timo van Roermund: connect to the hddtemp daemon, use a 5 second timeout.
42
            $fp = @fsockopen('localhost', 7634, $errno, $errstr, 5);
43
            // if connected, read the output of the hddtemp daemon
44
            if ($fp) {
45
                while (!feof($fp)) {
46
                    $lines .= fread($fp, 1024);
47
                }
48
                fclose($fp);
49
            } else {
50
                $this->error->addError("HDDTemp error", $errno.", ".$errstr);
51
            }
52
            $lines = str_replace("||", "|\n|", $lines);
53
            $ar_buf = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
54
            break;
55
        case 'command':
56
            $strDrives = "";
57
            $strContent = "";
58
            $hddtemp_value = "";
59
            if (CommonFunctions::rfts("/proc/diskstats", $strContent, 0, 4096, false)) {
60
                $arrContent = preg_split("/\n/", $strContent, -1, PREG_SPLIT_NO_EMPTY);
61 View Code Duplication
                foreach ($arrContent as $strLine) {
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...
62
                    preg_match("/^\s(.*)\s([a-z]*)\s(.*)/", $strLine, $arrSplit);
63
                    if (! empty($arrSplit[2])) {
64
                        $strDrive = '/dev/'.$arrSplit[2];
65
                        if (file_exists($strDrive)) {
66
                            $strDrives = $strDrives.$strDrive.' ';
67
                        }
68
                    }
69
                }
70
            } else {
71
                if (CommonFunctions::rfts("/proc/partitions", $strContent, 0, 4096, false)) {
72
                    $arrContent = preg_split("/\n/", $strContent, -1, PREG_SPLIT_NO_EMPTY);
73 View Code Duplication
                    foreach ($arrContent as $strLine) {
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...
74
                        if (!preg_match("/^\s(.*)\s([\/a-z0-9]*(\/disc))\s(.*)/", $strLine, $arrSplit)) {
75
                            preg_match("/^\s(.*)\s([a-z]*)\s(.*)/", $strLine, $arrSplit);
76
                        }
77
                        if (! empty($arrSplit[2])) {
78
                            $strDrive = '/dev/'.$arrSplit[2];
79
                            if (file_exists($strDrive)) {
80
                                $strDrives = $strDrives.$strDrive.' ';
81
                            }
82
                        }
83
                    }
84
                }
85
            }
86
            if (trim($strDrives) == "") {
87
                break;
88
            }
89
            if (CommonFunctions::executeProgram("hddtemp", $strDrives, $hddtemp_value, PSI_DEBUG)) {
90
                $hddtemp_value = preg_split("/\n/", $hddtemp_value, -1, PREG_SPLIT_NO_EMPTY);
91
                foreach ($hddtemp_value as $line) {
92
                    $temp = preg_split("/:\s/", $line, 3);
93
                    if (count($temp) == 3 && preg_match("/^[0-9]/", $temp[2])) {
94
                        preg_match("/^([0-9]*)(.*)/", $temp[2], $ar_temp);
95
                        $temp[2] = trim($ar_temp[1]);
96
                        $temp[3] = trim($ar_temp[2]);
97
                        array_push($ar_buf, "|".implode("|", $temp)."|");
98
                    }
99
                }
100
            }
101
            break;
102
        default:
103
            $this->error->addConfigError("temperature()", "PSI_HDD_TEMP");
104
            break;
105
        }
106
        // Timo van Roermund: parse the info from the hddtemp daemon.
107
        foreach ($ar_buf as $line) {
108
            $data = array();
109
            if (preg_match("/\|(.*)\|(.*)\|(.*)\|(.*)\|/", $line, $data)) {
110
                if (trim($data[3]) != "ERR") {
111
                    // get the info we need
112
                    $dev = new SensorDevice();
113
                    $dev->setName($data[1] . ' (' . (strpos($data[2], "  ")?substr($data[2], 0, strpos($data[2], "  ")):$data[2]) . ')');
114
                    if (is_numeric($data[3])) {
115
                        $dev->setValue($data[3]);
116
                    }
117
//                    $dev->setMax(60);
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...
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
    }
123
124
    /**
125
     * get the information
126
     *
127
     * @see PSI_Interface_Sensor::build()
128
     *
129
     * @return Void
130
     */
131
    public function build()
132
    {
133
        $this->_temperature();
134
    }
135
}
136