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); |
||
16 | final class DebugMiddleware implements MiddlewareInterface |
||
17 | { |
||
18 | const HR = '---------------------------------------' . PHP_EOL; |
||
19 | |||
20 | /** |
||
21 | * @var WritableStreamInterface |
||
22 | */ |
||
23 | private $stdout; |
||
24 | |||
25 | 1 | public function __construct(LoopInterface $loop, WritableStreamInterface $stdout = null) |
|
33 | |||
34 | /** |
||
35 | * Return the processed $request via a fulfilled promise. |
||
36 | * When implementing cache or other feature that returns a response, do it with a rejected promise. |
||
37 | * If neither is possible, e.g. on some kind of failure, resolve the unaltered request. |
||
38 | * |
||
39 | * @param RequestInterface $request |
||
40 | * @param string $transactionId |
||
41 | * @param array $options |
||
42 | * @return CancellablePromiseInterface |
||
43 | */ |
||
44 | 1 | public function pre(RequestInterface $request, string $transactionId, array $options = []): CancellablePromiseInterface |
|
55 | |||
56 | /** |
||
57 | * Return the processed $response via a promise. |
||
58 | * |
||
59 | * @param ResponseInterface $response |
||
60 | * @param string $transactionId |
||
61 | * @param array $options |
||
62 | * @return CancellablePromiseInterface |
||
63 | */ |
||
64 | 1 | View Code Duplication | public function post(ResponseInterface $response, string $transactionId, array $options = []): CancellablePromiseInterface |
74 | |||
75 | /** |
||
76 | * Deal with possible errors that occurred during request/response events. |
||
77 | * |
||
78 | * @param Throwable $throwable |
||
79 | * @param string $transactionId |
||
80 | * @param array $options |
||
81 | * @return CancellablePromiseInterface |
||
82 | */ |
||
83 | View Code Duplication | public function error(Throwable $throwable, string $transactionId, array $options = []): CancellablePromiseInterface |
|
93 | } |
||
94 |
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.