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 Song 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 Song, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Song extends Model |
||
30 | { |
||
31 | use SupportsDeleteWhereIDsNotIn; |
||
32 | |||
33 | protected $guarded = []; |
||
34 | |||
35 | /** |
||
36 | * Attributes to be hidden from JSON outputs. |
||
37 | * Here we specify to hide lyrics as well to save some bandwidth (actually, lots of it). |
||
38 | * Lyrics can then be queried on demand. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $hidden = ['lyrics', 'updated_at', 'path', 'mtime']; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $casts = [ |
||
48 | 'length' => 'float', |
||
49 | 'mtime' => 'int', |
||
50 | 'track' => 'int', |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Indicates if the IDs are auto-incrementing. |
||
55 | * |
||
56 | * @var bool |
||
57 | */ |
||
58 | public $incrementing = false; |
||
59 | |||
60 | public function artist() |
||
64 | |||
65 | public function album() |
||
69 | |||
70 | public function playlists() |
||
74 | |||
75 | /** |
||
76 | * Scrobble the song using Last.fm service. |
||
77 | * |
||
78 | * @param User $user |
||
79 | * @param string $timestamp The UNIX timestamp in which the song started playing. |
||
80 | * |
||
81 | * @return mixed |
||
82 | */ |
||
83 | public function scrobble(User $user, $timestamp) |
||
103 | |||
104 | /** |
||
105 | * Get a Song record using its path. |
||
106 | * |
||
107 | * @param string $path |
||
108 | * |
||
109 | * @return Song|null |
||
110 | */ |
||
111 | public static function byPath($path) |
||
115 | |||
116 | /** |
||
117 | * Update song info. |
||
118 | * |
||
119 | * @param array $ids |
||
120 | * @param array $data The data array, with these supported fields: |
||
121 | * - title |
||
122 | * - artistName |
||
123 | * - albumName |
||
124 | * - lyrics |
||
125 | * All of these are optional, in which case the info will not be changed |
||
126 | * (except for lyrics, which will be emptied). |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | public static function updateInfo($ids, $data) |
||
169 | |||
170 | /** |
||
171 | * Update a single song's info. |
||
172 | * |
||
173 | * @param string $title |
||
174 | * @param string $albumName |
||
175 | * @param string $artistName |
||
176 | * @param string $lyrics |
||
177 | * @param int $track |
||
178 | * @param int $compilationState |
||
179 | * |
||
180 | * @return self |
||
181 | */ |
||
182 | public function updateSingle($title, $albumName, $artistName, $lyrics, $track, $compilationState) |
||
223 | |||
224 | /** |
||
225 | * Scope a query to only include songs in a given directory. |
||
226 | * |
||
227 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
228 | * @param string $path Full path of the directory |
||
229 | * |
||
230 | * @return \Illuminate\Database\Eloquent\Builder |
||
231 | */ |
||
232 | public function scopeInDirectory($query, $path) |
||
239 | |||
240 | /** |
||
241 | * Get all songs favored by a user. |
||
242 | * |
||
243 | * @param User $user |
||
244 | * @param bool $toArray |
||
245 | * |
||
246 | * @return \Illuminate\Database\Eloquent\Collection|array |
||
247 | */ |
||
248 | public static function getFavorites(User $user, $toArray = false) |
||
257 | |||
258 | /** |
||
259 | * Get the song's Object Storage url for streaming or downloading. |
||
260 | * |
||
261 | * @param AwsClient $s3 |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | public function getObjectStoragePublicUrl(AwsClient $s3 = null) |
||
290 | |||
291 | public function getGcpObjectStoragePublicUrl(StorageClient $gcs = null) |
||
308 | |||
309 | |||
310 | |||
311 | /** |
||
312 | * Get the YouTube videos related to this song. |
||
313 | * |
||
314 | * @param string $youTubePageToken The YouTube page token, for pagination purpose. |
||
315 | * |
||
316 | * @return @return object|false |
||
317 | */ |
||
318 | public function getRelatedYouTubeVideos($youTubePageToken = '') |
||
322 | |||
323 | /** |
||
324 | * Sometimes the tags extracted from getID3 are HTML entity encoded. |
||
325 | * This makes sure they are always sane. |
||
326 | * |
||
327 | * @param $value |
||
328 | */ |
||
329 | public function setTitleAttribute($value) |
||
333 | |||
334 | /** |
||
335 | * Some songs don't have a title. |
||
336 | * Fall back to the file name (without extension) for such. |
||
337 | * |
||
338 | * @param $value |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | public function getTitleAttribute($value) |
||
346 | |||
347 | /** |
||
348 | * Prepare the lyrics for displaying. |
||
349 | * |
||
350 | * @param $value |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | public function getLyricsAttribute($value) |
||
361 | |||
362 | /** |
||
363 | * Determine if the song is an AWS S3 Object. |
||
364 | * |
||
365 | * @return bool |
||
366 | */ |
||
367 | public function isS3ObjectAttribute() |
||
371 | |||
372 | /** |
||
373 | * Get the bucket and key name of an S3 object. |
||
374 | * |
||
375 | * @return bool|array |
||
376 | */ |
||
377 | View Code Duplication | public function getS3ParamsAttribute() |
|
387 | |||
388 | public function isGCPObjectAttribute() |
||
392 | |||
393 | /** |
||
394 | * Get the bucket and key name of an GCS object. |
||
395 | * |
||
396 | * @return bool|array |
||
397 | */ |
||
398 | View Code Duplication | public function getGCPParamsAttribute() |
|
408 | } |
||
409 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: