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 declare(strict_types=1); |
||
19 | View Code Duplication | class OrcController |
|
|
|||
20 | { |
||
21 | /** @var int $numPerPage */ |
||
22 | private $numPerPage = 10; |
||
23 | |||
24 | /** @var Paginator $paginator */ |
||
25 | private $paginator; |
||
26 | |||
27 | /** @var OrcService $service */ |
||
28 | private $service; |
||
29 | |||
30 | /** @var ViewEngine $view */ |
||
31 | private $view; |
||
32 | |||
33 | /** |
||
34 | * @param OrcService $service |
||
35 | */ |
||
36 | public function __construct(ViewEngine $view, OrcService $service) |
||
42 | |||
43 | /** |
||
44 | * @param ServerRequestInterface $request |
||
45 | * @param array $args |
||
46 | * @return ResponseInterface $response |
||
47 | * @throws \Exception |
||
48 | */ |
||
49 | public function indexAction(ServerRequestInterface $request, array $args): ResponseInterface |
||
68 | |||
69 | /** |
||
70 | * @param ServerRequestInterface $request |
||
71 | * @return ResponseInterface $response |
||
72 | * @throws \Exception |
||
73 | */ |
||
74 | public function viewAction(ServerRequestInterface $request, array $args): ResponseInterface |
||
85 | |||
86 | /** |
||
87 | * @param ServerRequestInterface $request |
||
88 | * @return ResponseInterface $response |
||
89 | * @throws \Exception |
||
90 | */ |
||
91 | public function createAction(ServerRequestInterface $request, array $args): ResponseInterface |
||
117 | |||
118 | /** |
||
119 | * @param ServerRequestInterface $request |
||
120 | * @return ResponseInterface $response |
||
121 | * @throws \Exception |
||
122 | */ |
||
123 | public function editAction(ServerRequestInterface $request, array $args): ResponseInterface |
||
154 | |||
155 | /** |
||
156 | * @param ServerRequestInterface $request |
||
157 | * @return ResponseInterface $response |
||
158 | * @throws \Exception |
||
159 | */ |
||
160 | public function deleteAction(ServerRequestInterface $request, array $args): ResponseInterface |
||
190 | |||
191 | /** |
||
192 | * @param string $message |
||
193 | * @param string $class |
||
194 | * @return string |
||
195 | */ |
||
196 | private function alertBox(string $message, string $class): string |
||
203 | } |
||
204 |
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.