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 DomainConfiguration 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 DomainConfiguration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class DomainConfiguration extends BaseDomainConfiguration |
||
12 | { |
||
13 | const OVERRIDE_HOST = '_override_host'; |
||
14 | const SWITCH_HOST = '_switch_host'; |
||
15 | |||
16 | /** |
||
17 | * @var Node |
||
18 | */ |
||
19 | protected $rootNode; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $hosts; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $aliases = array(); |
||
30 | |||
31 | /** |
||
32 | * @var AdminRouteHelper |
||
33 | */ |
||
34 | protected $adminRouteHelper; |
||
35 | |||
36 | /** @var array */ |
||
37 | private $rootNodeCache = []; |
||
38 | |||
39 | /** @var EntityManagerInterface */ |
||
40 | private $em; |
||
41 | |||
42 | /** |
||
43 | * @param ContainerInterface|string $multilanguage |
||
|
|||
44 | */ |
||
45 | 32 | public function __construct(/*ContainerInterface|RequestStack*/ $requestStack, $multilanguage = null, $defaultLocale = null, $requiredLocales = null, AdminRouteHelper $adminRouteHelper = null, EntityManagerInterface $em = null, array $hosts = null) |
|
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | */ |
||
74 | 25 | public function getHost() |
|
87 | |||
88 | /** |
||
89 | * @return array |
||
90 | */ |
||
91 | 1 | public function getHosts() |
|
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | 2 | View Code Duplication | public function getDefaultLocale() |
108 | |||
109 | /** |
||
110 | * @param string|null $host |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | 3 | View Code Duplication | public function isMultiLanguage($host = null) |
126 | |||
127 | /** |
||
128 | * @param string|null $host |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 3 | View Code Duplication | public function getFrontendLocales($host = null) |
142 | |||
143 | /** |
||
144 | * @param string|null $host |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | 3 | View Code Duplication | public function getBackendLocales($host = null) |
158 | |||
159 | /** |
||
160 | * @return bool |
||
161 | */ |
||
162 | 5 | public function isMultiDomainHost() |
|
168 | |||
169 | /** |
||
170 | * Fetch the root node for the current host |
||
171 | * |
||
172 | * @param string|null $host |
||
173 | * |
||
174 | * @return Node|null |
||
175 | */ |
||
176 | 2 | public function getRootNode($host = null) |
|
200 | |||
201 | /** |
||
202 | * Return (optional) extra config settings for the current host |
||
203 | */ |
||
204 | 2 | View Code Duplication | public function getExtraData() |
214 | |||
215 | /** |
||
216 | * Return (optional) extra config settings for the locales for the current host |
||
217 | */ |
||
218 | 1 | View Code Duplication | public function getLocalesExtraData() |
228 | |||
229 | /** |
||
230 | * @return bool |
||
231 | */ |
||
232 | 25 | View Code Duplication | protected function hasHostOverride() |
241 | |||
242 | /** |
||
243 | * @return bool |
||
244 | */ |
||
245 | 2 | View Code Duplication | public function hasHostSwitched() |
254 | |||
255 | /** |
||
256 | * @return string|null |
||
257 | */ |
||
258 | 3 | protected function getHostOverride() |
|
266 | |||
267 | /** |
||
268 | * @return array |
||
269 | */ |
||
270 | 1 | public function getHostSwitched() |
|
282 | |||
283 | /** |
||
284 | * @return array |
||
285 | */ |
||
286 | 1 | public function getFullHostConfig() |
|
290 | |||
291 | /** |
||
292 | * @param string|null $host |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | 2 | public function getFullHost($host = null) |
|
306 | |||
307 | /** |
||
308 | * @param string|int $id |
||
309 | * |
||
310 | * @return array |
||
311 | */ |
||
312 | 1 | public function getFullHostById($id) |
|
324 | |||
325 | /** |
||
326 | * @param string|null $host |
||
327 | * |
||
328 | * @return string |
||
329 | */ |
||
330 | 1 | public function getHostBaseUrl($host = null) |
|
336 | |||
337 | /** |
||
338 | * @param string|null $host |
||
339 | * |
||
340 | * @return string|null |
||
341 | */ |
||
342 | 12 | private function getRealHost($host = null) |
|
350 | } |
||
351 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.