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); |
||
5 | View Code Duplication | class Message implements EmailPart |
|
|
|||
6 | { |
||
7 | /** |
||
8 | * @var string |
||
9 | */ |
||
10 | private $contentType; |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $body; |
||
16 | |||
17 | /** |
||
18 | * @var string[] |
||
19 | */ |
||
20 | private $parameters; |
||
21 | |||
22 | /** |
||
23 | * Constructor. |
||
24 | * |
||
25 | * @param string $contentType |
||
26 | * @param string $body |
||
27 | * @param string[] $parameters |
||
28 | */ |
||
29 | 11 | public function __construct(string $contentType, string $body, array $parameters = []) |
|
35 | |||
36 | /** |
||
37 | * Get content type of the message. |
||
38 | * |
||
39 | * @return string |
||
40 | */ |
||
41 | 5 | public function getContentType(): string |
|
45 | |||
46 | /** |
||
47 | * Get body of the message. |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | 5 | public function getBody(): string |
|
55 | |||
56 | /** |
||
57 | * Get parameters of the message. |
||
58 | * |
||
59 | * @return string[] |
||
60 | */ |
||
61 | 7 | public function getParameters(): array |
|
65 | } |
||
66 |
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.