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 = null; |
||
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 | /** |
||
37 | * @param ContainerInterface|string $multilanguage |
||
38 | */ |
||
39 | public function __construct(/*ContainerInterface|string*/ $multilanguage, $defaultLocale = null, $requiredLocales = null, AdminRouteHelper $adminRouteHelper = null, EntityManagerInterface $em = null, array $hosts = null) |
||
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getHost() |
||
82 | |||
83 | /** |
||
84 | * @return array |
||
85 | */ |
||
86 | public function getHosts() |
||
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | View Code Duplication | public function getDefaultLocale() |
|
103 | |||
104 | /** |
||
105 | * @param string|null $host |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | View Code Duplication | public function isMultiLanguage($host = null) |
|
121 | |||
122 | /** |
||
123 | * @param string|null $host |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | View Code Duplication | public function getFrontendLocales($host = null) |
|
137 | |||
138 | /** |
||
139 | * @param string|null $host |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | View Code Duplication | public function getBackendLocales($host = null) |
|
153 | |||
154 | /** |
||
155 | * @return bool |
||
156 | */ |
||
157 | public function isMultiDomainHost() |
||
163 | |||
164 | /** |
||
165 | * Fetch the root node for the current host |
||
166 | * |
||
167 | * @param string|null $host |
||
168 | * |
||
169 | * @return Node|null |
||
170 | */ |
||
171 | public function getRootNode($host = null) |
||
187 | |||
188 | /** |
||
189 | * Return (optional) extra config settings for the current host |
||
190 | */ |
||
191 | View Code Duplication | public function getExtraData() |
|
201 | |||
202 | /** |
||
203 | * Return (optional) extra config settings for the locales for the current host |
||
204 | */ |
||
205 | View Code Duplication | public function getLocalesExtraData() |
|
215 | |||
216 | /** |
||
217 | * @return bool |
||
218 | */ |
||
219 | View Code Duplication | protected function hasHostOverride() |
|
228 | |||
229 | /** |
||
230 | * @return bool |
||
231 | */ |
||
232 | View Code Duplication | public function hasHostSwitched() |
|
241 | |||
242 | /** |
||
243 | * @return string|null |
||
244 | */ |
||
245 | protected function getHostOverride() |
||
253 | |||
254 | /** |
||
255 | * @return array |
||
256 | */ |
||
257 | public function getHostSwitched() |
||
269 | |||
270 | /** |
||
271 | * @return array |
||
272 | */ |
||
273 | public function getFullHostConfig() |
||
277 | |||
278 | /** |
||
279 | * @param string|null $host |
||
280 | * |
||
281 | * @return array |
||
282 | */ |
||
283 | public function getFullHost($host = null) |
||
293 | |||
294 | |||
295 | /** |
||
296 | * @param int $id |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | public function getFullHostById($id) |
||
312 | |||
313 | /** |
||
314 | * @param string|null $host |
||
315 | * |
||
316 | * @return string |
||
317 | */ |
||
318 | public function getHostBaseUrl($host = null) |
||
324 | |||
325 | /** |
||
326 | * @param string|null $host |
||
327 | * |
||
328 | * @return null|string |
||
329 | */ |
||
330 | private function getRealHost($host = null) |
||
338 | } |
||
339 |
If you suppress an error, we recommend checking for the error condition explicitly: