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 | class OrcService |
||
13 | { |
||
14 | /** @var EntityManager $em */ |
||
15 | private $em; |
||
16 | |||
17 | /** |
||
18 | * @param EntityManager $em |
||
19 | */ |
||
20 | public function __construct(EntityManager $em) |
||
24 | |||
25 | /** |
||
26 | * @param array $data |
||
27 | * @return Orc |
||
28 | */ |
||
29 | public function createFromArray(array $data): Orc |
||
35 | |||
36 | /** |
||
37 | * @param Orc $orc |
||
38 | * @param array $data |
||
39 | * @return Orc |
||
40 | */ |
||
41 | public function updateFromArray(Orc $orc, array $data): Orc |
||
60 | |||
61 | /** |
||
62 | * @param Orc $orc |
||
63 | * @return Orc |
||
64 | * @throws \Doctrine\ORM\ORMException |
||
65 | * @throws \Doctrine\ORM\OptimisticLockException |
||
66 | */ |
||
67 | public function saveOrc(Orc $orc): Orc |
||
71 | |||
72 | /** |
||
73 | * @param Orc $orc |
||
74 | * @throws \Doctrine\ORM\ORMException |
||
75 | * @throws \Doctrine\ORM\OptimisticLockException |
||
76 | */ |
||
77 | public function deleteOrc(Orc $orc): void |
||
81 | |||
82 | /** |
||
83 | * @return OrcRepository |
||
84 | */ |
||
85 | public function getRepository(): OrcRepository |
||
92 | } |
||
93 |
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.