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); |
||
18 | class MessageValidator |
||
19 | { |
||
20 | /** |
||
21 | * @var Description |
||
22 | */ |
||
23 | private $description; |
||
24 | |||
25 | /** |
||
26 | * @var RequestParameterAssembler |
||
27 | */ |
||
28 | private $parameterAssembler; |
||
29 | |||
30 | /** |
||
31 | * @var SchemaValidator |
||
32 | */ |
||
33 | private $validator; |
||
34 | |||
35 | /** |
||
36 | * MessageValidator constructor. |
||
37 | * |
||
38 | * @param Description $description |
||
39 | * @param RequestParameterAssembler $parameterAssembler |
||
40 | * @param SchemaValidator $validator |
||
41 | */ |
||
42 | public function __construct( |
||
51 | |||
52 | /** |
||
53 | * @param ServerRequestInterface $request |
||
54 | * @param string $path |
||
55 | * |
||
56 | * @return ValidationResult |
||
57 | */ |
||
58 | View Code Duplication | public function validateRequest(ServerRequestInterface $request, string $path): ValidationResult |
|
66 | |||
67 | /** |
||
68 | * @param array|object $body |
||
69 | * @param ServerRequestInterface $request |
||
70 | * @param ResponseInterface $response |
||
71 | * @param string $path |
||
72 | * |
||
73 | * @return ValidationResult |
||
74 | */ |
||
75 | View Code Duplication | public function validateResponse( |
|
89 | } |
||
90 |
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.