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 |
||
5 | class CredentialRepository extends Repository |
||
6 | { |
||
7 | /** |
||
8 | * Find every credential |
||
9 | * |
||
10 | * @return array |
||
11 | */ |
||
12 | View Code Duplication | public function findAll() |
|
27 | |||
28 | /** |
||
29 | * @param integer $id |
||
30 | * |
||
31 | * @return Credential |
||
32 | */ |
||
33 | View Code Duplication | public function find($id) |
|
43 | |||
44 | /** |
||
45 | * Save credential into db |
||
46 | * |
||
47 | * @param Credential $credential |
||
48 | */ |
||
49 | public function save(Credential $credential) |
||
61 | |||
62 | /** |
||
63 | * @param $id |
||
64 | */ |
||
65 | public function delete($id) |
||
69 | |||
70 | /** |
||
71 | * @return array |
||
72 | */ |
||
73 | View Code Duplication | public function findAllAsArray() |
|
88 | |||
89 | /** |
||
90 | * @param $repoHook |
||
91 | * @param $branchHook |
||
92 | * |
||
93 | * @return \Doctrine\DBAL\Driver\Statement|mixed |
||
94 | */ |
||
95 | View Code Duplication | public function findToken($repoHook, $branchHook) |
|
114 | |||
115 | /** |
||
116 | * @param $repoHook |
||
117 | * |
||
118 | * @return \Doctrine\DBAL\Driver\Statement|mixed |
||
119 | */ |
||
120 | public function findNameCredential($repoHook) |
||
131 | |||
132 | /** |
||
133 | * @param $row |
||
134 | * @return Credential |
||
135 | */ |
||
136 | protected function buildDomainObject($row) |
||
145 | } |
||
146 |
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.