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 |
||
24 | class SinchService |
||
25 | { |
||
26 | /** |
||
27 | * @var Client $guzzleHTTPClient Guzzle HTTP client |
||
28 | */ |
||
29 | private $guzzleHTTPClient; |
||
30 | |||
31 | /** |
||
32 | * @var string $host Host |
||
33 | */ |
||
34 | private $host; |
||
35 | |||
36 | /** |
||
37 | * @var string $key Key |
||
38 | */ |
||
39 | private $key; |
||
40 | |||
41 | /** |
||
42 | * @var string $secret Secret |
||
43 | */ |
||
44 | private $secret; |
||
45 | |||
46 | /** |
||
47 | * @var string $from From |
||
48 | */ |
||
49 | private $from; |
||
50 | |||
51 | /** |
||
52 | * Constructor |
||
53 | * |
||
54 | * @param string $host Host |
||
55 | * @param string $key Key |
||
56 | * @param string $secret Secret |
||
57 | * @param string|null $from From |
||
58 | */ |
||
59 | public function __construct($host, $key, $secret, $from = null) |
||
70 | |||
71 | // region Public API |
||
72 | |||
73 | /** |
||
74 | * Send SMS |
||
75 | * |
||
76 | * @param string $phoneNumber Phone number |
||
77 | * @param string $messageText Message text |
||
78 | * @param string|null $from From |
||
79 | * |
||
80 | * @return int Message ID |
||
81 | * |
||
82 | * @throws \Fresh\SinchBundle\Exception\SinchException |
||
83 | * @throws GuzzleException |
||
84 | */ |
||
85 | public function sendSMS($phoneNumber, $messageText, $from = null) |
||
121 | |||
122 | /** |
||
123 | * Get status of sent SMS |
||
124 | * |
||
125 | * Available SMS statuses: Successful, Pending, Faulted, Unknown |
||
126 | * |
||
127 | * @param int $messageId Message ID |
||
128 | * |
||
129 | * @return string SMS status |
||
130 | * |
||
131 | * @throws GuzzleException |
||
132 | */ |
||
133 | public function getStatusOfSMS($messageId) |
||
144 | |||
145 | // endregion |
||
146 | |||
147 | // region Check status helpers |
||
148 | |||
149 | /** |
||
150 | * Returns true if SMS with some ID was sent successfully, otherwise returns false |
||
151 | * |
||
152 | * @param int $messageId Message ID |
||
153 | * |
||
154 | * @return bool True if SMS was sent successfully, otherwise - false |
||
155 | */ |
||
156 | View Code Duplication | public function smsIsSentSuccessfully($messageId) |
|
167 | |||
168 | /** |
||
169 | * Returns true if SMS with some ID is still pending, otherwise returns false |
||
170 | * |
||
171 | * @param int $messageId Message ID |
||
172 | * |
||
173 | * @return bool True if SMS is still pending, otherwise - false |
||
174 | */ |
||
175 | View Code Duplication | public function smsIsPending($messageId) |
|
186 | |||
187 | /** |
||
188 | * Returns true if SMS with some ID was faulted, otherwise returns false |
||
189 | * |
||
190 | * @param int $messageId Message ID |
||
191 | * |
||
192 | * @return bool True if SMS was faulted, otherwise - false |
||
193 | */ |
||
194 | View Code Duplication | public function smsIsFaulted($messageId) |
|
205 | |||
206 | /** |
||
207 | * Returns true if SMS with some ID in unknown status, otherwise returns false |
||
208 | * |
||
209 | * @param int $messageId Message ID |
||
210 | * |
||
211 | * @return bool True if SMS in unknown status, otherwise - false |
||
212 | */ |
||
213 | View Code Duplication | public function smsInUnknownStatus($messageId) |
|
224 | |||
225 | // endregion |
||
226 | |||
227 | // region Private functions |
||
228 | |||
229 | /** |
||
230 | * Send request to check status of SMS |
||
231 | * |
||
232 | * @param int $messageId Message ID |
||
233 | * |
||
234 | * @return array|null |
||
235 | * |
||
236 | * @throws \Fresh\SinchBundle\Exception\SinchException |
||
237 | */ |
||
238 | private function sendRequestToCheckStatusOfSMS($messageId) |
||
264 | |||
265 | // endregion |
||
266 | } |
||
267 |
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.