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 |
||
36 | class ContentApiSdk |
||
37 | { |
||
38 | /** |
||
39 | * Items endpoint |
||
40 | */ |
||
41 | const SUPERDESK_ENDPOINT_ITEMS = '/items'; |
||
42 | |||
43 | /** |
||
44 | * Package endpoint |
||
45 | */ |
||
46 | const SUPERDESK_ENDPOINT_PACKAGES = '/packages'; |
||
47 | |||
48 | /** |
||
49 | * Type indication for packages |
||
50 | */ |
||
51 | const PACKAGE_TYPE_COMPOSITE = 'composite'; |
||
52 | |||
53 | /** |
||
54 | * Supported API version by this SDK version |
||
55 | */ |
||
56 | const API_VERSION = 1; |
||
57 | |||
58 | /** |
||
59 | * Useragent string sent to the API when making requests. |
||
60 | */ |
||
61 | const USERAGENT = 'Content API SDK v1'; |
||
62 | |||
63 | /** |
||
64 | * Any (http) client that implements ClientInterface. |
||
65 | * |
||
66 | * @var ApiClientInterface |
||
67 | */ |
||
68 | protected $client; |
||
69 | |||
70 | /** |
||
71 | * Hostname of the api instance |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $host; |
||
76 | |||
77 | /** |
||
78 | * Port of the api instalce |
||
79 | * |
||
80 | * @var int |
||
81 | */ |
||
82 | protected $port = null; |
||
83 | |||
84 | /** |
||
85 | * Authentication object. |
||
86 | * |
||
87 | * @var AuthenticationInterface |
||
88 | */ |
||
89 | protected $authentication = null; |
||
90 | |||
91 | /** |
||
92 | * Construct method for class. |
||
93 | * |
||
94 | * @param ApiClientInterface $client |
||
95 | * @param string|null $host |
||
96 | * @param int|null $port |
||
97 | */ |
||
98 | public function __construct(ApiClientInterface $client, $host = null, $port = null) |
||
110 | |||
111 | /** |
||
112 | * Gets the value of client. |
||
113 | * |
||
114 | * @return ApiClientInterface |
||
115 | */ |
||
116 | public function getClient() |
||
120 | |||
121 | /** |
||
122 | * Sets the value of client. |
||
123 | * |
||
124 | * @param ApiClientInterface $client Value to set |
||
125 | * |
||
126 | * @return self |
||
127 | */ |
||
128 | public function setClient(ApiClientInterface $client) |
||
134 | |||
135 | /** |
||
136 | * Gets the value of apiHost. |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getHost() |
||
144 | |||
145 | /** |
||
146 | * Sets the value of host. |
||
147 | * |
||
148 | * @param string $host Value to set |
||
149 | * |
||
150 | * @return self |
||
151 | */ |
||
152 | public function setHost($host) |
||
162 | |||
163 | /** |
||
164 | * Gets the value of port. |
||
165 | * |
||
166 | * @return int |
||
167 | */ |
||
168 | public function getPort() |
||
172 | |||
173 | /** |
||
174 | * Sets the value of port. |
||
175 | * |
||
176 | * @param int $port Value to set |
||
177 | * |
||
178 | * @return self |
||
179 | */ |
||
180 | public function setPort($port) |
||
190 | |||
191 | /** |
||
192 | * Get a single item via id. |
||
193 | * |
||
194 | * @param string $itemId Identifier for item |
||
195 | * |
||
196 | * @return Item |
||
197 | */ |
||
198 | public function getItem($itemId) |
||
211 | |||
212 | /** |
||
213 | * Get multiple items based on a filter. |
||
214 | * |
||
215 | * @param array $params Filter parameters |
||
216 | * |
||
217 | * @return ResourceCollection |
||
218 | */ |
||
219 | View Code Duplication | public function getItems($params) |
|
236 | |||
237 | /** |
||
238 | * Get package by identifier. |
||
239 | * |
||
240 | * @param string $packageId Package identifier |
||
241 | * @param bool $resolveAssociations Inject full associations recursively |
||
242 | * instead of references by uri. |
||
243 | * |
||
244 | * @return Package |
||
245 | */ |
||
246 | public function getPackage($packageId, $resolveAssociations = false) |
||
261 | |||
262 | /** |
||
263 | * Get multiple packages based on a filter. |
||
264 | * |
||
265 | * @param array $params Filter parameters |
||
266 | * @param bool $resolveAssociations Inject full associations recursively |
||
267 | * instead of references by uri. |
||
268 | * |
||
269 | * @return ResourceCollection |
||
270 | */ |
||
271 | View Code Duplication | public function getPackages($params, $resolveAssociations = false) |
|
291 | |||
292 | /** |
||
293 | * Gets full objects for all associations for a package. |
||
294 | * |
||
295 | * @param Package $package A package |
||
296 | * |
||
297 | * @return stdClass List of associations |
||
298 | */ |
||
299 | public function getAssociationsFromPackage(Package $package) |
||
331 | |||
332 | /** |
||
333 | * Overwrite the associations links in a packages with the actual association |
||
334 | * data. |
||
335 | * |
||
336 | * @param Package $package Package |
||
337 | * @param stdClass $associations Multiple items or packages |
||
338 | * |
||
339 | * @return Package Package with data injected |
||
340 | */ |
||
341 | public function injectAssociations(Package $package, stdClass $associations) |
||
349 | |||
350 | /** |
||
351 | * Shortcut method to create new class. |
||
352 | * |
||
353 | * @param string $uri Uri of the request |
||
354 | * @param array $parameters Parameters for the request object |
||
355 | * |
||
356 | * @return Request |
||
357 | */ |
||
358 | public function getNewRequest($uri, array $parameters = array()) |
||
366 | |||
367 | /** |
||
368 | * Tries to find a valid id in an uri, both item as package uris. The id |
||
369 | * is returned urldecoded. |
||
370 | * |
||
371 | * @param string $uri Item or package uri |
||
372 | * |
||
373 | * @return string Urldecoded id |
||
374 | */ |
||
375 | public static function getIdFromUri($uri) |
||
391 | |||
392 | /** |
||
393 | * Returns a list of all supported endpoints for the Superdesk Content API. |
||
394 | * |
||
395 | * @return string[] |
||
396 | */ |
||
397 | public static function getAvailableEndpoints() |
||
404 | |||
405 | /** |
||
406 | * Converts json string into StdClass object. Throws an InvalidDataException |
||
407 | * when string could not be converted to object. |
||
408 | * |
||
409 | * @param string $jsonString JSON string |
||
410 | * |
||
411 | * @return object |
||
412 | * @throws Exception|InvalidDataException |
||
413 | */ |
||
414 | public static function getValidJsonObj($jsonString) |
||
423 | |||
424 | /** |
||
425 | * Returns version of api for creating verioned url. |
||
426 | * |
||
427 | * @return string |
||
428 | */ |
||
429 | public static function getVersionURL() |
||
433 | } |
||
434 |
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.