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 |
||
18 | abstract class MoipResource implements JsonSerializable |
||
19 | { |
||
20 | /** |
||
21 | * Version of API. |
||
22 | * |
||
23 | * @const string |
||
24 | */ |
||
25 | const VERSION = 'v2'; |
||
26 | |||
27 | /** |
||
28 | * @var \Moip\Moip |
||
29 | */ |
||
30 | protected $moip; |
||
31 | |||
32 | /** |
||
33 | * @var \stdClass |
||
34 | */ |
||
35 | protected $data; |
||
36 | |||
37 | /** |
||
38 | * Initialize a new instance. |
||
39 | */ |
||
40 | abstract protected function initialize(); |
||
41 | |||
42 | /** |
||
43 | * Mount information of a determined object. |
||
44 | * |
||
45 | * @param \stdClass $response |
||
46 | * |
||
47 | * @return mixed |
||
48 | */ |
||
49 | abstract protected function populate(stdClass $response); |
||
50 | |||
51 | /** |
||
52 | * Create a new instance. |
||
53 | * |
||
54 | * @param \Moip\Moip $moip |
||
55 | */ |
||
56 | public function __construct(Moip $moip) |
||
62 | |||
63 | /** |
||
64 | * Get a key of an object if it exists. |
||
65 | * |
||
66 | * @param string $key |
||
67 | * @param \stdClass|null $data |
||
68 | * |
||
69 | * @return mixed |
||
70 | */ |
||
71 | protected function getIfSet($key, stdClass $data = null) |
||
81 | |||
82 | /** |
||
83 | * @return \Moip\Helper\Links |
||
84 | */ |
||
85 | public function getLinks() |
||
93 | |||
94 | /** |
||
95 | * @param $key |
||
96 | * @param $fmt |
||
97 | * @param stdClass|null $data |
||
98 | * |
||
99 | * @return bool|\DateTime|null |
||
100 | */ |
||
101 | View Code Duplication | protected function getIfSetDateFmt($key, $fmt, stdClass $data = null) |
|
|
|||
102 | { |
||
103 | $val = $this->getIfSet($key, $data); |
||
104 | if (!empty($val)) { |
||
105 | $dt = \DateTime::createFromFormat($fmt, $val); |
||
106 | |||
107 | return $dt ? $dt : null; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get a key, representing a date (Y-m-d), of an object if it exists. |
||
113 | * |
||
114 | * @param string $key |
||
115 | * @param stdClass|null $data |
||
116 | * |
||
117 | * @return \DateTime|null |
||
118 | */ |
||
119 | protected function getIfSetDate($key, stdClass $data = null) |
||
123 | |||
124 | /** |
||
125 | * Get a key representing a datetime (\Datetime::ATOM), of an object if it exists. |
||
126 | * |
||
127 | * @param string $key |
||
128 | * @param stdClass|null $data |
||
129 | * |
||
130 | * @return \DateTime|null |
||
131 | */ |
||
132 | View Code Duplication | protected function getIfSetDateTime($key, stdClass $data = null) |
|
143 | |||
144 | /** |
||
145 | * Specify data which should be serialized to JSON. |
||
146 | * |
||
147 | * @return \stdClass |
||
148 | */ |
||
149 | public function jsonSerialize() |
||
153 | |||
154 | /** |
||
155 | * Generate URL to request. |
||
156 | * |
||
157 | * @param $action |
||
158 | * @param $id |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | public function generatePath($action, $id = null) |
||
170 | |||
171 | /** |
||
172 | * Generate URL to request a get list. |
||
173 | * |
||
174 | * @param Pagination $pagination |
||
175 | * @param Filters $filters |
||
176 | * @param array $params |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function generateListPath(Pagination $pagination = null, Filters $filters = null, $params = []) |
||
208 | |||
209 | /** |
||
210 | * Execute a http request. If payload == null no body will be sent. Empty body ('{}') is supported by sending a |
||
211 | * empty stdClass. |
||
212 | * |
||
213 | * @param string $path |
||
214 | * @param string $method |
||
215 | * @param mixed|null $payload |
||
216 | * |
||
217 | * @throws Exceptions\ValidationException if the API returns a 4xx http status code. Usually means invalid data was sent. |
||
218 | * @throws Exceptions\UnautorizedException if the API returns a 401 http status code. Check API token and key. |
||
219 | * @throws Exceptions\UnexpectedException if the API returns a 500 http status code or something unexpected happens (ie.: Network error). |
||
220 | * |
||
221 | * @return stdClass |
||
222 | */ |
||
223 | protected function httpRequest($path, $method, $payload = null) |
||
257 | |||
258 | /** |
||
259 | * Find by path. |
||
260 | * |
||
261 | * @param string $path |
||
262 | * |
||
263 | * @return stdClass |
||
264 | */ |
||
265 | public function getByPath($path) |
||
275 | |||
276 | /** |
||
277 | * Find by path with no populate method. |
||
278 | * |
||
279 | * @param string $path |
||
280 | * |
||
281 | * @return stdClass |
||
282 | */ |
||
283 | public function getByPathNoPopulate($path) |
||
287 | |||
288 | /** |
||
289 | * Create a new item in Moip. |
||
290 | * |
||
291 | * @param string $path |
||
292 | * |
||
293 | * @return stdClass |
||
294 | */ |
||
295 | public function createResource($path) |
||
301 | |||
302 | /** |
||
303 | * Delete a new item in Moip. |
||
304 | * |
||
305 | * @param $path |
||
306 | * |
||
307 | * @return mixed |
||
308 | */ |
||
309 | public function deleteByPath($path) |
||
313 | } |
||
314 |
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.