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 |
||
11 | View Code Duplication | class HttpResponseHeader implements HttpHeaderInterface |
|
|
|||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $protocol; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $code; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $message; |
||
27 | |||
28 | /** |
||
29 | * HttpResponseHeader constructor. |
||
30 | * @param string $protocol |
||
31 | * @param string $code |
||
32 | 2 | * @param string $message |
|
33 | */ |
||
34 | 2 | public function __construct(string $protocol, string $code, string $message) |
|
40 | |||
41 | /** |
||
42 | 2 | * @return string |
|
43 | */ |
||
44 | 2 | public function getProtocol(): string |
|
48 | |||
49 | /** |
||
50 | * @param string $protocol |
||
51 | 1 | * @return HttpResponseHeader |
|
52 | */ |
||
53 | 1 | public function setProtocol(string $protocol): HttpResponseHeader |
|
58 | |||
59 | /** |
||
60 | 2 | * @return string |
|
61 | */ |
||
62 | 2 | public function getCode(): string |
|
66 | |||
67 | /** |
||
68 | * @param string $code |
||
69 | 1 | * @return HttpResponseHeader |
|
70 | */ |
||
71 | 1 | public function setCode(string $code): HttpResponseHeader |
|
76 | |||
77 | /** |
||
78 | 2 | * @return string |
|
79 | */ |
||
80 | 2 | public function getMessage(): string |
|
84 | |||
85 | /** |
||
86 | * @param string $message |
||
87 | 1 | * @return HttpResponseHeader |
|
88 | */ |
||
89 | 1 | public function setMessage(string $message): HttpResponseHeader |
|
94 | } |
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.