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 |
||
15 | class ExceptionDecorator implements MiddlewareInterface |
||
16 | { |
||
17 | |||
18 | use HasLayoutTrait; |
||
19 | |||
20 | /** @var ViewEngine */ |
||
21 | protected $viewEngine; |
||
22 | |||
23 | /** @var string $view */ |
||
24 | protected $view; |
||
25 | |||
26 | /** |
||
27 | * @var array $templates |
||
28 | */ |
||
29 | protected $templates; |
||
30 | |||
31 | /** |
||
32 | * ExceptionDecorator constructor. |
||
33 | * @param ViewEngine $viewEngine |
||
34 | * @param array $templates |
||
35 | */ |
||
36 | public function __construct(ViewEngine $viewEngine, array $templates) |
||
42 | |||
43 | /** |
||
44 | * @param string $view |
||
45 | */ |
||
46 | protected function setView(string $view) |
||
50 | |||
51 | /** |
||
52 | * @param ServerRequestInterface $request |
||
53 | * @param RequestHandlerInterface $handler |
||
54 | * @return ResponseInterface |
||
55 | */ |
||
56 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
84 | |||
85 | |||
86 | |||
87 | /** |
||
88 | * @param ResponseInterface $response |
||
89 | * @param string $body |
||
90 | * @param int $status |
||
91 | * @return \Psr\Http\Message\MessageInterface|Response |
||
92 | */ |
||
93 | View Code Duplication | protected function getResponseWithBodyAndStatus(ResponseInterface $response, string $body, int $status = 200) |
|
101 | } |
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.