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 Container 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 Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Container extends DataContainer implements ContainerInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $environment; |
||
26 | |||
27 | /** |
||
28 | * @var Finder |
||
29 | */ |
||
30 | protected $finder; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $handlers; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $configFolder = 'config'; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $defaultFormat = 'php'; |
||
46 | |||
47 | /** |
||
48 | * @param string $environment |
||
49 | * @param Finder $finder |
||
50 | * @param string $defaultFormat |
||
51 | */ |
||
52 | 15 | public function __construct($environment = null, $finder = null, $defaultFormat = 'php') |
|
73 | |||
74 | /** |
||
75 | * Returns the default format |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 1 | public function getDefaultFormat() |
|
83 | |||
84 | /** |
||
85 | * Sets the default format |
||
86 | * |
||
87 | * @param string $format |
||
88 | */ |
||
89 | 1 | public function setDefaultFormat($format) |
|
93 | |||
94 | /** |
||
95 | * Ensures a default config format |
||
96 | * |
||
97 | * @param string $file |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | 10 | public function ensureDefaultFormat($file) |
|
110 | |||
111 | /** |
||
112 | * Returns the environment |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 1 | public function getEnvironment() |
|
120 | |||
121 | /** |
||
122 | * Sets the environment |
||
123 | * |
||
124 | * @param string $environment |
||
125 | */ |
||
126 | 3 | public function setEnvironment($environment) |
|
135 | |||
136 | /** |
||
137 | * Unloads a config group |
||
138 | * |
||
139 | * @param string $group |
||
140 | */ |
||
141 | 1 | public function unload($group) |
|
145 | |||
146 | /** |
||
147 | * Reloads a group |
||
148 | * |
||
149 | * @param string $name |
||
150 | * @param string|boolean $group |
||
151 | * |
||
152 | * @return array|null |
||
153 | */ |
||
154 | 1 | public function reload($name, $group = true) |
|
165 | |||
166 | /** |
||
167 | * Loads a config file |
||
168 | * |
||
169 | * @param string $name |
||
170 | * @param null|string|boolean $group |
||
171 | * |
||
172 | * @return array|null |
||
173 | */ |
||
174 | 8 | public function load($name, $group = null) |
|
214 | |||
215 | /** |
||
216 | * Stores a config file |
||
217 | * |
||
218 | * @param string $group |
||
219 | * @param string|null $destination |
||
220 | * |
||
221 | * @throws \RuntimeException |
||
222 | */ |
||
223 | 6 | public function save($group, $destination = null) |
|
249 | |||
250 | /** |
||
251 | * Finds a config file |
||
252 | * |
||
253 | * @param string $destination |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | 5 | public function findDestination($destination) |
|
280 | |||
281 | /** |
||
282 | * Retrieves the handler for a file type |
||
283 | * |
||
284 | * @param string $extension |
||
285 | * |
||
286 | * @return Handler |
||
287 | * |
||
288 | * @throws \RuntimeException |
||
289 | */ |
||
290 | 11 | public function getHandler($extension) |
|
309 | |||
310 | /** |
||
311 | * Sets a handler for an extension |
||
312 | * |
||
313 | * @param string $extension |
||
314 | * @param Handler $handler |
||
315 | */ |
||
316 | 1 | public function setHandler($extension, Handler $loader) |
|
320 | |||
321 | /** |
||
322 | * Sets the config folder |
||
323 | * |
||
324 | * @param string $folder |
||
325 | */ |
||
326 | 6 | public function setConfigFolder($folder) |
|
330 | |||
331 | /** |
||
332 | * Adds a path |
||
333 | * |
||
334 | * @param string $path |
||
335 | * |
||
336 | * @return $this |
||
337 | */ |
||
338 | 8 | View Code Duplication | public function addPath($path) |
350 | |||
351 | /** |
||
352 | * Adds paths to look in |
||
353 | * |
||
354 | * @param array $paths |
||
355 | */ |
||
356 | 1 | public function addPaths(array $paths) |
|
360 | |||
361 | /** |
||
362 | * Removes a path |
||
363 | * |
||
364 | * @param string $path |
||
365 | */ |
||
366 | 1 | View Code Duplication | public function removePath($path) |
376 | |||
377 | /** |
||
378 | * Removes paths |
||
379 | * |
||
380 | * @param array $paths |
||
381 | */ |
||
382 | 1 | public function removePaths(array $paths) |
|
386 | } |
||
387 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: