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 MusicBrainz 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 MusicBrainz, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class MusicBrainz |
||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private static $validIncludes = array( |
||
21 | 'artist' => array( |
||
22 | "recordings", |
||
23 | "releases", |
||
24 | "release-groups", |
||
25 | "works", |
||
26 | "various-artists", |
||
27 | "discids", |
||
28 | "media", |
||
29 | "aliases", |
||
30 | "tags", |
||
31 | "user-tags", |
||
32 | "ratings", |
||
33 | "user-ratings", // misc |
||
34 | "artist-rels", |
||
35 | "label-rels", |
||
36 | "recording-rels", |
||
37 | "release-rels", |
||
38 | "release-group-rels", |
||
39 | "url-rels", |
||
40 | "work-rels", |
||
41 | "annotation" |
||
42 | ), |
||
43 | 'annotation' => array(), |
||
44 | 'label' => array( |
||
45 | "releases", |
||
46 | "discids", |
||
47 | "media", |
||
48 | "aliases", |
||
49 | "tags", |
||
50 | "user-tags", |
||
51 | "ratings", |
||
52 | "user-ratings", // misc |
||
53 | "artist-rels", |
||
54 | "label-rels", |
||
55 | "recording-rels", |
||
56 | "release-rels", |
||
57 | "release-group-rels", |
||
58 | "url-rels", |
||
59 | "work-rels", |
||
60 | "annotation" |
||
61 | ), |
||
62 | 'recording' => array( |
||
63 | "artists", |
||
64 | "releases", // sub queries |
||
65 | "discids", |
||
66 | "media", |
||
67 | "artist-credits", |
||
68 | "tags", |
||
69 | "user-tags", |
||
70 | "ratings", |
||
71 | "user-ratings", // misc |
||
72 | "artist-rels", |
||
73 | "label-rels", |
||
74 | "recording-rels", |
||
75 | "release-rels", |
||
76 | "release-group-rels", |
||
77 | "url-rels", |
||
78 | "work-rels", |
||
79 | "annotation", |
||
80 | "aliases" |
||
81 | ), |
||
82 | 'release' => array( |
||
83 | "artists", |
||
84 | "labels", |
||
85 | "recordings", |
||
86 | "release-groups", |
||
87 | "media", |
||
88 | "artist-credits", |
||
89 | "discids", |
||
90 | "puids", |
||
91 | "echoprints", |
||
92 | "isrcs", |
||
93 | "artist-rels", |
||
94 | "label-rels", |
||
95 | "recording-rels", |
||
96 | "release-rels", |
||
97 | "release-group-rels", |
||
98 | "url-rels", |
||
99 | "work-rels", |
||
100 | "recording-level-rels", |
||
101 | "work-level-rels", |
||
102 | "annotation", |
||
103 | "aliases" |
||
104 | ), |
||
105 | 'release-group' => array( |
||
106 | "artists", |
||
107 | "releases", |
||
108 | "discids", |
||
109 | "media", |
||
110 | "artist-credits", |
||
111 | "tags", |
||
112 | "user-tags", |
||
113 | "ratings", |
||
114 | "user-ratings", // misc |
||
115 | "artist-rels", |
||
116 | "label-rels", |
||
117 | "recording-rels", |
||
118 | "release-rels", |
||
119 | "release-group-rels", |
||
120 | "url-rels", |
||
121 | "work-rels", |
||
122 | "annotation", |
||
123 | "aliases" |
||
124 | ), |
||
125 | 'work' => array( |
||
126 | "artists", // sub queries |
||
127 | "aliases", |
||
128 | "tags", |
||
129 | "user-tags", |
||
130 | "ratings", |
||
131 | "user-ratings", // misc |
||
132 | "artist-rels", |
||
133 | "label-rels", |
||
134 | "recording-rels", |
||
135 | "release-rels", |
||
136 | "release-group-rels", |
||
137 | "url-rels", |
||
138 | "work-rels", |
||
139 | "annotation" |
||
140 | ), |
||
141 | 'discid' => array( |
||
142 | "artists", |
||
143 | "labels", |
||
144 | "recordings", |
||
145 | "release-groups", |
||
146 | "media", |
||
147 | "artist-credits", |
||
148 | "discids", |
||
149 | "puids", |
||
150 | "echoprints", |
||
151 | "isrcs", |
||
152 | "artist-rels", |
||
153 | "label-rels", |
||
154 | "recording-rels", |
||
155 | "release-rels", |
||
156 | "release-group-rels", |
||
157 | "url-rels", |
||
158 | "work-rels", |
||
159 | "recording-level-rels", |
||
160 | "work-level-rels" |
||
161 | ), |
||
162 | 'echoprint' => array( |
||
163 | "artists", |
||
164 | "releases" |
||
165 | ), |
||
166 | 'puid' => array( |
||
167 | "artists", |
||
168 | "releases", |
||
169 | "puids", |
||
170 | "echoprints", |
||
171 | "isrcs" |
||
172 | ), |
||
173 | 'isrc' => array( |
||
174 | "artists", |
||
175 | "releases", |
||
176 | "puids", |
||
177 | "echoprints", |
||
178 | "isrcs" |
||
179 | ), |
||
180 | 'iswc' => array( |
||
181 | "artists" |
||
182 | ), |
||
183 | 'collection' => array( |
||
184 | 'releases' |
||
185 | ) |
||
186 | ); |
||
187 | /** |
||
188 | * @var array |
||
189 | */ |
||
190 | private static $validBrowseIncludes = array( |
||
191 | 'release' => array( |
||
192 | "artist-credits", |
||
193 | "labels", |
||
194 | "recordings", |
||
195 | "release-groups", |
||
196 | "media", |
||
197 | "discids", |
||
198 | "artist-rels", |
||
199 | "label-rels", |
||
200 | "recording-rels", |
||
201 | "release-rels", |
||
202 | "release-group-rels", |
||
203 | "url-rels", |
||
204 | "work-rels" |
||
205 | ), |
||
206 | 'recording' => array( |
||
207 | "artist-credits", |
||
208 | "tags", |
||
209 | "ratings", |
||
210 | "user-tags", |
||
211 | "user-ratings" |
||
212 | ), |
||
213 | 'label' => array( |
||
214 | "aliases", |
||
215 | "tags", |
||
216 | "ratings", |
||
217 | "user-tags", |
||
218 | "user-ratings" |
||
219 | ), |
||
220 | 'artist' => array( |
||
221 | "aliases", |
||
222 | "tags", |
||
223 | "ratings", |
||
224 | "user-tags", |
||
225 | "user-ratings" |
||
226 | ), |
||
227 | 'release-group' => array( |
||
228 | "artist-credits", |
||
229 | "tags", |
||
230 | "ratings", |
||
231 | "user-tags", |
||
232 | "user-ratings" |
||
233 | ) |
||
234 | ); |
||
235 | /** |
||
236 | * @var array |
||
237 | */ |
||
238 | private static $validReleaseTypes = array( |
||
239 | "nat", |
||
240 | "album", |
||
241 | "single", |
||
242 | "ep", |
||
243 | "compilation", |
||
244 | "soundtrack", |
||
245 | "spokenword", |
||
246 | "interview", |
||
247 | "audiobook", |
||
248 | "live", |
||
249 | "remix", |
||
250 | "other" |
||
251 | ); |
||
252 | /** |
||
253 | * @var array |
||
254 | */ |
||
255 | private static $validReleaseStatuses = array( |
||
256 | "official", |
||
257 | "promotion", |
||
258 | "bootleg", |
||
259 | "pseudo-release" |
||
260 | ); |
||
261 | /** |
||
262 | * @var string |
||
263 | */ |
||
264 | private $userAgent = 'MusicBrainz PHP Api/0.2.0'; |
||
265 | /** |
||
266 | * The username a MusicBrainz user. Used for authentication. |
||
267 | * |
||
268 | * @var string |
||
269 | */ |
||
270 | private $user = NULL; |
||
271 | /** |
||
272 | * The password of a MusicBrainz user. Used for authentication. |
||
273 | * |
||
274 | * @var string |
||
275 | */ |
||
276 | private $password = NULL; |
||
277 | /** |
||
278 | * The Http adapter used to make requests |
||
279 | * |
||
280 | * @var \MusicBrainz\HttpAdapters\AbstractHttpAdapter |
||
281 | */ |
||
282 | private $adapter; |
||
283 | |||
284 | /** |
||
285 | * Initializes the class. You can pass the user’s username and password |
||
286 | * However, you can modify or add all values later. |
||
287 | * |
||
288 | * @param HttpAdapters\AbstractHttpAdapter $adapter The Http adapter used to make requests |
||
289 | * @param string $user |
||
290 | * @param string $password |
||
291 | */ |
||
292 | public function __construct(AbstractHttpAdapter $adapter, $user = NULL, $password = NULL) |
||
304 | |||
305 | /** |
||
306 | * Do a MusicBrainz lookup |
||
307 | * |
||
308 | * http://musicbrainz.org/doc/XML_Web_Service |
||
309 | * |
||
310 | * @param string $entity |
||
311 | * @param string $mbid Music Brainz ID |
||
312 | * @param array $includes |
||
313 | * |
||
314 | * @throws Exception |
||
315 | * @return array |
||
316 | */ |
||
317 | public function lookup($entity, $mbid, array $includes = array()) |
||
337 | |||
338 | /** |
||
339 | * @param Filters\FilterInterface $filter |
||
340 | * @param $entity |
||
341 | * @param $mbid |
||
342 | * @param array $includes |
||
343 | * @param int $limit |
||
344 | * @param null $offset |
||
345 | * @param array $releaseType |
||
346 | * @param array $releaseStatus |
||
347 | * |
||
348 | * @return array |
||
349 | * @throws Exception |
||
350 | */ |
||
351 | protected function browse( |
||
386 | |||
387 | /** |
||
388 | * @param $entity |
||
389 | * @param $mbid |
||
390 | * @param array $includes |
||
391 | * @param int $limit |
||
392 | * @param null $offset |
||
393 | * |
||
394 | * @return array |
||
395 | * @throws Exception |
||
396 | */ |
||
397 | View Code Duplication | public function browseArtist($entity, $mbid, array $includes = array(), $limit = 25, $offset = NULL) |
|
405 | |||
406 | /** |
||
407 | * @param $entity |
||
408 | * @param $mbid |
||
409 | * @param array $includes |
||
410 | * @param int $limit |
||
411 | * @param null $offset |
||
412 | * |
||
413 | * @return array |
||
414 | * @throws Exception |
||
415 | */ |
||
416 | View Code Duplication | public function browseLabel($entity, $mbid, array $includes, $limit = 25, $offset = NULL) |
|
424 | |||
425 | /** |
||
426 | * @param $entity |
||
427 | * @param $mbid |
||
428 | * @param array $includes |
||
429 | * @param int $limit |
||
430 | * @param null $offset |
||
431 | * |
||
432 | * @return array |
||
433 | * @throws Exception |
||
434 | */ |
||
435 | View Code Duplication | public function browseRecording($entity, $mbid, array $includes = array(), $limit = 25, $offset = NULL) |
|
443 | |||
444 | /** |
||
445 | * @param string $entity |
||
446 | * @param string $mbid |
||
447 | * @param array $includes |
||
448 | * @param int $limit |
||
449 | * @param null $offset |
||
450 | * @param array $releaseType |
||
451 | * @param array $releaseStatus |
||
452 | * |
||
453 | * @return array |
||
454 | * @throws Exception |
||
455 | */ |
||
456 | public function browseRelease( |
||
480 | |||
481 | /** |
||
482 | * @param $entity |
||
483 | * @param $mbid |
||
484 | * @param int $limit |
||
485 | * @param null $offset |
||
486 | * @param array $includes |
||
487 | * @param array $releaseType |
||
488 | * |
||
489 | * @return array |
||
490 | * @throws Exception |
||
491 | */ |
||
492 | View Code Duplication | public function browseReleaseGroup( |
|
514 | |||
515 | /** |
||
516 | * Performs a query based on the parameters supplied in the Filter object. |
||
517 | * Returns an array of possible matches with scores, as returned by the |
||
518 | * musicBrainz web service. |
||
519 | * |
||
520 | * Note that these types of queries only return some information, and not all the |
||
521 | * information available about a particular item is available using this type of query. |
||
522 | * You will need to get the MusicBrainz id (mbid) and perform a lookup with browse |
||
523 | * to return complete information about a release. This method returns an array of |
||
524 | * objects that are possible matches. |
||
525 | * |
||
526 | * @param Filters\FilterInterface $filter |
||
527 | * @param int $limit |
||
528 | * @param null|int $offset |
||
529 | * |
||
530 | * @throws Exception |
||
531 | * @return array |
||
532 | */ |
||
533 | public function search(Filters\FilterInterface $filter, $limit = 25, $offset = NULL) |
||
549 | |||
550 | /** |
||
551 | * @param $mbid |
||
552 | * |
||
553 | * @return int |
||
554 | */ |
||
555 | public function isValidMBID($mbid) |
||
559 | |||
560 | /** |
||
561 | * Check the list of allowed entities |
||
562 | * |
||
563 | * @param string $entity |
||
564 | * |
||
565 | * @return bool |
||
566 | */ |
||
567 | private function isValidEntity($entity) |
||
571 | |||
572 | /** |
||
573 | * Some calls require authentication |
||
574 | * |
||
575 | * @param string $entity |
||
576 | * @param $includes |
||
577 | * |
||
578 | * @return bool |
||
579 | */ |
||
580 | protected function isAuthRequired($entity, $includes) |
||
592 | |||
593 | /** |
||
594 | * @param $includes |
||
595 | * @param $validIncludes |
||
596 | * |
||
597 | * @return bool |
||
598 | * @throws \OutOfBoundsException |
||
599 | */ |
||
600 | public function validateInclude($includes, $validIncludes) |
||
610 | |||
611 | /** |
||
612 | * @param $values |
||
613 | * @param $valid |
||
614 | * |
||
615 | * @return bool |
||
616 | * @throws Exception |
||
617 | */ |
||
618 | public function validateFilter($values, $valid) |
||
628 | |||
629 | /** |
||
630 | * Check that the status or type values are valid. Then, check that |
||
631 | * the filters can be used with the given includes. |
||
632 | * |
||
633 | * @param string $entity |
||
634 | * @param array $includes |
||
635 | * @param array $releaseType |
||
636 | * @param array $releaseStatus |
||
637 | * |
||
638 | * @throws Exception |
||
639 | * @return array |
||
640 | */ |
||
641 | public function getBrowseFilterParams( |
||
676 | |||
677 | /** |
||
678 | * @return array |
||
679 | */ |
||
680 | public function getHttpOptions() |
||
689 | |||
690 | /** |
||
691 | * Returns the user agent. |
||
692 | * |
||
693 | * @return string |
||
694 | */ |
||
695 | public function getUserAgent() |
||
699 | |||
700 | /** |
||
701 | * Set the user agent for POST requests (and GET requests for user tags) |
||
702 | * |
||
703 | * @param string $application The name of the application using this library |
||
704 | * @param string $version The version of the application using this library |
||
705 | * @param string $contactInfo E-mail or website of the application |
||
706 | * |
||
707 | * @throws Exception |
||
708 | */ |
||
709 | public function setUserAgent($application, $version, $contactInfo) |
||
717 | |||
718 | /** |
||
719 | * Returns the MusicBrainz user |
||
720 | * |
||
721 | * @return string |
||
722 | */ |
||
723 | public function getUser() |
||
727 | |||
728 | /** |
||
729 | * Sets the MusicBrainz user |
||
730 | * |
||
731 | * @param string $user |
||
732 | */ |
||
733 | public function setUser($user) |
||
737 | |||
738 | /** |
||
739 | * Returns the user’s password |
||
740 | * |
||
741 | * @return string |
||
742 | */ |
||
743 | public function getPassword() |
||
747 | |||
748 | /** |
||
749 | * Sets the user’s password |
||
750 | * |
||
751 | * @param string $password |
||
752 | */ |
||
753 | public function setPassword($password) |
||
757 | } |
||
758 |