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 EntityManagerInterface */ |
||
37 | private $em; |
||
38 | |||
39 | /** |
||
40 | * @param ContainerInterface|string $multilanguage |
||
|
|||
41 | */ |
||
42 | 32 | public function __construct(/*ContainerInterface|RequestStack*/ $requestStack, $multilanguage = null, $defaultLocale = null, $requiredLocales = null, AdminRouteHelper $adminRouteHelper = null, EntityManagerInterface $em = null, array $hosts = null) |
|
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | 25 | public function getHost() |
|
84 | |||
85 | /** |
||
86 | * @return array |
||
87 | */ |
||
88 | 1 | public function getHosts() |
|
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | 2 | View Code Duplication | public function getDefaultLocale() |
105 | |||
106 | /** |
||
107 | * @param string|null $host |
||
108 | * |
||
109 | * @return bool |
||
110 | */ |
||
111 | 3 | View Code Duplication | public function isMultiLanguage($host = null) |
123 | |||
124 | /** |
||
125 | * @param string|null $host |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | 3 | View Code Duplication | public function getFrontendLocales($host = null) |
139 | |||
140 | /** |
||
141 | * @param string|null $host |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | 3 | View Code Duplication | public function getBackendLocales($host = null) |
155 | |||
156 | /** |
||
157 | * @return bool |
||
158 | */ |
||
159 | 5 | public function isMultiDomainHost() |
|
165 | |||
166 | /** |
||
167 | * Fetch the root node for the current host |
||
168 | * |
||
169 | * @param string|null $host |
||
170 | * |
||
171 | * @return Node|null |
||
172 | */ |
||
173 | 2 | public function getRootNode($host = null) |
|
189 | |||
190 | /** |
||
191 | * Return (optional) extra config settings for the current host |
||
192 | */ |
||
193 | 2 | View Code Duplication | public function getExtraData() |
203 | |||
204 | /** |
||
205 | * Return (optional) extra config settings for the locales for the current host |
||
206 | */ |
||
207 | 1 | View Code Duplication | public function getLocalesExtraData() |
217 | |||
218 | /** |
||
219 | * @return bool |
||
220 | */ |
||
221 | 25 | View Code Duplication | protected function hasHostOverride() |
230 | |||
231 | /** |
||
232 | * @return bool |
||
233 | */ |
||
234 | 2 | View Code Duplication | public function hasHostSwitched() |
243 | |||
244 | /** |
||
245 | * @return string|null |
||
246 | */ |
||
247 | 3 | protected function getHostOverride() |
|
255 | |||
256 | /** |
||
257 | * @return array |
||
258 | */ |
||
259 | 1 | public function getHostSwitched() |
|
271 | |||
272 | /** |
||
273 | * @return array |
||
274 | */ |
||
275 | 1 | public function getFullHostConfig() |
|
279 | |||
280 | /** |
||
281 | * @param string|null $host |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | 2 | public function getFullHost($host = null) |
|
295 | |||
296 | /** |
||
297 | * @param string|int $id |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | 1 | public function getFullHostById($id) |
|
313 | |||
314 | /** |
||
315 | * @param string|null $host |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | 1 | public function getHostBaseUrl($host = null) |
|
325 | |||
326 | /** |
||
327 | * @param string|null $host |
||
328 | * |
||
329 | * @return string|null |
||
330 | */ |
||
331 | 12 | private function getRealHost($host = null) |
|
339 | } |
||
340 |
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.