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 |
||
20 | trait LocaleAwareTrait |
||
21 | { |
||
22 | /** |
||
23 | * Available languages as defined by the application configset. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $locales = []; |
||
28 | |||
29 | /** |
||
30 | * Store the processed link structures to translations |
||
31 | * for the current route, if any. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $alternateTranslations; |
||
36 | |||
37 | /** |
||
38 | * Retrieve the available locales. |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | protected function locales() |
||
46 | |||
47 | /** |
||
48 | * Set the available locales. |
||
49 | * |
||
50 | * @param array $locales The list of language structures. |
||
51 | * @return self |
||
52 | */ |
||
53 | protected function setLocales(array $locales) |
||
62 | |||
63 | /** |
||
64 | * Parse the given locale. |
||
65 | * |
||
66 | * @see \Charcoal\Admin\Widget\FormSidebarWidget::languages() |
||
67 | * @see \Charcoal\Admin\Widget\FormGroupWidget::languages() |
||
68 | * @param array $localeStruct The language structure. |
||
69 | * @param string $langCode The language code. |
||
70 | * @throws InvalidArgumentException If the locale does not have a language code. |
||
71 | * @return array |
||
72 | */ |
||
73 | private function parseLocale(array $localeStruct, $langCode) |
||
107 | |||
108 | /** |
||
109 | * Retrieve the translator service. |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | protected function availableLanguages() |
||
117 | |||
118 | /** |
||
119 | * Build the alternate translations associated with the current route. |
||
120 | * |
||
121 | * This method _excludes_ the current route's canonical URI. |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | protected function buildAlternateTranslations() |
||
148 | |||
149 | /** |
||
150 | * Retrieve the alternate translations associated with the current route. |
||
151 | * |
||
152 | * This method _excludes_ the current route's canonical URI. |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | protected function getAlternateTranslations() |
||
164 | |||
165 | /** |
||
166 | * Format an alternate translation for the given translatable model. |
||
167 | * |
||
168 | * Note: The application's locale is already modified and will be reset |
||
169 | * after processing all available languages. |
||
170 | * |
||
171 | * @param mixed $context The translated {@see \Charcoal\Model\ModelInterface model} |
||
172 | * or array-accessible structure. |
||
173 | * @param array $localeStruct The currently iterated language. |
||
174 | * @return array Returns a link structure. |
||
175 | */ |
||
176 | protected function formatAlternateTranslation($context, array $localeStruct) |
||
188 | |||
189 | /** |
||
190 | * Format an alternate translation URL for the given translatable model. |
||
191 | * |
||
192 | * Note: The application's locale is already modified and will be reset |
||
193 | * after processing all available languages. |
||
194 | * |
||
195 | * @param mixed $context The translated {@see \Charcoal\Model\ModelInterface model} |
||
196 | * or array-accessible structure. |
||
197 | * @param array $localeStruct The currently iterated language. |
||
198 | * @return string Returns a link. |
||
199 | */ |
||
200 | protected function formatAlternateTranslationUrl($context, array $localeStruct) |
||
212 | |||
213 | /** |
||
214 | * Yield the alternate translations associated with the current route. |
||
215 | * |
||
216 | * @return Generator|null |
||
217 | */ |
||
218 | public function alternateTranslations() |
||
224 | |||
225 | /** |
||
226 | * Determine if there exists alternate translations associated with the current route. |
||
227 | * |
||
228 | * @return boolean |
||
229 | */ |
||
230 | public function hasAlternateTranslations() |
||
234 | |||
235 | /** |
||
236 | * Retrieve the translator service. |
||
237 | * |
||
238 | * @see \Charcoal\Translator\TranslatorAwareTrait |
||
239 | * @return \Charcoal\Translator\Translator |
||
240 | */ |
||
241 | abstract protected function translator(); |
||
242 | |||
243 | /** |
||
244 | * Retrieve the template's identifier. |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | abstract public function templateName(); |
||
249 | |||
250 | /** |
||
251 | * Retrieve the title of the page (from the context). |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | abstract public function title(); |
||
256 | |||
257 | /** |
||
258 | * Retrieve the current URI of the context. |
||
259 | * |
||
260 | * @return \Psr\Http\Message\UriInterface|string |
||
261 | */ |
||
262 | abstract public function currentUrl(); |
||
263 | |||
264 | /** |
||
265 | * Retrieve the current object relative to the context. |
||
266 | * |
||
267 | * @return \Charcoal\Model\ModelInterface|null |
||
268 | */ |
||
269 | abstract public function contextObject(); |
||
270 | |||
271 | /** |
||
272 | * Retrieve the base URI of the project. |
||
273 | * |
||
274 | * @throws RuntimeException If the base URI is missing. |
||
275 | * @return UriInterface|null |
||
276 | */ |
||
277 | abstract public function baseUrl(); |
||
278 | } |
||
279 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.