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 |
|
132 | |||
133 | 2 | public function addListener( |
|
152 | |||
153 | 10 | View Code Duplication | public function setRootDir(string $rootDir) : self |
165 | |||
166 | 4 | public function getRootDir() : string |
|
170 | |||
171 | 5 | public function setWatchDirectories(array $watchDirectories) : self |
|
187 | |||
188 | 3 | public function getWatchDirectories() : array |
|
192 | |||
193 | 5 | View Code Duplication | public function setTestsDirectory(string $testsDirectory) : self |
205 | |||
206 | 3 | public function getTestsDirectory() : string |
|
210 | |||
211 | 4 | View Code Duplication | public function setBootstrapPath(string $bootstrapPath) : self |
223 | |||
224 | 3 | public function getBootstrapPath() : string |
|
228 | |||
229 | 10 | View Code Duplication | public function setPhpunitPath(string $phpunitPath) : self |
241 | |||
242 | 3 | public function getPhpunitPath() : string |
|
246 | |||
247 | 1 | public function setDatabaseSandbox(DatabaseSandbox $databaseSandbox) : self |
|
253 | |||
254 | 4 | public function getDatabaseSandbox() : DatabaseSandbox |
|
262 | |||
263 | 3 | public function setDatabaseNames(array $databaseNames) : self |
|
269 | |||
270 | 1 | public function setSandboxEnabled(bool $sandboxEnabled) : self |
|
276 | |||
277 | 2 | public function setMemoryLimit(string $memoryLimit) : self |
|
283 | |||
284 | 2 | public function getMemoryLimit() : string |
|
288 | |||
289 | 2 | public function setNumChunks(int $numChunks) : self |
|
295 | |||
296 | 1 | public function getNumChunks() : int |
|
300 | |||
301 | 1 | public function setEventDispatcher(EventDispatcher $eventDispatcher) : self |
|
307 | |||
308 | 3 | public function getEventDispatcher() : EventDispatcher |
|
316 | |||
317 | 2 | public function throwExceptionIfConfigurationIncomplete() |
|
337 | } |
||
338 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: