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 | * @deprecated since KunstmaanMultiDomainBundle 5.7 and will be removed in KunstmaanMultiDomainBundle 6.0. Use the `$rootNodeCache` property instead. |
||
20 | */ |
||
21 | protected $rootNode; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $hosts; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $aliases = array(); |
||
32 | |||
33 | /** |
||
34 | * @var AdminRouteHelper |
||
35 | */ |
||
36 | protected $adminRouteHelper; |
||
37 | |||
38 | /** @var array */ |
||
39 | private $rootNodeCache = []; |
||
40 | |||
41 | /** @var EntityManagerInterface */ |
||
42 | private $em; |
||
43 | |||
44 | /** |
||
45 | * @param ContainerInterface|string $multilanguage |
||
|
|||
46 | */ |
||
47 | 32 | public function __construct(/*ContainerInterface|RequestStack*/ $requestStack, $multilanguage = null, $defaultLocale = null, $requiredLocales = null, AdminRouteHelper $adminRouteHelper = null, EntityManagerInterface $em = null, array $hosts = null) |
|
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | 25 | public function getHost() |
|
89 | |||
90 | /** |
||
91 | * @return array |
||
92 | */ |
||
93 | 1 | public function getHosts() |
|
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | 2 | View Code Duplication | public function getDefaultLocale() |
110 | |||
111 | /** |
||
112 | * @param string|null $host |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | 3 | View Code Duplication | public function isMultiLanguage($host = null) |
128 | |||
129 | /** |
||
130 | * @param string|null $host |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | 3 | View Code Duplication | public function getFrontendLocales($host = null) |
144 | |||
145 | /** |
||
146 | * @param string|null $host |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | 3 | View Code Duplication | public function getBackendLocales($host = null) |
160 | |||
161 | /** |
||
162 | * @return bool |
||
163 | */ |
||
164 | 5 | public function isMultiDomainHost() |
|
170 | |||
171 | /** |
||
172 | * Fetch the root node for the current host |
||
173 | * |
||
174 | * @param string|null $host |
||
175 | * |
||
176 | * @return Node|null |
||
177 | */ |
||
178 | 2 | public function getRootNode($host = null) |
|
202 | |||
203 | /** |
||
204 | * Return (optional) extra config settings for the current host |
||
205 | */ |
||
206 | 2 | View Code Duplication | public function getExtraData() |
216 | |||
217 | /** |
||
218 | * Return (optional) extra config settings for the locales for the current host |
||
219 | */ |
||
220 | 1 | View Code Duplication | public function getLocalesExtraData() |
230 | |||
231 | /** |
||
232 | * @return bool |
||
233 | */ |
||
234 | 25 | View Code Duplication | protected function hasHostOverride() |
243 | |||
244 | /** |
||
245 | * @return bool |
||
246 | */ |
||
247 | 2 | View Code Duplication | public function hasHostSwitched() |
256 | |||
257 | /** |
||
258 | * @return string|null |
||
259 | */ |
||
260 | 3 | protected function getHostOverride() |
|
268 | |||
269 | /** |
||
270 | * @return array |
||
271 | */ |
||
272 | 1 | public function getHostSwitched() |
|
284 | |||
285 | /** |
||
286 | * @return array |
||
287 | */ |
||
288 | 1 | public function getFullHostConfig() |
|
292 | |||
293 | /** |
||
294 | * @param string|null $host |
||
295 | * |
||
296 | * @return array |
||
297 | */ |
||
298 | 2 | public function getFullHost($host = null) |
|
308 | |||
309 | /** |
||
310 | * @param string|int $id |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | 1 | public function getFullHostById($id) |
|
326 | |||
327 | /** |
||
328 | * @param string|null $host |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | 1 | public function getHostBaseUrl($host = null) |
|
338 | |||
339 | /** |
||
340 | * @param string|null $host |
||
341 | * |
||
342 | * @return string|null |
||
343 | */ |
||
344 | 12 | private function getRealHost($host = null) |
|
352 | } |
||
353 |
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.