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 |
||
14 | class JetSmsHttpClient implements JetSmsClientInterface |
||
15 | { |
||
16 | /** |
||
17 | * The Http client. |
||
18 | * |
||
19 | * @var Client |
||
20 | */ |
||
21 | private $httpClient; |
||
22 | |||
23 | /** |
||
24 | * The JetSms xml request url. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | private $url; |
||
29 | |||
30 | /** |
||
31 | * The auth username. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $username; |
||
36 | |||
37 | /** |
||
38 | * The auth password. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $password; |
||
43 | |||
44 | /** |
||
45 | * The outbox name. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $outboxName; |
||
50 | |||
51 | /** |
||
52 | * XmlJetSmsClient constructor. |
||
53 | * |
||
54 | * @param Client $client |
||
55 | * @param string $url |
||
56 | * @param string $username |
||
57 | * @param string $password |
||
58 | * @param string $outboxName |
||
59 | */ |
||
60 | View Code Duplication | public function __construct(Client $client, $url, $username, $password, $outboxName) |
|
68 | |||
69 | /** |
||
70 | * Send a short message using the JetSms services. |
||
71 | * |
||
72 | * @param ShortMessage $shortMessage |
||
73 | * |
||
74 | * @return JetSmsResponseInterface |
||
75 | */ |
||
76 | View Code Duplication | public function sendShortMessage(ShortMessage $shortMessage) |
|
88 | |||
89 | /** |
||
90 | * Send multiple short messages using the JetSms services. |
||
91 | * |
||
92 | * @param ShortMessageCollection $shortMessageCollection |
||
93 | * |
||
94 | * @return JetSmsResponseInterface |
||
95 | */ |
||
96 | View Code Duplication | public function sendShortMessages(ShortMessageCollection $shortMessageCollection) |
|
108 | |||
109 | /** |
||
110 | * Get the send date of the contents. |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | private function getSendDate() |
||
120 | |||
121 | /** |
||
122 | * Get the auth credentials array. |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | private function getCredentials() |
||
134 | } |
||
135 |
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.