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 |
||
17 | class SimpleMachinesBridge implements BridgeInterface |
||
18 | { |
||
19 | /** |
||
20 | * @param EntityManager $entityManager |
||
21 | * @param int|null $entityId |
||
22 | * @return MemberEntity |
||
23 | */ |
||
24 | View Code Duplication | public function getMemberEntity(EntityManager $entityManager, $entityId = null) |
|
36 | |||
37 | /** |
||
38 | * @param EntityManager $entityManager |
||
39 | * @param int|null $entityId |
||
40 | * @return CategoryEntity |
||
41 | */ |
||
42 | View Code Duplication | public function getCategoryEntity(EntityManager $entityManager, $entityId = null) |
|
54 | |||
55 | /** |
||
56 | * @param EntityManager $entityManager |
||
57 | * @param int|null $entityId |
||
58 | * @return BoardEntity |
||
59 | */ |
||
60 | View Code Duplication | public function getForumEntity(EntityManager $entityManager, $entityId = null) |
|
72 | |||
73 | /** |
||
74 | * @param EntityManager $entityManager |
||
75 | * @param int|null $entityId |
||
76 | * @return TopicEntity |
||
77 | */ |
||
78 | View Code Duplication | public function getTopicEntity(EntityManager $entityManager, $entityId = null) |
|
90 | |||
91 | /** |
||
92 | * @param EntityManager $entityManager |
||
93 | * @param int|null $entityId |
||
94 | * @return MessageEntity |
||
95 | */ |
||
96 | View Code Duplication | public function getPostEntity(EntityManager $entityManager, $entityId = null) |
|
108 | |||
109 | /** |
||
110 | * @param EntityManager $entityManager |
||
111 | * @return BoardEntity[] |
||
112 | */ |
||
113 | public function getForumList(EntityManager $entityManager) |
||
117 | |||
118 | /** |
||
119 | * @param EntityInterface $entity |
||
120 | * @param string $expectedClass |
||
121 | * @throws \InvalidArgumentException |
||
122 | */ |
||
123 | protected function validateEntityClass(EntityInterface $entity, $expectedClass) |
||
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.