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 |
||
9 | class DeleteRequest extends Request |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private $parameters; |
||
15 | |||
16 | /** |
||
17 | * @var Body |
||
18 | */ |
||
19 | private $body; |
||
20 | |||
21 | /** |
||
22 | * @param Uri $uri |
||
23 | * @param HeaderCollection $headers |
||
24 | * @param array $parameters |
||
25 | * @param Body $body |
||
26 | */ |
||
27 | public function __construct(Uri $uri, HeaderCollection $headers, array $parameters, Body $body) |
||
33 | |||
34 | /** |
||
35 | * @param $name |
||
36 | * @return bool |
||
37 | */ |
||
38 | public function hasParameter($name): bool |
||
42 | |||
43 | /** |
||
44 | * @param $name |
||
45 | * @return mixed |
||
46 | * @throws RequestParameterException |
||
47 | */ |
||
48 | View Code Duplication | public function getParameter($name) |
|
55 | |||
56 | /** |
||
57 | * @return Body |
||
58 | */ |
||
59 | public function getBody(): Body |
||
63 | |||
64 | /** |
||
65 | * @return RequestMethod |
||
66 | */ |
||
67 | public function getMethod(): RequestMethod |
||
71 | } |
||
72 |
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.