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 |
||
| 9 | View Code Duplication | class LanguageMatcher extends RepositoryMatcher implements KeyMatcherInterface |
|
|
|
|||
| 10 | { |
||
| 11 | use FlexibleKeyMatcherTrait; |
||
| 12 | |||
| 13 | const MATCH_LANGUAGE_ID = 'language_id'; |
||
| 14 | const MATCH_LANGUAGE_CODE = 'language_code'; |
||
| 15 | |||
| 16 | protected $allowedConditions = array( |
||
| 17 | self::MATCH_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
||
| 18 | self::MATCH_LANGUAGE_ID, self::MATCH_LANGUAGE_CODE, |
||
| 19 | // aliases |
||
| 20 | 'id', 'language' |
||
| 21 | ); |
||
| 22 | protected $returns = 'Language'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
| 26 | * @return LanguageCollection |
||
| 27 | */ |
||
| 28 | public function match(array $conditions) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
| 35 | * @return LanguageCollection |
||
| 36 | */ |
||
| 37 | public function matchLanguage(array $conditions) |
||
| 70 | |||
| 71 | protected function getConditionsFromKey($key) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param int[] $languageIds |
||
| 81 | * @return Language[] |
||
| 82 | */ |
||
| 83 | protected function findLanguagesById(array $languageIds) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string[] $languageIdentifiers |
||
| 98 | * @return Language[] |
||
| 99 | */ |
||
| 100 | protected function findLanguagesByCode(array $languageIdentifiers) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return Language[] |
||
| 115 | */ |
||
| 116 | protected function findAllLanguages() |
||
| 127 | } |
||
| 128 |
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.