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 QNX 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 QNX, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class QNX extends OS |
||
|
|
|||
| 28 | { |
||
| 29 | /** |
||
| 30 | * content of the syslog |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $_dmesg = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * call parent constructor |
||
| 38 | */ |
||
| 39 | public function __construct() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * get the cpu information |
||
| 46 | * |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | protected function _cpuinfo() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * QNX Version |
||
| 69 | * |
||
| 70 | * @return void |
||
| 71 | */ |
||
| 72 | private function _kernel() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Distribution |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | protected function _distro() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * UpTime |
||
| 96 | * time the system is running |
||
| 97 | * |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | private function _uptime() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Number of Users |
||
| 128 | * |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | protected function _users() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Virtual Host Name |
||
| 138 | * |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | View Code Duplication | private function _hostname() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Physical memory information and Swap Space information |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | private function _memory() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * filesystem information |
||
| 172 | * |
||
| 173 | * @return void |
||
| 174 | */ |
||
| 175 | private function _filesystems() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * network information |
||
| 185 | * |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | private function _network() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * get the information |
||
| 221 | * |
||
| 222 | * @return Void |
||
| 223 | */ |
||
| 224 | View Code Duplication | public function build() |
|
| 237 | } |
||
| 238 |
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.