| Conditions | 1 |
| Paths | 1 |
| Total Lines | 210 |
| Code Lines | 2 |
| Lines | 80 |
| Ratio | 38.1 % |
| 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 |
||
| 61 | public static function df($df_param = "") |
||
| 62 | { |
||
| 63 | $arrResult = array(); |
||
| 64 | if (CommonFunctions::executeProgram('mount', '', $mount, PSI_DEBUG)) { |
||
| 65 | $mount = preg_split("/\n/", $mount, -1, PREG_SPLIT_NO_EMPTY); |
||
| 66 | foreach ($mount as $mount_line) { |
||
| 67 | if (preg_match("/(\S+) on ([\S ]+) type (.*) \((.*)\)/", $mount_line, $mount_buf)) { |
||
| 68 | $parm = array(); |
||
| 69 | $parm['mountpoint'] = trim($mount_buf[2]); |
||
| 70 | $parm['fstype'] = $mount_buf[3]; |
||
| 71 | $parm['name'] = $mount_buf[1]; |
||
| 72 | if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4]; |
||
| 73 | $mount_parm[] = $parm; |
||
| 74 | } elseif (preg_match("/(\S+) is (.*) mounted on (\S+) \(type (.*)\)/", $mount_line, $mount_buf)) { |
||
| 75 | $parm = array(); |
||
| 76 | $parm['mountpoint'] = trim($mount_buf[3]); |
||
| 77 | $parm['fstype'] = $mount_buf[4]; |
||
| 78 | $parm['name'] = $mount_buf[1]; |
||
| 79 | if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[2]; |
||
| 80 | $mount_parm[] = $parm; |
||
| 81 | } elseif (preg_match("/(\S+) (.*) on (\S+) \((.*)\)/", $mount_line, $mount_buf)) { |
||
| 82 | $parm = array(); |
||
| 83 | $parm['mountpoint'] = trim($mount_buf[3]); |
||
| 84 | $parm['fstype'] = $mount_buf[2]; |
||
| 85 | $parm['name'] = $mount_buf[1]; |
||
| 86 | if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4]; |
||
| 87 | $mount_parm[] = $parm; |
||
| 88 | View Code Duplication | } elseif (preg_match("/(\S+) on ([\S ]+) \((\S+)(,\s(.*))?\)/", $mount_line, $mount_buf)) { |
|
| 89 | $parm = array(); |
||
| 90 | $parm['mountpoint'] = trim($mount_buf[2]); |
||
| 91 | $parm['fstype'] = $mount_buf[3]; |
||
| 92 | $parm['name'] = $mount_buf[1]; |
||
| 93 | if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = isset($mount_buf[5]) ? $mount_buf[5] : ''; |
||
| 94 | $mount_parm[] = $parm; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } elseif (CommonFunctions::rfts("/etc/mtab", $mount)) { |
||
| 98 | $mount = preg_split("/\n/", $mount, -1, PREG_SPLIT_NO_EMPTY); |
||
| 99 | foreach ($mount as $mount_line) { |
||
| 100 | View Code Duplication | if (preg_match("/(\S+) (\S+) (\S+) (\S+) ([0-9]+) ([0-9]+)/", $mount_line, $mount_buf)) { |
|
| 101 | $parm = array(); |
||
| 102 | $mount_point = preg_replace("/\\\\040/i", ' ', $mount_buf[2]); //space as \040 |
||
| 103 | $parm['mountpoint'] = $mount_point; |
||
| 104 | $parm['fstype'] = $mount_buf[3]; |
||
| 105 | $parm['name'] = $mount_buf[1]; |
||
| 106 | if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4]; |
||
| 107 | $mount_parm[] = $parm; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | if (CommonFunctions::executeProgram('df', '-k '.$df_param, $df, PSI_DEBUG) && ($df!=="")) { |
||
| 112 | $df = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY); |
||
| 113 | if (PSI_SHOW_INODES) { |
||
| 114 | if (CommonFunctions::executeProgram('df', '-i '.$df_param, $df2, PSI_DEBUG)) { |
||
| 115 | $df2 = preg_split("/\n/", $df2, -1, PREG_SPLIT_NO_EMPTY); |
||
| 116 | // Store inode use% in an associative array (df_inodes) for later use |
||
| 117 | foreach ($df2 as $df2_line) { |
||
| 118 | if (preg_match("/^(\S+).*\s([0-9]+)%/", $df2_line, $inode_buf)) { |
||
| 119 | $df_inodes[$inode_buf[1]] = $inode_buf[2]; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | foreach ($df as $df_line) { |
||
| 125 | $df_buf1 = preg_split("/(\%\s)/", $df_line, 3); |
||
| 126 | if (count($df_buf1) < 2) { |
||
| 127 | continue; |
||
| 128 | } |
||
| 129 | if (preg_match("/(.*)(\s+)(([0-9]+)(\s+)([0-9]+)(\s+)([\-0-9]+)(\s+)([0-9]+)$)/", $df_buf1[0], $df_buf2)) { |
||
| 130 | if (count($df_buf1) == 3) { |
||
| 131 | $df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[2]); |
||
| 132 | } else { |
||
| 133 | $df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[1]); |
||
| 134 | } |
||
| 135 | if (count($df_buf) == 6) { |
||
| 136 | $df_buf[5] = trim($df_buf[5]); |
||
| 137 | $dev = new DiskDevice(); |
||
| 138 | $dev->setName(trim($df_buf[0])); |
||
| 139 | if ($df_buf[2] < 0) { |
||
| 140 | $dev->setTotal($df_buf[3] * 1024); |
||
| 141 | $dev->setUsed($df_buf[3] * 1024); |
||
| 142 | } else { |
||
| 143 | $dev->setTotal($df_buf[1] * 1024); |
||
| 144 | $dev->setUsed($df_buf[2] * 1024); |
||
| 145 | if ($df_buf[3]>0) { |
||
| 146 | $dev->setFree($df_buf[3] * 1024); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($df_buf[5]); |
||
| 150 | |||
| 151 | $notwas = true; |
||
| 152 | if (isset($mount_parm)) { |
||
| 153 | foreach ($mount_parm as $mount_param) { //name and mountpoint find |
||
| 154 | if (($mount_param['name']===trim($df_buf[0])) && ($mount_param['mountpoint']===$df_buf[5])) { |
||
| 155 | $dev->setFsType($mount_param['fstype']); |
||
| 156 | View Code Duplication | if (PSI_SHOW_MOUNT_OPTION) { |
|
| 157 | if (PSI_SHOW_MOUNT_CREDENTIALS) { |
||
| 158 | $dev->setOptions($mount_param['options']); |
||
| 159 | } else { |
||
| 160 | $mpo=$mount_param['options']; |
||
| 161 | |||
| 162 | $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo); |
||
| 163 | $mpo=preg_replace('/,guest,/i', ',', $mpo); |
||
| 164 | |||
| 165 | $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo); |
||
| 166 | $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo); |
||
| 167 | |||
| 168 | $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo); |
||
| 169 | $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo); |
||
| 170 | |||
| 171 | $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo); |
||
| 172 | $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo); |
||
| 173 | |||
| 174 | $dev->setOptions($mpo); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | $notwas = false; |
||
| 178 | break; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | if ($notwas) foreach ($mount_parm as $mount_param) { //mountpoint find |
||
| 182 | if ($mount_param['mountpoint']===$df_buf[5]) { |
||
| 183 | $dev->setFsType($mount_param['fstype']); |
||
| 184 | View Code Duplication | if (PSI_SHOW_MOUNT_OPTION) { |
|
| 185 | if (PSI_SHOW_MOUNT_CREDENTIALS) { |
||
| 186 | $dev->setOptions($mount_param['options']); |
||
| 187 | } else { |
||
| 188 | $mpo=$mount_param['options']; |
||
| 189 | |||
| 190 | $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo); |
||
| 191 | $mpo=preg_replace('/,guest,/i', ',', $mpo); |
||
| 192 | |||
| 193 | $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo); |
||
| 194 | $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo); |
||
| 195 | |||
| 196 | $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo); |
||
| 197 | $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo); |
||
| 198 | |||
| 199 | $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo); |
||
| 200 | $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo); |
||
| 201 | |||
| 202 | $dev->setOptions($mpo); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | $notwas = false; |
||
| 206 | break; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | if ($notwas) { |
||
| 212 | $dev->setFsType('unknown'); |
||
| 213 | } |
||
| 214 | |||
| 215 | if (PSI_SHOW_INODES && isset($df_inodes[trim($df_buf[0])])) { |
||
| 216 | $dev->setPercentInodesUsed($df_inodes[trim($df_buf[0])]); |
||
| 217 | } |
||
| 218 | $arrResult[] = $dev; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } else { |
||
| 223 | if (isset($mount_parm)) { |
||
| 224 | foreach ($mount_parm as $mount_param) { |
||
| 225 | $total = disk_total_space($mount_param['mountpoint']); |
||
| 226 | if (($mount_param['fstype'] != 'none') && ($total > 0)) { |
||
| 227 | $dev = new DiskDevice(); |
||
| 228 | $dev->setName($mount_param['name']); |
||
| 229 | $dev->setFsType($mount_param['fstype']); |
||
| 230 | |||
| 231 | if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($mount_param['mountpoint']); |
||
| 232 | |||
| 233 | $dev->setTotal($total); |
||
| 234 | $free = disk_free_space($mount_param['mountpoint']); |
||
| 235 | if ($free > 0) { |
||
| 236 | $dev->setFree($free); |
||
| 237 | } else { |
||
| 238 | $free = 0; |
||
| 239 | } |
||
| 240 | if ($total > $free) $dev->setUsed($total - $free); |
||
| 241 | |||
| 242 | View Code Duplication | if (PSI_SHOW_MOUNT_OPTION) { |
|
| 243 | if (PSI_SHOW_MOUNT_CREDENTIALS) { |
||
| 244 | $dev->setOptions($mount_param['options']); |
||
| 245 | } else { |
||
| 246 | $mpo=$mount_param['options']; |
||
| 247 | |||
| 248 | $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo); |
||
| 249 | $mpo=preg_replace('/,guest,/i', ',', $mpo); |
||
| 250 | |||
| 251 | $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo); |
||
| 252 | $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo); |
||
| 253 | |||
| 254 | $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo); |
||
| 255 | $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo); |
||
| 256 | |||
| 257 | $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo); |
||
| 258 | $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo); |
||
| 259 | |||
| 260 | $dev->setOptions($mpo); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | $arrResult[] = $dev; |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | return $arrResult; |
||
| 270 | } |
||
| 271 | } |
||
| 272 |
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.