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 | /** |
||
| 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) |
|
| 174 | { |
||
| 175 | 2 | if (!$this->isMultiDomainHost()) { |
|
| 176 | 1 | return parent::getRootNode(); |
|
| 177 | } |
||
| 178 | |||
| 179 | 1 | $host = $this->getRealHost($host); |
|
| 180 | 1 | if (null === $host) { |
|
| 181 | return null; |
||
| 182 | } |
||
| 183 | |||
| 184 | 1 | if (!array_key_exists($host, $this->rootNodeCache)) { |
|
| 185 | 1 | $internalName = $this->hosts[$host]['root']; |
|
| 186 | 1 | $nodeRepo = $this->em->getRepository('KunstmaanNodeBundle:Node'); |
|
| 187 | 1 | $this->rootNodeCache[$host] = $nodeRepo->getNodeByInternalName($internalName); |
|
| 188 | |||
| 189 | // Keep BC by setting the first node found. |
||
| 190 | 1 | if (null === $this->rootNode) { |
|
| 191 | 1 | $this->rootNode = $this->rootNodeCache[$host]; |
|
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | 1 | return $this->rootNodeCache[$host]; |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return (optional) extra config settings for the current host |
||
| 200 | */ |
||
| 201 | 2 | View Code Duplication | public function getExtraData() |
| 202 | { |
||
| 203 | 2 | $host = $this->getHost(); |
|
| 204 | |||
| 205 | 2 | if (!isset($this->hosts[$host]['extra'])) { |
|
| 206 | 1 | return parent::getExtraData(); |
|
| 207 | } |
||
| 208 | |||
| 209 | 1 | return $this->hosts[$host]['extra']; |
|
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Return (optional) extra config settings for the locales for the current host |
||
| 214 | */ |
||
| 215 | 1 | View Code Duplication | public function getLocalesExtraData() |
| 225 | |||
| 226 | /** |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | 25 | View Code Duplication | protected function hasHostOverride() |
| 230 | { |
||
| 231 | 25 | $request = $this->getMasterRequest(); |
|
| 232 | |||
| 233 | 25 | return !\is_null($request) && |
|
| 234 | 25 | $this->adminRouteHelper->isAdminRoute($request->getRequestUri()) && |
|
| 235 | 25 | $request->hasPreviousSession() && |
|
| 236 | 25 | $request->getSession()->has(self::OVERRIDE_HOST); |
|
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | 2 | View Code Duplication | public function hasHostSwitched() |
| 243 | { |
||
| 244 | 2 | $request = $this->getMasterRequest(); |
|
| 245 | |||
| 246 | 2 | return !\is_null($request) && |
|
| 247 | 2 | $this->adminRouteHelper->isAdminRoute($request->getRequestUri()) && |
|
| 248 | 2 | $request->hasPreviousSession() && |
|
| 249 | 2 | $request->getSession()->has(self::SWITCH_HOST); |
|
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return string|null |
||
| 254 | */ |
||
| 255 | 3 | protected function getHostOverride() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | 1 | public function getHostSwitched() |
|
| 268 | { |
||
| 269 | 1 | $request = $this->getMasterRequest(); |
|
| 270 | |||
| 271 | 1 | $host = $this->getHost(); |
|
| 272 | |||
| 273 | 1 | if ($this->hasHostSwitched()) { |
|
| 274 | 1 | $host = $request->getSession()->get(self::SWITCH_HOST); |
|
| 275 | } |
||
| 276 | |||
| 277 | 1 | return $this->hosts[$host]; |
|
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | 1 | public function getFullHostConfig() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param string|null $host |
||
| 290 | * |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | 2 | public function getFullHost($host = null) |
|
| 294 | { |
||
| 295 | 2 | $host = $this->getRealHost($host); |
|
| 296 | |||
| 297 | 2 | if ($host && isset($this->hosts[$host])) { |
|
| 298 | 1 | return $this->hosts[$host]; |
|
| 299 | } |
||
| 300 | |||
| 301 | 1 | return null; |
|
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string|int $id |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | 1 | public function getFullHostById($id) |
|
| 310 | { |
||
| 311 | 1 | foreach ($this->hosts as $host => $parameters) { |
|
| 312 | 1 | if (!isset($parameters['id']) || $parameters['id'] !== $id) { |
|
| 313 | 1 | continue; |
|
| 314 | } |
||
| 315 | |||
| 316 | 1 | return $parameters; |
|
| 317 | } |
||
| 318 | |||
| 319 | 1 | return null; |
|
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string|null $host |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | 1 | public function getHostBaseUrl($host = null) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param string|null $host |
||
| 336 | * |
||
| 337 | * @return string|null |
||
| 338 | */ |
||
| 339 | 12 | private function getRealHost($host = null) |
|
| 340 | { |
||
| 347 | } |
||
| 348 |
This check looks for
@paramannotations 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.