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 RoleMatcher extends RepositoryMatcher implements KeyMatcherInterface |
|
|
|
|||
| 10 | { |
||
| 11 | use FlexibleKeyMatcherTrait; |
||
| 12 | |||
| 13 | const MATCH_ROLE_ID = 'role_id'; |
||
| 14 | const MATCH_ROLE_IDENTIFIER = 'role_identifier'; |
||
| 15 | |||
| 16 | protected $allowedConditions = array( |
||
| 17 | self::MATCH_ALL, |
||
| 18 | self::MATCH_ROLE_ID, self::MATCH_ROLE_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 RoleCollection |
||
| 27 | */ |
||
| 28 | public function match(array $conditions) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
| 35 | * @return RoleCollection |
||
| 36 | */ |
||
| 37 | public function matchRole(array $conditions) |
||
| 61 | |||
| 62 | protected function getConditionsFromKey($key) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param int[] $roleIds |
||
| 72 | * @return Role[] |
||
| 73 | */ |
||
| 74 | protected function findRolesById(array $roleIds) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string[] $roleIdentifiers |
||
| 89 | * @return Role[] |
||
| 90 | */ |
||
| 91 | protected function findRolesByIdentifier(array $roleIdentifiers) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return Role[] |
||
| 106 | */ |
||
| 107 | protected function findAllRoles() |
||
| 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.