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); |
||
12 | final class MiddlewareRunner |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | private $options; |
||
18 | |||
19 | /** |
||
20 | * @var MiddlewareInterface[] |
||
21 | */ |
||
22 | private $middlewares; |
||
23 | |||
24 | /** |
||
25 | * MiddlewareRunner constructor. |
||
26 | * @param array $options |
||
27 | * @param MiddlewareInterface[] $middlewares |
||
28 | */ |
||
29 | 1 | public function __construct(array $options, MiddlewareInterface ...$middlewares) |
|
34 | |||
35 | /** |
||
36 | * Sort the middlewares by priority |
||
37 | * |
||
38 | * @param MiddlewareInterface[] $middlewares |
||
39 | * @return array |
||
40 | */ |
||
41 | 1 | protected function orderMiddlewares(MiddlewareInterface ...$middlewares): array |
|
49 | |||
50 | /** |
||
51 | * @param RequestInterface $request |
||
52 | * @return CancellablePromiseInterface |
||
53 | */ |
||
54 | 1 | public function pre( |
|
68 | |||
69 | /** |
||
70 | * @param ResponseInterface $response |
||
71 | * @return CancellablePromiseInterface |
||
72 | */ |
||
73 | 1 | View Code Duplication | public function post( |
89 | |||
90 | /** |
||
91 | * @param Throwable $throwable |
||
92 | * @return CancellablePromiseInterface |
||
93 | */ |
||
94 | 1 | View Code Duplication | public function error( |
111 | } |
||
112 |
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.