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 |
||
15 | class File extends AbstractBackend |
||
16 | { |
||
17 | protected $variableName = 'lang'; |
||
18 | protected $dictionary; |
||
19 | |||
20 | protected $baseDirectory; |
||
21 | |||
22 | /** |
||
23 | * @return mixed |
||
24 | */ |
||
25 | 1 | public function getBaseDirectory() |
|
29 | |||
30 | /** |
||
31 | * @param mixed $baseDirectory |
||
32 | */ |
||
33 | 1 | public function setBaseDirectory($baseDirectory) |
|
37 | |||
38 | /** |
||
39 | * @param array $languages |
||
40 | */ |
||
41 | 1 | public function addLanguages($languages) |
|
47 | |||
48 | 1 | public function addLanguage($language) |
|
53 | |||
54 | 1 | protected function compileLanguageDirectory($lang) |
|
58 | |||
59 | /** |
||
60 | * Adds a language to the dictionary |
||
61 | * |
||
62 | * @param string $language |
||
63 | * @param string $path Path to file containing translations |
||
64 | * @return $this |
||
65 | */ |
||
66 | 1 | public function addLanguageFromPath($language, $path) |
|
86 | |||
87 | /** |
||
88 | * @param $language |
||
89 | * @param $path |
||
90 | */ |
||
91 | 1 | public function loadDirectory($language, $path) |
|
102 | |||
103 | /** |
||
104 | * @param $language |
||
105 | * @param $path |
||
106 | */ |
||
107 | 1 | protected function loadFile($language, $path) |
|
126 | |||
127 | /** |
||
128 | * @param $language |
||
129 | * @param $messages |
||
130 | */ |
||
131 | 1 | protected function loadMessages($language, $messages) |
|
139 | |||
140 | /** |
||
141 | * Returns dictionary entry for $slug in $language |
||
142 | * @param string $slug |
||
143 | * @param string|bool $language |
||
144 | * @return string|bool |
||
145 | */ |
||
146 | View Code Duplication | protected function doTranslation($slug, $language = false) |
|
154 | } |
||
155 |
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.