| Conditions | 25 |
| Paths | 60 |
| Total Lines | 88 |
| Code Lines | 61 |
| Lines | 20 |
| Ratio | 22.73 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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) { |
|
| 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) { |
|
| 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); |
||
| 118 | $this->mbinfo->setMbTemp($dev); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 136 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.