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 |
||
16 | class LogExceptionMiddleware implements MiddlewareInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var LoggerInterface |
||
20 | 5 | */ |
|
21 | public $log; |
||
22 | 5 | ||
23 | 5 | ||
24 | /** |
||
25 | * @param LoggerInterface $log |
||
26 | */ |
||
27 | public function __construct(LoggerInterface $log) |
||
31 | |||
32 | |||
33 | 5 | /** |
|
34 | * PSR-15 Single Pass |
||
35 | * |
||
36 | * @param ServerRequestInterface $request Server reuest instance |
||
37 | * @param RequestHandlerInterface $handler Request handler |
||
38 | 5 | * @return ResponseInterface |
|
39 | */ |
||
40 | View Code Duplication | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
|
62 | 5 | ||
63 | |||
64 | |||
65 | 5 | /** |
|
66 | 5 | * PSR-7 Double Pass |
|
67 | 5 | * |
|
68 | 5 | * @param RequestInterface $request Request instance |
|
69 | 1 | * @param ResponseInterface $response Response instance |
|
70 | * @param callable $next Middelware callable |
||
71 | 5 | * |
|
72 | 5 | * @return ResponseInterface |
|
73 | 5 | */ |
|
74 | 5 | View Code Duplication | public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) |
96 | |||
97 | |||
98 | /** |
||
99 | * @param \Exception|\Throwable $e |
||
100 | */ |
||
101 | public function handleThrowable( $e ) |
||
122 | |||
123 | } |
||
124 |
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.