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 |
||
10 | class UserRepository extends EntityRepository |
||
11 | { |
||
12 | /** @var QueryBuilder $qb */ |
||
13 | private $qb; |
||
14 | |||
15 | /** |
||
16 | * @param UserInterface $user |
||
17 | * @return UserInterface |
||
18 | * @throws \Exception |
||
19 | */ |
||
20 | View Code Duplication | public function save(UserInterface $user) |
|
29 | |||
30 | /** |
||
31 | * @param UserInterface $user |
||
32 | * @param bool $deletePerson |
||
33 | * @throws \Exception |
||
34 | */ |
||
35 | View Code Duplication | public function delete(UserInterface $user, $deletePerson = false) |
|
43 | |||
44 | /** |
||
45 | * @param UserCriteria $criteria |
||
46 | * @return UserCriteria |
||
47 | */ |
||
48 | private function checkCriteriaForId(UserCriteria $criteria) |
||
57 | |||
58 | /** |
||
59 | * @param UserCriteria $criteria |
||
60 | * @return UserCriteria |
||
61 | */ |
||
62 | private function checkCriteriaForEmail(UserCriteria $criteria) |
||
71 | |||
72 | private function checkCriteriaForState(UserCriteria $criteria) |
||
79 | |||
80 | private function checkCriteriaForRegistrationDate(UserCriteria $criteria) |
||
87 | |||
88 | private function checkCriteriaForLastLoginDate(UserCriteria $criteria) |
||
95 | |||
96 | /** |
||
97 | * @param UserCriteria $criteria |
||
98 | * @return array |
||
99 | */ |
||
100 | public function findByCriteria(UserCriteria $criteria) |
||
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.