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 OrderApi extends AbstractApi |
||
13 | { |
||
14 | /** |
||
15 | * @param array $params |
||
16 | * @return Order |
||
17 | */ |
||
18 | public function add(array $params) : Order |
||
47 | |||
48 | /** |
||
49 | * @return array |
||
50 | */ |
||
51 | public function all(): array |
||
57 | |||
58 | /** |
||
59 | * @param int $id |
||
60 | * @return Order |
||
61 | */ |
||
62 | public function show(int $id) : Order |
||
68 | |||
69 | /** |
||
70 | * @param int $id |
||
71 | * @param array $params |
||
72 | * @return string |
||
73 | */ |
||
74 | public function pay(int $id, array $params) : string |
||
89 | |||
90 | /** |
||
91 | * @param int $id |
||
92 | * @param array $params |
||
93 | * @return Order |
||
94 | */ |
||
95 | View Code Duplication | public function annul(int $id, array $params) : Order |
|
110 | |||
111 | /** |
||
112 | * @param int $id |
||
113 | * @param array $params |
||
114 | * @return Order |
||
115 | */ |
||
116 | View Code Duplication | public function cancel(int $id, array $params): Order |
|
131 | |||
132 | /** |
||
133 | * @param int $id |
||
134 | * @param array $params |
||
135 | * @return Order |
||
136 | */ |
||
137 | public function close(int $id, array $params) : Order |
||
152 | |||
153 | /** |
||
154 | * @param int $id |
||
155 | * @param array $params |
||
156 | * @return Order |
||
157 | */ |
||
158 | public function escalate(int $id, array $params) : Order |
||
173 | |||
174 | /** |
||
175 | * @param int $userId |
||
176 | * @param int $orderId |
||
177 | * @param array $params |
||
178 | * @return Order |
||
179 | */ |
||
180 | View Code Duplication | public function bind(int $userId, int $orderId, array $params) : Order |
|
195 | } |
||
196 |
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.