Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BSDCommon often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BSDCommon, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | abstract class BSDCommon extends OS |
||
|
|
|||
| 29 | { |
||
| 30 | /** |
||
| 31 | * content of the syslog |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $_dmesg = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * regexp1 for cpu information out of the syslog |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $_CPURegExp1 = "//"; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * regexp2 for cpu information out of the syslog |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $_CPURegExp2 = "//"; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * regexp1 for scsi information out of the syslog |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $_SCSIRegExp1 = "//"; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * regexp2 for scsi information out of the syslog |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private $_SCSIRegExp2 = "//"; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * regexp1 for pci information out of the syslog |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | private $_PCIRegExp1 = "//"; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * regexp1 for pci information out of the syslog |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private $_PCIRegExp2 = "//"; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * call parent constructor |
||
| 81 | */ |
||
| 82 | public function __construct() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * setter for cpuregexp1 |
||
| 89 | * |
||
| 90 | * @param string $value value to set |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | protected function setCPURegExp1($value) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * setter for cpuregexp2 |
||
| 101 | * |
||
| 102 | * @param string $value value to set |
||
| 103 | * |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | protected function setCPURegExp2($value) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * setter for scsiregexp1 |
||
| 113 | * |
||
| 114 | * @param string $value value to set |
||
| 115 | * |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | protected function setSCSIRegExp1($value) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * setter for scsiregexp2 |
||
| 125 | * |
||
| 126 | * @param string $value value to set |
||
| 127 | * |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | protected function setSCSIRegExp2($value) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * setter for pciregexp1 |
||
| 137 | * |
||
| 138 | * @param string $value value to set |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | protected function setPCIRegExp1($value) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * setter for pciregexp2 |
||
| 149 | * |
||
| 150 | * @param string $value value to set |
||
| 151 | * |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | protected function setPCIRegExp2($value) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * read /var/run/dmesg.boot, but only if we haven't already |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | protected function readdmesg() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * get a value from sysctl command |
||
| 180 | * |
||
| 181 | * @param string $key key for the value to get |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | protected function grabkey($key) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Virtual Host Name |
||
| 197 | * |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | View Code Duplication | protected function hostname() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Kernel Version |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | protected function kernel() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Processor Load |
||
| 229 | * optionally create a loadbar |
||
| 230 | * |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | protected function loadavg() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * CPU information |
||
| 260 | * |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | protected function cpuinfo() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * SCSI devices |
||
| 314 | * get the scsi device information out of dmesg |
||
| 315 | * |
||
| 316 | * @return void |
||
| 317 | */ |
||
| 318 | protected function scsi() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * parsing the output of pciconf command |
||
| 352 | * |
||
| 353 | * @return Array |
||
| 354 | */ |
||
| 355 | protected function pciconf() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * PCI devices |
||
| 398 | * get the pci device information out of dmesg |
||
| 399 | * |
||
| 400 | * @return void |
||
| 401 | */ |
||
| 402 | protected function pci() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * IDE devices |
||
| 424 | * get the ide device information out of dmesg |
||
| 425 | * |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | protected function ide() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Physical memory information and Swap Space information |
||
| 471 | * |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | protected function memory() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * USB devices |
||
| 516 | * get the ide device information out of dmesg |
||
| 517 | * |
||
| 518 | * @return void |
||
| 519 | */ |
||
| 520 | View Code Duplication | protected function usb() |
|
| 532 | |||
| 533 | /** |
||
| 534 | * filesystem information |
||
| 535 | * |
||
| 536 | * @return void |
||
| 537 | */ |
||
| 538 | protected function filesystems() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Distribution |
||
| 548 | * |
||
| 549 | * @return void |
||
| 550 | */ |
||
| 551 | protected function distro() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * get the information |
||
| 560 | * |
||
| 561 | * @see PSI_Interface_OS::build() |
||
| 562 | * |
||
| 563 | * @return Void |
||
| 564 | */ |
||
| 565 | public function build() |
||
| 580 | } |
||
| 581 |
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.