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 |
||
8 | class ShortMessage |
||
9 | { |
||
10 | /** |
||
11 | * The short message body. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $body; |
||
16 | |||
17 | /** |
||
18 | * The receivers. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $receivers; |
||
23 | |||
24 | /** |
||
25 | * ShortMessage constructor. |
||
26 | * |
||
27 | * @param string $body |
||
28 | * @param string|array $receivers |
||
29 | */ |
||
30 | 6 | public function __construct($receivers, $body) |
|
37 | |||
38 | 6 | public function body() |
|
42 | |||
43 | 1 | public function hasManyReceivers() |
|
47 | |||
48 | 2 | public function receivers() |
|
52 | |||
53 | 6 | public function receiversString($glue = null) |
|
57 | |||
58 | 1 | public function toArray() |
|
65 | |||
66 | 1 | View Code Duplication | public function toSingleMessageXml() |
73 | |||
74 | /** |
||
75 | * Get the xml representation of the short message. |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 1 | View Code Duplication | public function toMultipleMessagesXml() |
86 | } |
||
87 |
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.