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 |
||
12 | View Code Duplication | class UnicornRepository extends EntityRepository |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * @param int $id |
||
16 | * @param int|null $lockMode |
||
17 | * @param int|null $lockVersion |
||
18 | * @return Unicorn |
||
19 | * @throws \Doctrine\ORM\ORMException |
||
20 | */ |
||
21 | public function find($id, $lockMode = null, $lockVersion = null): Unicorn |
||
31 | |||
32 | /** |
||
33 | * @param Unicorn $unicorn |
||
34 | * @return $unicorn |
||
35 | * @throws \Doctrine\ORM\ORMException |
||
36 | * @throws \Doctrine\ORM\OptimisticLockException |
||
37 | */ |
||
38 | public function save(Unicorn $unicorn): Unicorn |
||
47 | |||
48 | /** |
||
49 | * @param Unicorn $unicorn |
||
50 | * @throws \Doctrine\ORM\OptimisticLockException |
||
51 | * @throws \Doctrine\ORM\ORMException |
||
52 | */ |
||
53 | public function delete(Unicorn $unicorn): void |
||
58 | |||
59 | /** |
||
60 | * @return int |
||
61 | * @throws \Doctrine\ORM\NoResultException |
||
62 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
63 | */ |
||
64 | public function getTotalUnicornCount(): int |
||
72 | } |
||
73 |
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.