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 |
||
7 | class Builder |
||
8 | { |
||
9 | /** |
||
10 | * Get all allowed languages. |
||
11 | * |
||
12 | * @return array |
||
13 | **/ |
||
14 | public static function allowedLanguages() |
||
15 | { |
||
16 | if (config('localizer.allowed_langs')) { |
||
17 | return self::addNames(array_merge(config('localizer.allowed_langs'), [config('localizer.default_lang')])); |
||
18 | } else { |
||
19 | return self::addNames([config('localizer.default_lang')]); |
||
20 | } |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * Return true if $code is an allowed lang. |
||
25 | * |
||
26 | * @return bool |
||
27 | **/ |
||
28 | public static function isAllowedLanguage($code) |
||
32 | |||
33 | /** |
||
34 | * Add names to an array of language codes as [$code => $language]. |
||
35 | * |
||
36 | * @param array $codes |
||
37 | * |
||
38 | * @return array |
||
39 | **/ |
||
40 | View Code Duplication | public static function addNames($codes) |
|
62 | |||
63 | /** |
||
64 | * Add names to an array of language codes as [$language => $code]. |
||
65 | * |
||
66 | * @param array $langs |
||
67 | * |
||
68 | * @return array |
||
69 | **/ |
||
70 | View Code Duplication | public static function addCodes($langs) |
|
92 | |||
93 | /** |
||
94 | * Returns the url to set up language and return back. |
||
95 | * |
||
96 | * @param string $code |
||
97 | * |
||
98 | * @return string |
||
99 | **/ |
||
100 | public static function setRoute($code) |
||
104 | |||
105 | /** |
||
106 | * Returns the url to set up language and return to url('/'). |
||
107 | * |
||
108 | * @param string $code |
||
109 | * |
||
110 | * @return string |
||
111 | **/ |
||
112 | public static function setRouteHome($code) |
||
116 | |||
117 | /** |
||
118 | * Returns the current language code. |
||
119 | * |
||
120 | * @return string |
||
121 | **/ |
||
122 | public static function getCode($name = 'default') |
||
130 | |||
131 | /** |
||
132 | * Returns the language name. |
||
133 | * |
||
134 | * @return string |
||
135 | **/ |
||
136 | public static function getLanguage($code = 'default') |
||
144 | } |
||
145 |
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.