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 |
||
8 | class Lang |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | protected static $locale = 'eng'; |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected static $localeDefault = "eng"; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected static $langDir = '/'; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected static $lang = []; |
||
28 | |||
29 | /** |
||
30 | * @param $locale |
||
31 | */ |
||
32 | public static function setLocale($locale) |
||
36 | |||
37 | /** |
||
38 | * @param $dir |
||
39 | * @throws \Exception |
||
40 | */ |
||
41 | public static function setDir($dir) |
||
49 | |||
50 | /** |
||
51 | * @param $name |
||
52 | * @param array $params |
||
53 | * @param string $default |
||
54 | * @return mixed|null|string |
||
55 | */ |
||
56 | public static function get($name, $params = [], $default = "") |
||
72 | |||
73 | /** |
||
74 | * @param $arr |
||
75 | * @param $name |
||
76 | * @param array $params |
||
77 | * @param null $default |
||
78 | * @param bool $getFormDefault |
||
79 | * @return mixed|null |
||
80 | */ |
||
81 | public static function getFromArray($arr, $name, $params = [], $default = null, $getFormDefault = true) |
||
94 | |||
95 | /** |
||
96 | * @param $name |
||
97 | * @param array $params |
||
98 | * @param null $default |
||
99 | * @param bool $getFormDefault |
||
100 | * @return mixed|null |
||
101 | */ |
||
102 | public static function getFromFile($name, $params = [], $default = null, $getFormDefault = true) |
||
110 | |||
111 | /** |
||
112 | * @param $string |
||
113 | * @param array $params |
||
114 | * @return mixed|null |
||
115 | */ |
||
116 | public static function getFromString($string, $params = []) |
||
128 | |||
129 | /** |
||
130 | * @param array|string $name |
||
131 | * @return array |
||
132 | */ |
||
133 | protected static function tryExplodeName($name) |
||
142 | |||
143 | /** |
||
144 | * @param array $arrLang |
||
145 | * @param array $nameArr |
||
146 | * @return array|mixed|null |
||
147 | */ |
||
148 | protected static function searchArrayWithNameArray(array $arrLang, array $nameArr) |
||
157 | |||
158 | /** |
||
159 | * @param array $nameArr |
||
160 | * @param array $params |
||
161 | * @param null $default |
||
162 | * @param bool $getFormDefault |
||
163 | * @return mixed|null |
||
164 | */ |
||
165 | protected static function searchFormArrayLangOrFile( |
||
180 | |||
181 | /** |
||
182 | * @param $filename |
||
183 | * @return array|mixed |
||
184 | */ |
||
185 | protected static function loadFile($filename) |
||
190 | } |
||
191 |
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.