Complex classes like HasMediaTrait 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 HasMediaTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | trait HasMediaTrait |
||
24 | { |
||
25 | /** @var array */ |
||
26 | public $mediaConversions = []; |
||
27 | |||
28 | /** @var array */ |
||
29 | public $mediaCollections = []; |
||
30 | |||
31 | /** @var bool */ |
||
32 | protected $deletePreservingMedia = false; |
||
33 | |||
34 | /** @var array */ |
||
35 | protected $unAttachedMediaLibraryItems = []; |
||
36 | |||
37 | public static function bootHasMediaTrait() |
||
53 | |||
54 | /** |
||
55 | * Set the polymorphic relation. |
||
56 | * |
||
57 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
58 | */ |
||
59 | public function media() |
||
63 | |||
64 | /** |
||
65 | * Add a file to the medialibrary. |
||
66 | * |
||
67 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
68 | * |
||
69 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
70 | */ |
||
71 | public function addMedia($file) |
||
75 | |||
76 | /** |
||
77 | * Add a file from a request. |
||
78 | * |
||
79 | * @param string $key |
||
80 | * |
||
81 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
82 | */ |
||
83 | public function addMediaFromRequest(string $key) |
||
87 | |||
88 | /** |
||
89 | * Add multiple files from a request by keys. |
||
90 | * |
||
91 | * @param string[] $keys |
||
92 | * |
||
93 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
94 | */ |
||
95 | public function addMultipleMediaFromRequest(array $keys) |
||
99 | |||
100 | /** |
||
101 | * Add all files from a request. |
||
102 | * |
||
103 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
104 | */ |
||
105 | public function addAllMediaFromRequest() |
||
109 | |||
110 | /** |
||
111 | * Add a remote file to the medialibrary. |
||
112 | * |
||
113 | * @param string $url |
||
114 | * @param string|array ...$allowedMimeTypes |
||
115 | * |
||
116 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
117 | * |
||
118 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
119 | */ |
||
120 | public function addMediaFromUrl(string $url, ...$allowedMimeTypes) |
||
149 | |||
150 | /** |
||
151 | * Add a base64 encoded file to the medialibrary. |
||
152 | * |
||
153 | * @param string $base64data |
||
154 | * @param string|array ...$allowedMimeTypes |
||
155 | * |
||
156 | * @throws InvalidBase64Data |
||
157 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
158 | * |
||
159 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
160 | */ |
||
161 | public function addMediaFromBase64(string $base64data, ...$allowedMimeTypes): FileAdder |
||
191 | |||
192 | /** |
||
193 | * Copy a file to the medialibrary. |
||
194 | * |
||
195 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
196 | * |
||
197 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
198 | */ |
||
199 | public function copyMedia($file) |
||
203 | |||
204 | /* |
||
205 | * Determine if there is media in the given collection. |
||
206 | */ |
||
207 | public function hasMedia(string $collectionName = 'default'): bool |
||
211 | |||
212 | /** |
||
213 | * Get media collection by its collectionName. |
||
214 | * |
||
215 | * @param string $collectionName |
||
216 | * @param array|callable $filters |
||
217 | * |
||
218 | * @return \Illuminate\Support\Collection |
||
219 | */ |
||
220 | public function getMedia(string $collectionName = 'default', $filters = []): Collection |
||
224 | |||
225 | public function getFirstMedia(string $collectionName = 'default', array $filters = []): ?Media |
||
231 | |||
232 | /* |
||
233 | * Get the url of the image for the given conversionName |
||
234 | * for first media for the given collectionName. |
||
235 | * If no profile is given, return the source's url. |
||
236 | */ |
||
237 | public function getFirstMediaUrl(string $collectionName = 'default', string $conversionName = ''): string |
||
247 | |||
248 | /* |
||
249 | * Get the url of the image for the given conversionName |
||
250 | * for first media for the given collectionName. |
||
251 | * If no profile is given, return the source's url. |
||
252 | */ |
||
253 | public function getFirstTemporaryUrl(DateTimeInterface $expiration, string $collectionName = 'default', string $conversionName = ''): string |
||
263 | |||
264 | /* |
||
265 | * Get the url of the image for the given conversionName |
||
266 | * for first media for the given collectionName. |
||
267 | * If no profile is given, return the source's url. |
||
268 | */ |
||
269 | public function getFirstMediaPath(string $collectionName = 'default', string $conversionName = ''): string |
||
279 | |||
280 | /** |
||
281 | * Update a media collection by deleting and inserting again with new values. |
||
282 | * |
||
283 | * @param array $newMediaArray |
||
284 | * @param string $collectionName |
||
285 | * |
||
286 | * @return \Illuminate\Support\Collection |
||
287 | * |
||
288 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated |
||
289 | */ |
||
290 | public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection |
||
320 | |||
321 | protected function removeMediaItemsNotPresentInArray(array $newMediaArray, string $collectionName = 'default') |
||
329 | |||
330 | /** |
||
331 | * Remove all media in the given collection. |
||
332 | * |
||
333 | * @param string $collectionName |
||
334 | * |
||
335 | * @return $this |
||
336 | */ |
||
337 | public function clearMediaCollection(string $collectionName = 'default'): self |
||
350 | |||
351 | /** |
||
352 | * Remove all media in the given collection except some. |
||
353 | * |
||
354 | * @param string $collectionName |
||
355 | * @param \Spatie\MediaLibrary\Models\Media[]|\Illuminate\Support\Collection $excludedMedia |
||
356 | * |
||
357 | * @return $this |
||
358 | */ |
||
359 | public function clearMediaCollectionExcept(string $collectionName = 'default', $excludedMedia = []) |
||
383 | |||
384 | /** |
||
385 | * Delete the associated media with the given id. |
||
386 | * You may also pass a media object. |
||
387 | * |
||
388 | * @param int|\Spatie\MediaLibrary\Models\Media $mediaId |
||
389 | * |
||
390 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeDeleted |
||
391 | */ |
||
392 | public function deleteMedia($mediaId) |
||
406 | |||
407 | /* |
||
408 | * Add a conversion. |
||
409 | */ |
||
410 | public function addMediaConversion(string $name): Conversion |
||
418 | |||
419 | public function addMediaCollection(string $name): MediaCollection |
||
427 | |||
428 | /** |
||
429 | * Delete the model, but preserve all the associated media. |
||
430 | * |
||
431 | * @return bool |
||
432 | */ |
||
433 | public function deletePreservingMedia(): bool |
||
439 | |||
440 | /** |
||
441 | * Determines if the media files should be preserved when the media object gets deleted. |
||
442 | * |
||
443 | * @return bool |
||
444 | */ |
||
445 | public function shouldDeletePreservingMedia() |
||
449 | |||
450 | protected function mediaIsPreloaded(): bool |
||
454 | |||
455 | /** |
||
456 | * Cache the media on the object. |
||
457 | * |
||
458 | * @param string $collectionName |
||
459 | * |
||
460 | * @return mixed |
||
461 | */ |
||
462 | public function loadMedia(string $collectionName) |
||
479 | |||
480 | public function prepareToAttachMedia(Media $media, FileAdder $fileAdder) |
||
484 | |||
485 | public function processUnattachedMedia(callable $callable) |
||
493 | |||
494 | protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeTypes) |
||
511 | |||
512 | public function registerMediaConversions(Media $media = null) |
||
515 | |||
516 | public function registerMediaCollections() |
||
519 | |||
520 | public function registerAllMediaConversions(Media $media = null) |
||
543 | } |
||
544 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: