Total Complexity | 59 |
Total Lines | 532 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Languagemenu 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.
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 Languagemenu, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Languagemenu extends \UIComponents\View\Helper\AbstractHelper |
||
23 | |||
24 | { |
||
25 | use ComponentAttributesTrait; |
||
26 | |||
27 | /** |
||
28 | * @var Detector $detector |
||
29 | */ |
||
30 | protected $detector; |
||
31 | |||
32 | /** |
||
33 | * Set the class to be used on the list container |
||
34 | * |
||
35 | * @var string || null |
||
36 | */ |
||
37 | protected $class; |
||
38 | |||
39 | /** |
||
40 | * Method used to construct a title for each item |
||
41 | * |
||
42 | * @var string || null |
||
43 | */ |
||
44 | protected $titleMethod = 'displayLanguage'; |
||
45 | |||
46 | /** |
||
47 | * Flag to specify specifies whether the title should be in the current locale |
||
48 | * |
||
49 | * @var boolean default false |
||
50 | */ |
||
51 | protected $titleInCurrentLocale = false; |
||
52 | |||
53 | /** |
||
54 | * Method used to construct a label for each item |
||
55 | * |
||
56 | * @var string || null |
||
57 | */ |
||
58 | protected $labelMethod = 'displayLanguage'; |
||
59 | |||
60 | /** |
||
61 | * Flag to specify specifies whether the label should be in the current locale |
||
62 | * |
||
63 | * @var boolean default true |
||
64 | */ |
||
65 | protected $labelInCurrentLocale = true; |
||
66 | |||
67 | /** |
||
68 | * Flag to specify the current locale should be omitted from the menu |
||
69 | * |
||
70 | * @var boolean default false |
||
71 | */ |
||
72 | protected $omitCurrent = false; |
||
73 | |||
74 | /** |
||
75 | * default CSS class to use for li elements |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $defaultLiClass = ''; |
||
80 | |||
81 | /** |
||
82 | * CSS class to use for the ul sub-menu element |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $subUlClass = 'dropdown-menu'; |
||
87 | |||
88 | /** |
||
89 | * CSS class to use for the 1. level (NOT root level!) ul sub-menu element |
||
90 | * |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $subUlClassLevel1 = 'dropdown-menu'; |
||
94 | |||
95 | /** |
||
96 | * CSS class to use for the active li sub-menu element |
||
97 | * |
||
98 | * @var string |
||
99 | */ |
||
100 | protected $subLiClass = 'dropdown-submenu'; |
||
101 | |||
102 | /** |
||
103 | * CSS class to use for the active li sub-menu element |
||
104 | * |
||
105 | * @var string |
||
106 | */ |
||
107 | protected $subLiClassLevel0 = 'dropdown'; |
||
108 | |||
109 | /** |
||
110 | * CSS class prefix to use for the menu element's icon class |
||
111 | * |
||
112 | * @var string |
||
113 | */ |
||
114 | protected $iconPrefixClass = 'icon-'; |
||
115 | |||
116 | /** |
||
117 | * HREF string to use for the sub-menu toggle element's HREF attribute, |
||
118 | * to override current page's href/'htmlify' setting |
||
119 | * |
||
120 | * @var string |
||
121 | */ |
||
122 | protected $hrefSubToggleOverride = null; |
||
123 | |||
124 | /** |
||
125 | * Partial view script to use for rendering menu link/item |
||
126 | * |
||
127 | * @var string|array |
||
128 | */ |
||
129 | protected $htmlifyPartial = null; |
||
130 | |||
131 | /** |
||
132 | * @param Detector $detector |
||
133 | */ |
||
134 | public function setDetector($detector) |
||
135 | { |
||
136 | $this->detector = $detector; |
||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return Detector $detector |
||
142 | */ |
||
143 | public function getDetector() |
||
144 | { |
||
145 | if (!$this->detector) { |
||
146 | $serviceLocator = $this->getServiceLocator(); |
||
147 | if ($serviceLocator instanceof HelperPluginManager) { |
||
148 | $serviceLocator = $serviceLocator->getServiceLocator(); |
||
149 | } |
||
150 | $this->detector = $serviceLocator->get('SlmLocale\Locale\Detector'); |
||
151 | } |
||
152 | return $this->detector; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param string $class |
||
157 | */ |
||
158 | public function setUlClass($class) |
||
159 | { |
||
160 | $this->class = $class; |
||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getUlClass() |
||
168 | { |
||
169 | return $this->class; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param string $itemTitleMethod |
||
174 | */ |
||
175 | public function setTitleMethod($titleMethod) |
||
176 | { |
||
177 | $this->checkLocaleMethod($titleMethod); |
||
178 | |||
179 | $this->titleMethod = $titleMethod; |
||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getTitleMethod() |
||
187 | { |
||
188 | return $this->titleMethod; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param boolean $flag |
||
193 | */ |
||
194 | public function setTitleInCurrentLocale($flag) |
||
195 | { |
||
196 | $this->titleInCurrentLocale = (bool) $flag; |
||
197 | return $this; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @return boolean |
||
202 | */ |
||
203 | public function getTitleInCurrentLocale() |
||
204 | { |
||
205 | return $this->titleInCurrentLocale; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param string $labelMethod |
||
210 | */ |
||
211 | public function setLabelMethod($labelMethod) |
||
212 | { |
||
213 | $this->checkLocaleMethod($labelMethod); |
||
214 | |||
215 | $this->labelMethod = $labelMethod; |
||
216 | return $this; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @return string |
||
221 | */ |
||
222 | public function getLabelMethod() |
||
223 | { |
||
224 | return $this->labelMethod; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param boolean $flag |
||
229 | */ |
||
230 | public function setLabelInCurrentLocale($flag) |
||
231 | { |
||
232 | $this->labelInCurrentLocale = (bool) $flag; |
||
233 | return $this; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @return boolean |
||
238 | */ |
||
239 | public function getLabelInCurrentLocale() |
||
240 | { |
||
241 | return $this->labelInCurrentLocale; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param boolean $omitCurrent |
||
246 | */ |
||
247 | public function setOmitCurrent($omitCurrent) |
||
248 | { |
||
249 | $this->omitCurrent = (bool) $omitCurrent; |
||
250 | return $this; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @return boolean |
||
255 | */ |
||
256 | public function omitCurrent() |
||
257 | { |
||
258 | return $this->omitCurrent; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @return the $defaultLiClass |
||
263 | */ |
||
264 | public function getDefaultLiClass() { |
||
265 | return $this->defaultLiClass; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param string $defaultLiClass |
||
270 | */ |
||
271 | public function setDefaultLiClass($defaultLiClass) { |
||
272 | $this->defaultLiClass = $defaultLiClass; |
||
273 | return $this; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @return the $subUlClass |
||
278 | */ |
||
279 | public function getSubUlClass() { |
||
280 | return $this->subUlClass; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param string $subUlClass |
||
285 | */ |
||
286 | public function setSubUlClass($subUlClass) { |
||
287 | $this->subUlClass = $subUlClass; |
||
288 | return $this; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @return the $subUlClassLevel1 |
||
293 | */ |
||
294 | public function getSubUlClassLevel1() { |
||
295 | return $this->subUlClassLevel1; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @param string $subUlClassLevel1 |
||
300 | */ |
||
301 | public function setSubUlClassLevel1($subUlClassLevel1) { |
||
302 | $this->subUlClassLevel1 = $subUlClassLevel1; |
||
303 | return $this; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @return the $subLiClass |
||
308 | */ |
||
309 | public function getSubLiClass() { |
||
310 | return $this->subLiClass; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * @param string $subLiClass |
||
315 | */ |
||
316 | public function setSubLiClass($subLiClass) { |
||
317 | $this->subLiClass = $subLiClass; |
||
318 | return $this; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @return the $subLiClassLevel0 |
||
323 | */ |
||
324 | public function getSubLiClassLevel0() { |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @param string $subLiClassLevel0 |
||
330 | */ |
||
331 | public function setSubLiClassLevel0($subLiClassLevel0) { |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @return the $iconPrefixClass |
||
338 | */ |
||
339 | public function getIconPrefixClass() { |
||
340 | return $this->iconPrefixClass; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @param string $iconPrefixClass |
||
345 | */ |
||
346 | public function setIconPrefixClass($iconPrefixClass) { |
||
347 | $this->iconPrefixClass = $iconPrefixClass; |
||
348 | return $this; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * @return the $hrefSubToggleOverride |
||
353 | */ |
||
354 | public function getHrefSubToggleOverride() { |
||
355 | return $this->hrefSubToggleOverride; |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * @param string $hrefSubToggleOverride |
||
360 | */ |
||
361 | public function setHrefSubToggleOverride($hrefSubToggleOverride) { |
||
362 | $this->hrefSubToggleOverride = $hrefSubToggleOverride; |
||
363 | return $this; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Sets which partial view script to use for rendering menu |
||
368 | * |
||
369 | * @param string|array $partial partial view script or null. If an array is |
||
370 | * given, it is expected to contain two |
||
371 | * values; the partial view script to use, |
||
372 | * and the module where the script can be |
||
373 | * found. |
||
374 | * @return self |
||
375 | */ |
||
376 | public function setHtmlifyPartial($partial) |
||
377 | { |
||
378 | if (null === $partial || is_string($partial) || is_array($partial)) { |
||
379 | $this->htmlifyPartial = $partial; |
||
380 | } |
||
381 | |||
382 | return $this; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Returns partial view script to use for rendering menu |
||
387 | * |
||
388 | * @return string|array|null |
||
389 | */ |
||
390 | public function getHtmlifyPartial() |
||
391 | { |
||
392 | return $this->htmlifyPartial; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * View helper entry point: |
||
397 | * Retrieves helper and optionally sets component options to operate on |
||
398 | * |
||
399 | * @param array|StdClass $options [optional] component options to operate on |
||
400 | * @return self |
||
401 | */ |
||
402 | public function __invoke($options = array()) |
||
403 | { |
||
404 | parent::__invoke($options); |
||
405 | return $this; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * render component |
||
410 | * |
||
411 | * @param boolean $output |
||
412 | * |
||
413 | * @return string |
||
414 | */ |
||
415 | public function render($output = false) |
||
416 | { |
||
417 | try { |
||
418 | |||
419 | if ($output) { |
||
420 | echo $this->buildComponent(); |
||
421 | } |
||
422 | return $this->buildComponent(); |
||
423 | |||
424 | } catch (\Exception $e) { |
||
425 | |||
426 | $msg = get_class($e) . ': ' . $e->getMessage() . "\n" . $e->getTraceAsString(); |
||
427 | trigger_error($msg, E_USER_ERROR); |
||
428 | return ''; |
||
429 | |||
430 | } |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * build markup |
||
435 | * |
||
436 | * @return string |
||
437 | */ |
||
438 | public function buildComponent() |
||
439 | { |
||
440 | if (!($detector = $this->getDetector())) { |
||
441 | throw new \RuntimeException('To assemble an url, a detector is required'); |
||
442 | } |
||
443 | |||
444 | $class = $this->getUlClass(); |
||
445 | $liclass = $this->getSubLiClassLevel0(); |
||
446 | $subulclass = $this->getSubUlClassLevel1(); |
||
447 | $iconprefixclass = $this->getIconPrefixClass(); |
||
448 | |||
449 | $list = ''; |
||
450 | $current = Locale::getDefault(); |
||
451 | foreach($detector->getSupported() as $locale) { |
||
452 | if ($this->omitCurrent() && $current === $locale) { |
||
453 | continue; |
||
454 | } |
||
455 | |||
456 | $titleLocale = $this->getTitleInCurrentLocale() ? $locale : $current; |
||
457 | $labelLocale = $this->getLabelInCurrentLocale() ? $locale : $current; |
||
458 | |||
459 | $url = $this->getView()->localeUrl($locale); |
||
460 | $title = $this->getLocaleProperty($this->getTitleMethod(), $locale, $titleLocale); |
||
461 | $label = $this->getLocaleProperty($this->getLabelMethod(), $locale, $labelLocale); |
||
462 | $primary = $this->getLocaleProperty('primaryLanguage', $locale, true); |
||
463 | $displayName = $this->getLocaleProperty('displayName', $locale, $labelLocale); |
||
464 | |||
465 | $item = sprintf( |
||
466 | '<li><a href="%s" title="%s"%s %s>%s</a></li>' . "\n", |
||
467 | $url, |
||
468 | $displayName, |
||
469 | ($current === $locale) ? ' class="active"' : '', |
||
470 | 'data-test="cta-lang-'.$this->slugify($label).'"', |
||
471 | (($iconprefixclass) ? '<span class="' . $iconprefixclass . $primary . '"></span> ' : '') . $label |
||
472 | ); |
||
473 | |||
474 | $list .= $item; |
||
475 | } |
||
476 | $attributes = $this->getAttributes(); |
||
477 | $html = |
||
478 | '<ul'.(($class) ? sprintf(' class="%s"', $class) : '').' '.($this->htmlAttribs($attributes)).'>'. |
||
479 | '<li'.(($liclass) ? sprintf(' class="%s"', $liclass) : '').'>'. |
||
480 | '<a href="" '. |
||
481 | 'class="'.(($liclass) ? $liclass.'-toggle' : '').'" '. |
||
482 | 'data-toggle="'.(($liclass) ? $liclass : '').'" '. |
||
483 | 'role="button" aria-haspopup="true" '. |
||
484 | 'aria-expanded="false" '. |
||
485 | 'title="'.Locale::getDisplayName(null).'"'. |
||
486 | 'data-test="cta-lang-selector cta-lang-selected-'.$this->slugify(Locale::getDisplayLanguage(null)).'"'. |
||
487 | '>'. |
||
488 | '<span class="'.(($iconprefixclass) ? $iconprefixclass : '').Locale::getPrimaryLanguage(null).'"></span> '. |
||
489 | ''.Locale::getDisplayLanguage(null). // ' - '.Locale::getDefault().' - '.Locale::getPrimaryLanguage(null).''. |
||
490 | '<span class="caret"></span>'. |
||
491 | '</a>'. |
||
492 | sprintf( |
||
493 | '<ul%s>%s</ul>', |
||
494 | ($subulclass) ? sprintf(' class="%s"', $subulclass) : '', |
||
495 | $list |
||
496 | ). |
||
497 | '</li>'. |
||
498 | '</ul>' |
||
499 | ; |
||
500 | |||
501 | return $html; |
||
502 | |||
503 | return '<h2>'.__CLASS__.'</h2>'; |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * Check whether method part of the Locale class is |
||
508 | * |
||
509 | * @param string $method Method to check |
||
510 | * @throws RuntimeException If method is not part of locale |
||
511 | * @return true |
||
512 | */ |
||
513 | protected function checkLocaleMethod($method) |
||
531 | )); |
||
532 | } |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * Retrieves a value by property from Locale |
||
537 | * |
||
538 | * @param $property |
||
539 | * @param $locale |
||
540 | * @param bool $in_locale |
||
541 | * @return mixed |
||
542 | */ |
||
543 | protected function getLocaleProperty($property, $locale, $in_locale = false) |
||
544 | { |
||
545 | $callback = sprintf('\Locale::get%s', ucfirst($property)); |
||
546 | |||
547 | $args = array($locale); |
||
548 | |||
549 | if ($in_locale && !in_array($property, array('primaryLanguage', 'region', 'script'))) { |
||
550 | $args[] = $in_locale; |
||
551 | } |
||
552 | |||
553 | return call_user_func_array($callback, $args); |
||
554 | } |
||
555 | |||
556 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths