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 |
||
28 | class InstitutionAuthorizationRepository extends EntityRepository |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * @param Institution $institution |
||
33 | * @param InstitutionRole $role |
||
34 | * @return InstitutionAuthorization[] |
||
35 | */ |
||
36 | View Code Duplication | public function findAuthorizationOptionsForInstitution(Institution $institution, InstitutionRole $role) |
|
46 | |||
47 | /** |
||
48 | * @param Institution $institution |
||
49 | * @param InstitutionRole $role |
||
50 | * @return InstitutionAuthorization|null |
||
51 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
52 | */ |
||
53 | View Code Duplication | public function findInstitutionByAuthorization(Institution $institution, InstitutionRole $role) |
|
63 | |||
64 | /** |
||
65 | * @param Institution $institution |
||
66 | * @param InstitutionOption $institutionOption |
||
67 | * @throws \Doctrine\ORM\OptimisticLockException |
||
68 | */ |
||
69 | public function saveInstitutionOption(Institution $institution, InstitutionOption $institutionOption) |
||
84 | |||
85 | /** |
||
86 | * @param Institution $institution |
||
87 | * @param InstitutionRole $role |
||
88 | * @param InstitutionAuthorization[] $institutionAuthorizations |
||
89 | * @throws \Doctrine\ORM\OptimisticLockException |
||
90 | */ |
||
91 | private function save(Institution $institution, InstitutionRole $role, array $institutionAuthorizations) |
||
100 | |||
101 | /** |
||
102 | * @param EntityManager $entityManager |
||
103 | * @param Institution $institution |
||
104 | * @param InstitutionRole $role |
||
105 | */ |
||
106 | private function clearOldAuthorizations(EntityManager $entityManager, Institution $institution, InstitutionRole $role) |
||
118 | |||
119 | /** |
||
120 | * @param EntityManager $entityManager |
||
121 | * @param InstitutionRole $role |
||
122 | * @param InstitutionAuthorization[] $institutionAuthorizations |
||
123 | */ |
||
124 | private function addNewAuthorizations(EntityManager $entityManager, InstitutionRole $role, array $institutionAuthorizations) |
||
132 | } |
||
133 |
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.