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:
1 | <?php |
||
17 | class DomainBasedLocaleRouter extends SlugRouter |
||
18 | { |
||
19 | /** @var RouteCollection */ |
||
20 | protected $routeCollectionMultiLanguage; |
||
21 | |||
22 | /** |
||
23 | * @var string|null |
||
24 | */ |
||
25 | private $otherSite; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $cachedNodeTranslations = []; |
||
31 | |||
32 | /** |
||
33 | * Generate an url for a supplied route |
||
34 | * |
||
35 | * @param string $name The path |
||
36 | * @param array $parameters The route parameters |
||
37 | * @param int|bool $referenceType The type of reference to be generated (one of the UrlGeneratorInterface constants) |
||
38 | * |
||
39 | * @return null|string |
||
40 | */ |
||
41 | 3 | public function generate($name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
|
42 | { |
||
43 | 3 | if ('_slug' === $name && $this->isMultiLanguage() && $this->isMultiDomainHost()) { |
|
44 | 3 | $locale = isset($parameters['_locale']) ? $parameters['_locale'] : $this->getRequestLocale(); |
|
45 | |||
46 | 3 | $reverseLocaleMap = $this->getReverseLocaleMap(); |
|
47 | 3 | if (isset($reverseLocaleMap[$locale])) { |
|
48 | 3 | $parameters['_locale'] = $reverseLocaleMap[$locale]; |
|
49 | } |
||
50 | } |
||
51 | |||
52 | 3 | if (isset($parameters['otherSite'])) { |
|
53 | 1 | $this->otherSite = $this->domainConfiguration->getFullHostById($parameters['otherSite']); |
|
54 | } else { |
||
55 | 3 | $this->otherSite = null; |
|
56 | } |
||
57 | |||
58 | 3 | $this->urlGenerator = new UrlGenerator( |
|
59 | 3 | $this->getRouteCollection(), |
|
60 | 3 | $this->getContext() |
|
61 | ); |
||
62 | |||
63 | 3 | if (isset($parameters['otherSite'])) { |
|
64 | 1 | unset($parameters['otherSite']); |
|
65 | } |
||
66 | |||
67 | 3 | return $this->urlGenerator->generate($name, $parameters, $referenceType); |
|
|
|||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string $pathinfo |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | 2 | public function match($pathinfo) |
|
76 | { |
||
77 | 2 | $urlMatcher = new UrlMatcher( |
|
78 | 2 | $this->getRouteCollection(), |
|
79 | 2 | $this->getContext() |
|
80 | ); |
||
81 | |||
82 | 2 | $result = $urlMatcher->match($pathinfo); |
|
83 | 2 | if (!empty($result)) { |
|
84 | // Remap locale for front-end requests |
||
85 | 2 | if ($this->isMultiDomainHost() && |
|
86 | 2 | $this->isMultiLanguage() && |
|
87 | 2 | !$result['preview'] |
|
88 | ) { |
||
89 | 2 | $localeMap = $this->getLocaleMap(); |
|
90 | 2 | $locale = $result['_locale']; |
|
91 | 2 | $result['_locale'] = $localeMap[$locale]; |
|
92 | } |
||
93 | |||
94 | 2 | $nodeTranslation = $this->getNodeTranslation($result); |
|
95 | 2 | if (\is_null($nodeTranslation)) { |
|
96 | 1 | throw new ResourceNotFoundException( |
|
97 | 1 | 'No page found for slug ' . $pathinfo |
|
98 | ); |
||
99 | } |
||
100 | 1 | $result['_nodeTranslation'] = $nodeTranslation; |
|
101 | } |
||
102 | |||
103 | 1 | return $result; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | 2 | protected function getRequestLocale() |
|
110 | { |
||
111 | 2 | $request = $this->getMasterRequest(); |
|
112 | 2 | $locale = $this->getDefaultLocale(); |
|
113 | 2 | if (!\is_null($request)) { |
|
114 | 2 | $locale = $request->getLocale(); |
|
115 | } |
||
116 | |||
117 | 2 | return $locale; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param array $matchResult |
||
122 | * |
||
123 | * @return \Kunstmaan\NodeBundle\Entity\NodeTranslation |
||
124 | */ |
||
125 | 2 | protected function getNodeTranslation($matchResult) |
|
126 | { |
||
127 | 2 | $key = $matchResult['_controller'].$matchResult['url'].$matchResult['_locale'].$matchResult['_route']; |
|
128 | 2 | if (!isset($this->cachedNodeTranslations[$key])) { |
|
129 | 2 | $rootNode = $this->domainConfiguration->getRootNode(); |
|
130 | |||
131 | // Lookup node translation |
||
132 | 2 | $nodeTranslationRepo = $this->getNodeTranslationRepository(); |
|
133 | |||
134 | /* @var NodeTranslation $nodeTranslation */ |
||
135 | 2 | $nodeTranslation = $nodeTranslationRepo->getNodeTranslationForUrl( |
|
136 | 2 | $matchResult['url'], |
|
137 | 2 | $matchResult['_locale'], |
|
138 | 2 | false, |
|
139 | 2 | null, |
|
140 | 2 | $rootNode |
|
141 | ); |
||
142 | 2 | $this->cachedNodeTranslations[$key] = $nodeTranslation; |
|
143 | } |
||
144 | |||
145 | 2 | return $this->cachedNodeTranslations[$key]; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * @return bool |
||
150 | */ |
||
151 | 5 | private function isMultiDomainHost() |
|
152 | { |
||
153 | 5 | return $this->domainConfiguration->isMultiDomainHost(); |
|
154 | } |
||
155 | |||
156 | 6 | private function getHostLocales() |
|
157 | { |
||
158 | 6 | return $this->domainConfiguration->getFrontendLocales($this->otherSite['host']); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * @return array |
||
163 | */ |
||
164 | 2 | private function getLocaleMap() |
|
165 | { |
||
166 | 2 | return array_combine( |
|
167 | 2 | $this->getFrontendLocales(), |
|
168 | 2 | $this->getBackendLocales() |
|
169 | ); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return array |
||
174 | */ |
||
175 | 3 | private function getReverseLocaleMap() |
|
176 | { |
||
177 | 3 | return array_combine( |
|
178 | 3 | $this->getBackendLocales(), |
|
179 | 3 | $this->getFrontendLocales() |
|
180 | ); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Getter for routeCollection |
||
185 | * |
||
186 | * Override slug router |
||
187 | * |
||
188 | * @return \Symfony\Component\Routing\RouteCollection |
||
189 | */ |
||
190 | 7 | public function getRouteCollection() |
|
191 | { |
||
192 | 7 | if (($this->otherSite && $this->isMultiLanguage($this->otherSite['host'])) || (!$this->otherSite && $this->isMultiLanguage())) { |
|
193 | 6 | if (!$this->routeCollectionMultiLanguage) { |
|
194 | 6 | $this->routeCollectionMultiLanguage = new RouteCollection(); |
|
195 | |||
196 | 6 | $this->addMultiLangPreviewRoute(); |
|
197 | 6 | $this->addMultiLangSlugRoute(); |
|
198 | } |
||
199 | |||
200 | 6 | return $this->routeCollectionMultiLanguage; |
|
201 | } |
||
202 | |||
203 | 1 | if (!$this->routeCollection) { |
|
204 | 1 | $this->routeCollection = new RouteCollection(); |
|
205 | |||
206 | 1 | $this->addPreviewRoute(); |
|
207 | 1 | $this->addSlugRoute(); |
|
208 | } |
||
209 | |||
210 | 1 | return $this->routeCollection; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * Add the slug route to the route collection |
||
215 | */ |
||
216 | 1 | protected function addSlugRoute() |
|
221 | |||
222 | /** |
||
223 | * Add the slug route to the route collection |
||
224 | */ |
||
225 | 6 | protected function addMultiLangPreviewRoute() |
|
230 | |||
231 | /** |
||
232 | * Add the slug route to the route collection multilanguage |
||
233 | */ |
||
234 | 6 | protected function addMultiLangSlugRoute() |
|
239 | |||
240 | /** |
||
241 | * @param string $name |
||
242 | * @param array $parameters |
||
243 | */ |
||
244 | 6 | View Code Duplication | protected function addMultiLangRoute($name, array $parameters = array()) |
255 | |||
256 | /** |
||
257 | * Return slug route parameters |
||
258 | * |
||
259 | * @return array |
||
260 | */ |
||
261 | 7 | protected function getSlugRouteParameters() |
|
296 | } |
||
297 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.