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 |
||
13 | class Configuration |
||
14 | { |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $rootDir = ''; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $watchDirectories = []; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $testsDirectory = ''; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $bootstrapPath = ''; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $phpunitPath = 'vendor/bin/phpunit'; |
||
39 | |||
40 | /** |
||
41 | * @var null|EventDispatcher |
||
42 | */ |
||
43 | private $eventDispatcher; |
||
44 | |||
45 | /** |
||
46 | * @var null|DatabaseSandbox |
||
47 | */ |
||
48 | private $databaseSandbox; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $memoryLimit = '256M'; |
||
54 | |||
55 | /** |
||
56 | * @var int |
||
57 | */ |
||
58 | private $numChunks = 1; |
||
59 | |||
60 | 2 | public static function createFromXmlFile(string $path) : self |
|
138 | |||
139 | 1 | public function addListener( |
|
158 | |||
159 | 9 | View Code Duplication | public function setRootDir(string $rootDir) : self |
171 | |||
172 | 3 | public function getRootDir() : string |
|
176 | |||
177 | 4 | public function setWatchDirectories(array $watchDirectories) : self |
|
193 | |||
194 | 3 | public function getWatchDirectories() : array |
|
198 | |||
199 | 4 | View Code Duplication | public function setTestsDirectory(string $testsDirectory) : self |
211 | |||
212 | 2 | public function getTestsDirectory() : string |
|
216 | |||
217 | 3 | View Code Duplication | public function setBootstrapPath(string $bootstrapPath) : self |
229 | |||
230 | 2 | public function getBootstrapPath() : string |
|
234 | |||
235 | 9 | View Code Duplication | public function setPhpunitPath(string $phpunitPath) : self |
247 | |||
248 | 3 | public function getPhpunitPath() : string |
|
252 | |||
253 | 1 | public function setDatabaseSandbox(DatabaseSandbox $databaseSandbox) : self |
|
259 | |||
260 | 3 | public function getDatabaseSandbox() : DatabaseSandbox |
|
268 | |||
269 | 2 | public function setDatabaseNames(array $databaseNames) : self |
|
275 | |||
276 | public function setSandboxEnabled(bool $sandboxEnabled) : self |
||
277 | { |
||
278 | $this->getDatabaseSandbox()->setSandboxEnabled($sandboxEnabled); |
||
279 | |||
280 | return $this; |
||
281 | } |
||
282 | |||
283 | 1 | public function setMemoryLimit(string $memoryLimit) : self |
|
289 | |||
290 | 2 | public function getMemoryLimit() : string |
|
294 | |||
295 | 1 | public function setNumChunks(int $numChunks) : self |
|
301 | |||
302 | 1 | public function getNumChunks() : int |
|
306 | |||
307 | 1 | public function setEventDispatcher(EventDispatcher $eventDispatcher) : self |
|
313 | |||
314 | 2 | public function getEventDispatcher() : EventDispatcher |
|
322 | |||
323 | public function isSetup() |
||
339 | } |
||
340 |