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 SectionMatcher extends RepositoryMatcher implements KeyMatcherInterface |
|
|
|
|||
| 10 | { |
||
| 11 | use FlexibleKeyMatcherTrait; |
||
| 12 | |||
| 13 | const MATCH_SECTION_ID = 'section_id'; |
||
| 14 | const MATCH_SECTION_IDENTIFIER = 'section_identifier'; |
||
| 15 | |||
| 16 | protected $allowedConditions = array( |
||
| 17 | self::MATCH_ALL, |
||
| 18 | self::MATCH_SECTION_ID, self::MATCH_SECTION_IDENTIFIER, |
||
| 19 | // aliases |
||
| 20 | 'id', 'identifier' |
||
| 21 | ); |
||
| 22 | protected $returns = 'Role'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
| 26 | * @return SectionCollection |
||
| 27 | */ |
||
| 28 | public function match(array $conditions) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
| 35 | * @return SectionCollection |
||
| 36 | */ |
||
| 37 | public function matchSection(array $conditions) |
||
| 61 | |||
| 62 | protected function getConditionsFromKey($key) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param int[] $sectionIds |
||
| 72 | * @return Section[] |
||
| 73 | */ |
||
| 74 | protected function findSectionsById(array $sectionIds) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string[] $sectionIdentifiers |
||
| 89 | * @return Section[] |
||
| 90 | */ |
||
| 91 | protected function findSectionsByIdentifier(array $sectionIdentifiers) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return Section[] |
||
| 106 | */ |
||
| 107 | protected function findAllSections() |
||
| 118 | } |
||
| 119 |
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.