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:
Complex classes like GetTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GetTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | trait GetTrait |
||
29 | { |
||
30 | abstract protected function uri($uri); |
||
31 | |||
32 | abstract protected function request($method, $uri, $options); |
||
33 | |||
34 | abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap); |
||
35 | |||
36 | /** |
||
37 | * Return an associative array of API keys associated with the Reporting Cloud account |
||
38 | * |
||
39 | * @return array|null |
||
40 | */ |
||
41 | public function getApiKeys() |
||
64 | 1 | ||
65 | 1 | /** |
|
66 | * Check a corpus of text for spelling errors. |
||
67 | 1 | * |
|
68 | * Return an array of misspelled words, if spelling errors are found in the corpus of text. |
||
69 | * |
||
70 | * Return null, if no misspelled words are found in the corpus of text. |
||
71 | * |
||
72 | * @param string $text Corpus of text that should be spell checked |
||
73 | * @param string $language Language of specified text |
||
74 | * |
||
75 | 1 | * @return array|null |
|
76 | */ |
||
77 | 1 | public function proofingCheck($text, $language) |
|
99 | 1 | ||
100 | /** |
||
101 | 1 | * Return an array of available dictionaries on the Reporting Cloud service |
|
102 | 1 | * |
|
103 | 1 | * @return array|null |
|
104 | */ |
||
105 | View Code Duplication | public function getAvailableDictionaries() |
|
117 | 1 | ||
118 | /** |
||
119 | * Return an array of suggestions for a misspelled word. |
||
120 | * |
||
121 | * @param string $word Word that should be spell checked |
||
122 | * @param string $language Language of specified text |
||
123 | * @param int $max Maximum number of suggestions to return |
||
124 | * |
||
125 | * @return array|null |
||
126 | */ |
||
127 | public function getProofingSuggestions($word, $language, $max = 10) |
||
149 | |||
150 | /** |
||
151 | * Return an array of merge blocks and merge fields in a template file in template storage. |
||
152 | * |
||
153 | * @param string $templateName Template name |
||
154 | * |
||
155 | * @throws InvalidArgumentException |
||
156 | * |
||
157 | * @return array|null |
||
158 | */ |
||
159 | public function getTemplateInfo($templateName) |
||
179 | 1 | ||
180 | 1 | /** |
|
181 | * Return an array of binary data. |
||
182 | 1 | * Each record in the array is the binary data of a thumbnail image |
|
183 | * |
||
184 | 1 | * @param string $templateName Template name |
|
185 | 1 | * @param int $zoomFactor Zoom factor |
|
186 | 1 | * @param int $fromPage From page |
|
187 | * @param int $toPage To page |
||
188 | 1 | * @param string $imageFormat Image format |
|
189 | * |
||
190 | * @throws InvalidArgumentException |
||
191 | * |
||
192 | * @return array|null |
||
193 | */ |
||
194 | public function getTemplateThumbnails($templateName, $zoomFactor, $fromPage, $toPage, $imageFormat) |
||
220 | 1 | ||
221 | 1 | /** |
|
222 | 1 | * Return the number of templates in template storage |
|
223 | * |
||
224 | 1 | * @return int |
|
225 | */ |
||
226 | public function getTemplateCount() |
||
230 | |||
231 | /** |
||
232 | * Return an array properties for the templates in template storage |
||
233 | * |
||
234 | * @return array|null |
||
235 | */ |
||
236 | 2 | View Code Duplication | public function getTemplateList() |
256 | 2 | ||
257 | /** |
||
258 | 2 | * Return the number of pages in a template in template storage |
|
259 | * |
||
260 | * @param string $templateName Template name |
||
261 | 1 | * |
|
262 | 1 | * @throws InvalidArgumentException |
|
263 | * |
||
264 | 1 | * @return int |
|
265 | */ |
||
266 | View Code Duplication | public function getTemplatePageCount($templateName) |
|
276 | 1 | ||
277 | /** |
||
278 | 1 | * Return true, if the template exists in template storage |
|
279 | 1 | * |
|
280 | 1 | * @param string $templateName Template name |
|
281 | * |
||
282 | 1 | * @throws InvalidArgumentException |
|
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | View Code Duplication | public function templateExists($templateName) |
|
296 | |||
297 | 1 | /** |
|
298 | * Return an array of available fonts on the Reporting Cloud service |
||
299 | 1 | * |
|
300 | 1 | * @return array|null |
|
301 | 1 | */ |
|
302 | 1 | View Code Duplication | public function getFontList() |
314 | |||
315 | /** |
||
316 | * Return an array properties for the ReportingCloud account |
||
317 | * |
||
318 | * @return null |
||
319 | 2 | * @throws \Zend\Filter\Exception\ExceptionInterface |
|
320 | */ |
||
321 | 2 | View Code Duplication | public function getAccountSettings() |
339 | |||
340 | /** |
||
341 | * Download the binary data of a template from template storage |
||
342 | * |
||
343 | * @param string $templateName Template name |
||
344 | * |
||
345 | * @throws InvalidArgumentException |
||
346 | 12 | * |
|
347 | * @return null|resource |
||
348 | 12 | */ |
|
349 | public function downloadTemplate($templateName) |
||
367 | |||
368 | /** |
||
369 | * Execute a GET request via REST client |
||
370 | * |
||
371 | * @param string $uri URI |
||
372 | * @param array $query Query |
||
373 | * |
||
374 | * @return mixed|null |
||
375 | */ |
||
376 | protected function get($uri, $query = []) |
||
392 | } |
||
393 |
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.