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 | 2 | public static function createFromXmlFile(string $path) : self |
|
125 | |||
126 | 10 | View Code Duplication | public function setRootDir(string $rootDir) : self |
138 | |||
139 | 4 | public function getRootDir() : string |
|
143 | |||
144 | 5 | public function setWatchDirectories(array $watchDirectories) : self |
|
160 | |||
161 | 3 | public function getWatchDirectories() : array |
|
165 | |||
166 | 5 | View Code Duplication | public function setTestsDirectory(string $testsDirectory) : self |
178 | |||
179 | 3 | public function getTestsDirectory() : string |
|
183 | |||
184 | 4 | View Code Duplication | public function setBootstrapPath(string $bootstrapPath) : self |
196 | |||
197 | 3 | public function getBootstrapPath() : string |
|
201 | |||
202 | 10 | View Code Duplication | public function setPhpunitPath(string $phpunitPath) : self |
214 | |||
215 | 3 | public function getPhpunitPath() : string |
|
219 | |||
220 | 1 | public function setDatabaseSandbox(DatabaseSandbox $databaseSandbox) : self |
|
226 | |||
227 | 4 | public function getDatabaseSandbox() : DatabaseSandbox |
|
235 | |||
236 | 3 | public function setDatabaseNames(array $databaseNames) : self |
|
242 | |||
243 | 1 | public function setSandboxEnabled(bool $sandboxEnabled) : self |
|
249 | |||
250 | 2 | public function setMemoryLimit(string $memoryLimit) : self |
|
256 | |||
257 | 2 | public function getMemoryLimit() : string |
|
261 | |||
262 | 2 | public function setNumChunks(int $numChunks) : self |
|
268 | |||
269 | 1 | public function getNumChunks() : int |
|
270 | { |
||
271 | 1 | return $this->numChunks; |
|
272 | } |
||
273 | |||
274 | 1 | public function setEventDispatcher(EventDispatcher $eventDispatcher) : self |
|
280 | |||
281 | 3 | public function getEventDispatcher() : EventDispatcher |
|
289 | |||
290 | 2 | public function throwExceptionIfConfigurationIncomplete() |
|
310 | } |
||
311 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.