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 Lastfm 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 Lastfm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Lastfm extends RESTfulService |
||
11 | { |
||
12 | /** |
||
13 | * Specify the response format, since Last.fm only returns XML. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $responseFormat = 'xml'; |
||
18 | |||
19 | /** |
||
20 | * Override the key param, since, again, Lastfm wants to be different. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $keyParam = 'api_key'; |
||
25 | |||
26 | /** |
||
27 | * Construct an instance of Lastfm service. |
||
28 | * |
||
29 | * @param string $key Last.fm API key. |
||
30 | * @param string $secret Last.fm API shared secret. |
||
31 | * @param Client $client The Guzzle HTTP client. |
||
32 | */ |
||
33 | public function __construct($key = null, $secret = null, Client $client = null) |
||
42 | |||
43 | /** |
||
44 | * Determine if our application is using Last.fm. |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function used() |
||
52 | |||
53 | /** |
||
54 | * Determine if Last.fm integration is enabled. |
||
55 | * |
||
56 | * @return bool |
||
57 | */ |
||
58 | public function enabled() |
||
62 | |||
63 | /** |
||
64 | * Get information about an artist. |
||
65 | * |
||
66 | * @param $name string Name of the artist |
||
67 | * |
||
68 | * @return array|false |
||
69 | */ |
||
70 | public function getArtistInfo($name) |
||
102 | |||
103 | /** |
||
104 | * Build a Koel-usable array of artist information using the data from Last.fm. |
||
105 | * |
||
106 | * @param array $lastfmArtist |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | private function buildArtistInfo(array $lastfmArtist) |
||
121 | |||
122 | /** |
||
123 | * Get information about an album. |
||
124 | * |
||
125 | * @param string $name Name of the album |
||
126 | * @param string $artistName Name of the artist |
||
127 | * |
||
128 | * @return array|false |
||
129 | */ |
||
130 | public function getAlbumInfo($name, $artistName) |
||
163 | |||
164 | /** |
||
165 | * Build a Koel-usable array of album information using the data from Last.fm. |
||
166 | * |
||
167 | * @param array $lastfmAlbum |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | private function buildAlbumInfo(array $lastfmAlbum) |
||
189 | |||
190 | /** |
||
191 | * Get Last.fm's session key for the authenticated user using a token. |
||
192 | * |
||
193 | * @param string $token The token after successfully connecting to Last.fm |
||
194 | * |
||
195 | * @link http://www.last.fm/api/webauth#4 |
||
196 | * |
||
197 | * @return string The token key |
||
198 | */ |
||
199 | public function getSessionKey($token) |
||
214 | |||
215 | /** |
||
216 | * Scrobble a song. |
||
217 | * |
||
218 | * @param string $artist The artist name |
||
219 | * @param string $track The track name |
||
220 | * @param string|int $timestamp The UNIX timestamp |
||
221 | * @param string $album The album name |
||
222 | * @param string $sk The session key |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | View Code Duplication | public function scrobble($artist, $track, $timestamp, $album, $sk) |
|
244 | |||
245 | /** |
||
246 | * Love or unlove a track on Last.fm. |
||
247 | * |
||
248 | * @param string $track The track name |
||
249 | * @param string $artist The artist's name |
||
250 | * @param string $sk The session key |
||
251 | * @param bool $love Whether to love or unlove. Such cheesy terms... urrgggh |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function toggleLoveTrack($track, $artist, $sk, $love = true) |
||
268 | |||
269 | /** |
||
270 | * Update a track's "now playing" on Last.fm. |
||
271 | * |
||
272 | * @param string $artist Name of the artist |
||
273 | * @param string $track Name of the track |
||
274 | * @param string $album Name of the album |
||
275 | * @param int|float $duration Duration of the track, in seconds |
||
276 | * @param string $sk The session key |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | View Code Duplication | public function updateNowPlaying($artist, $track, $album, $duration, $sk) |
|
297 | |||
298 | /** |
||
299 | * Build the parameters to use for _authenticated_ Last.fm API calls. |
||
300 | * Such calls require: |
||
301 | * - The API key (api_key) |
||
302 | * - The API signature (api_sig). |
||
303 | * |
||
304 | * @link http://www.last.fm/api/webauth#5 |
||
305 | * |
||
306 | * @param array $params The array of parameters. |
||
307 | * @param bool $toString Whether to turn the array into a query string |
||
308 | * |
||
309 | * @return array|string |
||
310 | */ |
||
311 | public function buildAuthCallParams(array $params, $toString = false) |
||
336 | |||
337 | /** |
||
338 | * Correctly format a string returned by Last.fm. |
||
339 | * |
||
340 | * @param string $str |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | protected function formatText($str) |
||
352 | } |
||
353 |
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.