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 Languages 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 Languages, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Languages { |
||
28 | /** @var array List of languages */ |
||
29 | protected $mLanguages; |
||
30 | |||
31 | /** @var array Raw list of the messages in each language */ |
||
32 | protected $mRawMessages; |
||
33 | |||
34 | /** @var array Messages in each language (except for English), divided to groups */ |
||
35 | protected $mMessages; |
||
36 | |||
37 | /** @var array Fallback language in each language */ |
||
38 | protected $mFallback; |
||
39 | |||
40 | /** @var array General messages in English, divided to groups */ |
||
41 | protected $mGeneralMessages; |
||
42 | |||
43 | /** @var array All the messages which should be exist only in the English file */ |
||
44 | protected $mIgnoredMessages; |
||
45 | |||
46 | /** @var array All the messages which may be translated or not, depending on the language */ |
||
47 | protected $mOptionalMessages; |
||
48 | |||
49 | /** @var array Namespace names */ |
||
50 | protected $mNamespaceNames; |
||
51 | |||
52 | /** @var array Namespace aliases */ |
||
53 | protected $mNamespaceAliases; |
||
54 | |||
55 | /** @var array Magic words */ |
||
56 | protected $mMagicWords; |
||
57 | |||
58 | /** @var array Special page aliases */ |
||
59 | protected $mSpecialPageAliases; |
||
60 | |||
61 | /** |
||
62 | * Load the list of languages: all the Messages*.php |
||
63 | * files in the languages directory. |
||
64 | */ |
||
65 | function __construct() { |
||
72 | |||
73 | /** |
||
74 | * Get the language list. |
||
75 | * |
||
76 | * @return array The language list. |
||
77 | */ |
||
78 | public function getLanguages() { |
||
79 | return $this->mLanguages; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get the ignored messages list. |
||
84 | * |
||
85 | * @return array The ignored messages list. |
||
86 | */ |
||
87 | public function getIgnoredMessages() { |
||
90 | |||
91 | /** |
||
92 | * Get the optional messages list. |
||
93 | * |
||
94 | * @return array The optional messages list. |
||
95 | */ |
||
96 | public function getOptionalMessages() { |
||
99 | |||
100 | /** |
||
101 | * Load the language file. |
||
102 | * |
||
103 | * @param string $code The language code. |
||
104 | */ |
||
105 | protected function loadFile( $code ) { |
||
148 | |||
149 | /** |
||
150 | * Load the messages for a specific language (which is not English) and divide them to |
||
151 | * groups: |
||
152 | * all - all the messages. |
||
153 | * required - messages which should be translated in order to get a complete translation. |
||
154 | * optional - messages which can be translated, the fallback translation is used if not |
||
155 | * translated. |
||
156 | * obsolete - messages which should not be translated, either because they do not exist, |
||
157 | * or they are ignored messages. |
||
158 | * translated - messages which are either required or optional, but translated from |
||
159 | * English and needed. |
||
160 | * |
||
161 | * @param string $code The language code. |
||
162 | */ |
||
163 | private function loadMessages( $code ) { |
||
186 | |||
187 | /** |
||
188 | * Load the messages for English and divide them to groups: |
||
189 | * all - all the messages. |
||
190 | * required - messages which should be translated to other languages in order to get a |
||
191 | * complete translation. |
||
192 | * optional - messages which can be translated to other languages, but it's not required |
||
193 | * for a complete translation. |
||
194 | * ignored - messages which should not be translated to other languages. |
||
195 | * translatable - messages which are either required or optional, but can be translated |
||
196 | * from English. |
||
197 | */ |
||
198 | private function loadGeneralMessages() { |
||
220 | |||
221 | /** |
||
222 | * Get all the messages for a specific language (not English), without the |
||
223 | * fallback language messages, divided to groups: |
||
224 | * all - all the messages. |
||
225 | * required - messages which should be translated in order to get a complete translation. |
||
226 | * optional - messages which can be translated, the fallback translation is used if not |
||
227 | * translated. |
||
228 | * obsolete - messages which should not be translated, either because they do not exist, |
||
229 | * or they are ignored messages. |
||
230 | * translated - messages which are either required or optional, but translated from |
||
231 | * English and needed. |
||
232 | * |
||
233 | * @param string $code The language code. |
||
234 | * |
||
235 | * @return string The messages in this language. |
||
236 | */ |
||
237 | public function getMessages( $code ) { |
||
242 | |||
243 | /** |
||
244 | * Get all the general English messages, divided to groups: |
||
245 | * all - all the messages. |
||
246 | * required - messages which should be translated to other languages in |
||
247 | * order to get a complete translation. |
||
248 | * optional - messages which can be translated to other languages, but it's |
||
249 | * not required for a complete translation. |
||
250 | * ignored - messages which should not be translated to other languages. |
||
251 | * translatable - messages which are either required or optional, but can be |
||
252 | * translated from English. |
||
253 | * |
||
254 | * @return array The general English messages. |
||
255 | */ |
||
256 | public function getGeneralMessages() { |
||
261 | |||
262 | /** |
||
263 | * Get fallback language code for a specific language. |
||
264 | * |
||
265 | * @param string $code The language code. |
||
266 | * |
||
267 | * @return string Fallback code. |
||
268 | */ |
||
269 | public function getFallback( $code ) { |
||
274 | |||
275 | /** |
||
276 | * Get namespace names for a specific language. |
||
277 | * |
||
278 | * @param string $code The language code. |
||
279 | * |
||
280 | * @return array Namespace names. |
||
281 | */ |
||
282 | public function getNamespaceNames( $code ) { |
||
287 | |||
288 | /** |
||
289 | * Get namespace aliases for a specific language. |
||
290 | * |
||
291 | * @param string $code The language code. |
||
292 | * |
||
293 | * @return array Namespace aliases. |
||
294 | */ |
||
295 | public function getNamespaceAliases( $code ) { |
||
300 | |||
301 | /** |
||
302 | * Get magic words for a specific language. |
||
303 | * |
||
304 | * @param string $code The language code. |
||
305 | * |
||
306 | * @return array Magic words. |
||
307 | */ |
||
308 | public function getMagicWords( $code ) { |
||
313 | |||
314 | /** |
||
315 | * Get special page aliases for a specific language. |
||
316 | * |
||
317 | * @param string $code The language code. |
||
318 | * |
||
319 | * @return array Special page aliases. |
||
320 | */ |
||
321 | public function getSpecialPageAliases( $code ) { |
||
326 | |||
327 | /** |
||
328 | * Get the untranslated messages for a specific language. |
||
329 | * |
||
330 | * @param string $code The language code. |
||
331 | * |
||
332 | * @return array The untranslated messages for this language. |
||
333 | */ |
||
334 | public function getUntranslatedMessages( $code ) { |
||
340 | |||
341 | /** |
||
342 | * Get the duplicate messages for a specific language. |
||
343 | * |
||
344 | * @param string $code The language code. |
||
345 | * |
||
346 | * @return array The duplicate messages for this language. |
||
347 | */ |
||
348 | public function getDuplicateMessages( $code ) { |
||
360 | |||
361 | /** |
||
362 | * Get the obsolete messages for a specific language. |
||
363 | * |
||
364 | * @param string $code The language code. |
||
365 | * |
||
366 | * @return array The obsolete messages for this language. |
||
367 | */ |
||
368 | public function getObsoleteMessages( $code ) { |
||
374 | |||
375 | /** |
||
376 | * Get the messages whose variables do not match the original ones. |
||
377 | * |
||
378 | * @param string $code The language code. |
||
379 | * |
||
380 | * @return array The messages whose variables do not match the original ones. |
||
381 | */ |
||
382 | public function getMessagesWithMismatchVariables( $code ) { |
||
408 | |||
409 | /** |
||
410 | * Get the messages which do not use plural. |
||
411 | * |
||
412 | * @param string $code The language code. |
||
413 | * |
||
414 | * @return array The messages which do not use plural in this language. |
||
415 | */ |
||
416 | public function getMessagesWithoutPlural( $code ) { |
||
430 | |||
431 | /** |
||
432 | * Get the empty messages. |
||
433 | * |
||
434 | * @param string $code The language code. |
||
435 | * |
||
436 | * @return array The empty messages for this language. |
||
437 | */ |
||
438 | public function getEmptyMessages( $code ) { |
||
450 | |||
451 | /** |
||
452 | * Get the messages with trailing whitespace. |
||
453 | * |
||
454 | * @param string $code The language code. |
||
455 | * |
||
456 | * @return array The messages with trailing whitespace in this language. |
||
457 | */ |
||
458 | public function getMessagesWithWhitespace( $code ) { |
||
470 | |||
471 | /** |
||
472 | * Get the non-XHTML messages. |
||
473 | * |
||
474 | * @param string $code The language code. |
||
475 | * |
||
476 | * @return array The non-XHTML messages for this language. |
||
477 | */ |
||
478 | public function getNonXHTMLMessages( $code ) { |
||
499 | |||
500 | /** |
||
501 | * Get the messages which include wrong characters. |
||
502 | * |
||
503 | * @param string $code The language code. |
||
504 | * |
||
505 | * @return array The messages which include wrong characters in this language. |
||
506 | */ |
||
507 | public function getMessagesWithWrongChars( $code ) { |
||
537 | |||
538 | /** |
||
539 | * Get the messages which include dubious links. |
||
540 | * |
||
541 | * @param string $code The language code. |
||
542 | * |
||
543 | * @return array The messages which include dubious links in this language. |
||
544 | */ |
||
545 | public function getMessagesWithDubiousLinks( $code ) { |
||
567 | |||
568 | /** |
||
569 | * Get the messages which include unbalanced brackets. |
||
570 | * |
||
571 | * @param string $code The language code. |
||
572 | * |
||
573 | * @return array The messages which include unbalanced brackets in this language. |
||
574 | */ |
||
575 | public function getMessagesWithUnbalanced( $code ) { |
||
605 | |||
606 | /** |
||
607 | * Get the untranslated namespace names. |
||
608 | * |
||
609 | * @param string $code The language code. |
||
610 | * |
||
611 | * @return array The untranslated namespace names in this language. |
||
612 | */ |
||
613 | public function getUntranslatedNamespaces( $code ) { |
||
623 | |||
624 | /** |
||
625 | * Get the project talk namespace names with no $1. |
||
626 | * |
||
627 | * @param string $code The language code. |
||
628 | * |
||
629 | * @return array The problematic project talk namespaces in this language. |
||
630 | */ |
||
631 | public function getProblematicProjectTalks( $code ) { |
||
652 | |||
653 | /** |
||
654 | * Get the untranslated magic words. |
||
655 | * |
||
656 | * @param string $code The language code. |
||
657 | * |
||
658 | * @return array The untranslated magic words in this language. |
||
659 | */ |
||
660 | View Code Duplication | public function getUntranslatedMagicWords( $code ) { |
|
672 | |||
673 | /** |
||
674 | * Get the obsolete magic words. |
||
675 | * |
||
676 | * @param string $code The language code. |
||
677 | * |
||
678 | * @return array The obsolete magic words in this language. |
||
679 | */ |
||
680 | View Code Duplication | public function getObsoleteMagicWords( $code ) { |
|
692 | |||
693 | /** |
||
694 | * Get the magic words that override the original English magic word. |
||
695 | * |
||
696 | * @param string $code The language code. |
||
697 | * |
||
698 | * @return array The overriding magic words in this language. |
||
699 | */ |
||
700 | public function getOverridingMagicWords( $code ) { |
||
722 | |||
723 | /** |
||
724 | * Get the magic words which do not match the case-sensitivity of the original words. |
||
725 | * |
||
726 | * @param string $code The language code. |
||
727 | * |
||
728 | * @return array The magic words whose case does not match in this language. |
||
729 | */ |
||
730 | public function getCaseMismatchMagicWords( $code ) { |
||
746 | |||
747 | /** |
||
748 | * Get the untranslated special page names. |
||
749 | * |
||
750 | * @param string $code The language code. |
||
751 | * |
||
752 | * @return array The untranslated special page names in this language. |
||
753 | */ |
||
754 | View Code Duplication | public function getUntraslatedSpecialPages( $code ) { |
|
766 | |||
767 | /** |
||
768 | * Get the obsolete special page names. |
||
769 | * |
||
770 | * @param string $code The language code. |
||
771 | * |
||
772 | * @return array The obsolete special page names in this language. |
||
773 | */ |
||
774 | View Code Duplication | public function getObsoleteSpecialPages( $code ) { |
|
786 | } |
||
787 | |||
828 |
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.