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 Snapshot 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 Snapshot, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Snapshot |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var Blacklist |
||
| 23 | */ |
||
| 24 | private $blacklist; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $globalVariables = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $superGlobalArrays = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $superGlobalVariables = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $staticAttributes = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $iniSettings = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $includedFiles = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $constants = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $functions = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private $interfaces = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private $classes = array(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | private $traits = array(); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Creates a snapshot of the current global state. |
||
| 83 | * |
||
| 84 | * @param Blacklist $blacklist |
||
| 85 | * @param bool $includeGlobalVariables |
||
| 86 | * @param bool $includeStaticAttributes |
||
| 87 | * @param bool $includeConstants |
||
| 88 | * @param bool $includeFunctions |
||
| 89 | * @param bool $includeClasses |
||
| 90 | * @param bool $includeInterfaces |
||
| 91 | * @param bool $includeTraits |
||
| 92 | * @param bool $includeIniSettings |
||
| 93 | * @param bool $includeIncludedFiles |
||
| 94 | */ |
||
| 95 | public function __construct(Blacklist $blacklist = null, $includeGlobalVariables = true, $includeStaticAttributes = true, $includeConstants = true, $includeFunctions = true, $includeClasses = true, $includeInterfaces = true, $includeTraits = true, $includeIniSettings = true, $includeIncludedFiles = true) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return Blacklist |
||
| 143 | */ |
||
| 144 | public function blacklist() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | public function globalVariables() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function superGlobalVariables() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns a list of all super-global variable arrays. |
||
| 167 | * |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | public function superGlobalArrays() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function staticAttributes() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public function iniSettings() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function includedFiles() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function constants() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public function functions() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | public function interfaces() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function classes() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | public function traits() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Creates a snapshot user-defined constants. |
||
| 241 | */ |
||
| 242 | private function snapshotConstants() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Creates a snapshot user-defined functions. |
||
| 253 | */ |
||
| 254 | private function snapshotFunctions() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Creates a snapshot user-defined classes. |
||
| 263 | */ |
||
| 264 | View Code Duplication | private function snapshotClasses() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Creates a snapshot user-defined interfaces. |
||
| 281 | */ |
||
| 282 | View Code Duplication | private function snapshotInterfaces() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Creates a snapshot of all global and super-global variables. |
||
| 299 | */ |
||
| 300 | private function snapshotGlobals() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Creates a snapshot a super-global variable array. |
||
| 320 | * |
||
| 321 | * @param $superGlobalArray |
||
| 322 | */ |
||
| 323 | private function snapshotSuperGlobalArray($superGlobalArray) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Creates a snapshot of all static attributes in user-defined classes. |
||
| 336 | */ |
||
| 337 | private function snapshotStaticAttributes() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Returns a list of all super-global variable arrays. |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | private function setupSuperGlobalArrays() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param mixed $variable |
||
| 400 | * @return bool |
||
| 401 | * @todo Implement this properly |
||
| 402 | */ |
||
| 403 | private function canBeSerialized($variable) |
||
| 423 | } |
||
| 424 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.