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 Configuration 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 Configuration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Configuration |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | private $rootDir = ''; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private $watchDirectories = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $testsDirectory = ''; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $bootstrapPath = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $phpunitPath = 'vendor/bin/phpunit'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var null|EventDispatcher |
||
| 40 | */ |
||
| 41 | private $eventDispatcher; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var null|DatabaseSandbox |
||
| 45 | */ |
||
| 46 | private $databaseSandbox; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $memoryLimit = '256M'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | private $numChunks = 1; |
||
| 57 | |||
| 58 | 3 | public static function createFromXmlFile(string $path) : self |
|
| 136 | |||
| 137 | 2 | public function addListener( |
|
| 156 | |||
| 157 | 10 | View Code Duplication | public function setRootDir(string $rootDir) : self |
| 169 | |||
| 170 | 4 | public function getRootDir() : string |
|
| 174 | |||
| 175 | 5 | public function setWatchDirectories(array $watchDirectories) : self |
|
| 191 | |||
| 192 | 3 | public function getWatchDirectories() : array |
|
| 196 | |||
| 197 | 5 | View Code Duplication | public function setTestsDirectory(string $testsDirectory) : self |
| 209 | |||
| 210 | 3 | public function getTestsDirectory() : string |
|
| 214 | |||
| 215 | 4 | View Code Duplication | public function setBootstrapPath(string $bootstrapPath) : self |
| 227 | |||
| 228 | 3 | public function getBootstrapPath() : string |
|
| 232 | |||
| 233 | 10 | View Code Duplication | public function setPhpunitPath(string $phpunitPath) : self |
| 245 | |||
| 246 | 3 | public function getPhpunitPath() : string |
|
| 250 | |||
| 251 | 1 | public function setDatabaseSandbox(DatabaseSandbox $databaseSandbox) : self |
|
| 257 | |||
| 258 | 4 | public function getDatabaseSandbox() : DatabaseSandbox |
|
| 266 | |||
| 267 | 3 | public function setDatabaseNames(array $databaseNames) : self |
|
| 273 | |||
| 274 | 1 | public function setSandboxEnabled(bool $sandboxEnabled) : self |
|
| 280 | |||
| 281 | 2 | public function setMemoryLimit(string $memoryLimit) : self |
|
| 287 | |||
| 288 | 2 | public function getMemoryLimit() : string |
|
| 292 | |||
| 293 | 2 | public function setNumChunks(int $numChunks) : self |
|
| 299 | |||
| 300 | 1 | public function getNumChunks() : int |
|
| 304 | |||
| 305 | 1 | public function setEventDispatcher(EventDispatcher $eventDispatcher) : self |
|
| 311 | |||
| 312 | 3 | public function getEventDispatcher() : EventDispatcher |
|
| 320 | |||
| 321 | 2 | public function throwExceptionIfConfigurationIncomplete() |
|
| 341 | } |
||
| 342 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.