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 |
||
| 20 | class LanguageModel extends Model |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Get all language codes. |
||
| 24 | * |
||
| 25 | * @static |
||
| 26 | * |
||
| 27 | * @return string[] |
||
| 28 | */ |
||
| 29 | public static function getCodes() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Find language code. |
||
| 39 | * |
||
| 40 | * @static |
||
| 41 | * |
||
| 42 | * @param string $code |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public static function findCode($code) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get available languages. |
||
| 55 | * |
||
| 56 | * @param bool $prepend Prepend a default value |
||
| 57 | * |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | public function getLanguages($prepend = false) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get javascript language code. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getJsLanguageCode() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get current language. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function getCurrentLanguage() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Load translations for the current language. |
||
| 108 | */ |
||
| 109 | public function loadCurrentLanguage() |
||
| 113 | } |
||
| 114 |
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.