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 |
||
| 25 | trait HasMediaTrait |
||
| 26 | { |
||
| 27 | /** @var Conversion[] */ |
||
| 28 | public $mediaConversions = []; |
||
| 29 | |||
| 30 | /** @var MediaCollection[] */ |
||
| 31 | public $mediaCollections = []; |
||
| 32 | |||
| 33 | /** @var bool */ |
||
| 34 | protected $deletePreservingMedia = false; |
||
| 35 | |||
| 36 | /** @var array */ |
||
| 37 | protected $unAttachedMediaLibraryItems = []; |
||
| 38 | |||
| 39 | public static function bootHasMediaTrait() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Set the polymorphic relation. |
||
| 58 | * |
||
| 59 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
| 60 | */ |
||
| 61 | public function media() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Add a file to the medialibrary. |
||
| 68 | * |
||
| 69 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 70 | * |
||
| 71 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 72 | */ |
||
| 73 | public function addMedia($file) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Add a file from a request. |
||
| 80 | * |
||
| 81 | * @param string $key |
||
| 82 | * |
||
| 83 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 84 | */ |
||
| 85 | public function addMediaFromRequest(string $key) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Add multiple files from a request by keys. |
||
| 92 | * |
||
| 93 | * @param string[] $keys |
||
| 94 | * |
||
| 95 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 96 | */ |
||
| 97 | public function addMultipleMediaFromRequest(array $keys) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Add all files from a request. |
||
| 104 | * |
||
| 105 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 106 | */ |
||
| 107 | public function addAllMediaFromRequest() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Add a remote file to the medialibrary. |
||
| 114 | * |
||
| 115 | * @param string $url |
||
| 116 | * @param string|array ...$allowedMimeTypes |
||
| 117 | * |
||
| 118 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 119 | * |
||
| 120 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 121 | */ |
||
| 122 | public function addMediaFromUrl(string $url, ...$allowedMimeTypes) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Add a base64 encoded file to the medialibrary. |
||
| 154 | * |
||
| 155 | * @param string $base64data |
||
| 156 | * @param string|array ...$allowedMimeTypes |
||
| 157 | * |
||
| 158 | * @throws InvalidBase64Data |
||
| 159 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 160 | * |
||
| 161 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 162 | */ |
||
| 163 | public function addMediaFromBase64(string $base64data, ...$allowedMimeTypes): FileAdder |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Copy a file to the medialibrary. |
||
| 196 | * |
||
| 197 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 198 | * |
||
| 199 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 200 | */ |
||
| 201 | public function copyMedia($file) |
||
| 205 | |||
| 206 | /* |
||
| 207 | * Determine if there is media in the given collection. |
||
| 208 | */ |
||
| 209 | public function hasMedia(string $collectionName = 'default'): bool |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get media collection by its collectionName. |
||
| 216 | * |
||
| 217 | * @param string $collectionName |
||
| 218 | * @param array|callable $filters |
||
| 219 | * |
||
| 220 | * @return \Illuminate\Support\Collection |
||
| 221 | */ |
||
| 222 | public function getMedia(string $collectionName = 'default', $filters = []): Collection |
||
| 226 | |||
| 227 | public function getFirstMedia(string $collectionName = 'default', array $filters = []): ?Media |
||
| 233 | |||
| 234 | /* |
||
| 235 | * Get the url of the image for the given conversionName |
||
| 236 | * for first media for the given collectionName. |
||
| 237 | * If no profile is given, return the source's url. |
||
| 238 | */ |
||
| 239 | public function getFirstMediaUrl(string $collectionName = 'default', string $conversionName = ''): string |
||
| 249 | |||
| 250 | /* |
||
| 251 | * Get the url of the image for the given conversionName |
||
| 252 | * for first media for the given collectionName. |
||
| 253 | * |
||
| 254 | * If no profile is given, return the source's url. |
||
| 255 | */ |
||
| 256 | public function getFirstTemporaryUrl(DateTimeInterface $expiration, string $collectionName = 'default', string $conversionName = ''): string |
||
| 266 | |||
| 267 | public function getMediaCollection(string $collectionName = 'default'): ?MediaCollection |
||
| 276 | |||
| 277 | public function getFallbackMediaUrl(string $collectionName = 'default') |
||
| 281 | |||
| 282 | public function getFallbackMediaPath(string $collectionName = 'default'): string |
||
| 286 | |||
| 287 | /* |
||
| 288 | * Get the url of the image for the given conversionName |
||
| 289 | * for first media for the given collectionName. |
||
| 290 | * If no profile is given, return the source's url. |
||
| 291 | */ |
||
| 292 | public function getFirstMediaPath(string $collectionName = 'default', string $conversionName = ''): string |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Update a media collection by deleting and inserting again with new values. |
||
| 305 | * |
||
| 306 | * @param array $newMediaArray |
||
| 307 | * @param string $collectionName |
||
| 308 | * |
||
| 309 | * @return \Illuminate\Support\Collection |
||
| 310 | * |
||
| 311 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated |
||
| 312 | */ |
||
| 313 | public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection |
||
| 346 | |||
| 347 | protected function removeMediaItemsNotPresentInArray(array $newMediaArray, string $collectionName = 'default') |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Remove all media in the given collection. |
||
| 358 | * |
||
| 359 | * @param string $collectionName |
||
| 360 | * |
||
| 361 | * @return $this |
||
| 362 | */ |
||
| 363 | public function clearMediaCollection(string $collectionName = 'default'): self |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Remove all media in the given collection except some. |
||
| 379 | * |
||
| 380 | * @param string $collectionName |
||
| 381 | * @param \Spatie\MediaLibrary\Models\Media[]|\Illuminate\Support\Collection $excludedMedia |
||
| 382 | * |
||
| 383 | * @return $this |
||
| 384 | */ |
||
| 385 | public function clearMediaCollectionExcept(string $collectionName = 'default', $excludedMedia = []) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Delete the associated media with the given id. |
||
| 412 | * You may also pass a media object. |
||
| 413 | * |
||
| 414 | * @param int|\Spatie\MediaLibrary\Models\Media $mediaId |
||
| 415 | * |
||
| 416 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeDeleted |
||
| 417 | */ |
||
| 418 | public function deleteMedia($mediaId) |
||
| 432 | |||
| 433 | /* |
||
| 434 | * Add a conversion. |
||
| 435 | */ |
||
| 436 | public function addMediaConversion(string $name): Conversion |
||
| 444 | |||
| 445 | public function addMediaCollection(string $name): MediaCollection |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Delete the model, but preserve all the associated media. |
||
| 456 | * |
||
| 457 | * @return bool |
||
| 458 | */ |
||
| 459 | public function deletePreservingMedia(): bool |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Determines if the media files should be preserved when the media object gets deleted. |
||
| 468 | * |
||
| 469 | * @return bool |
||
| 470 | */ |
||
| 471 | public function shouldDeletePreservingMedia() |
||
| 475 | |||
| 476 | protected function mediaIsPreloaded(): bool |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Cache the media on the object. |
||
| 483 | * |
||
| 484 | * @param string $collectionName |
||
| 485 | * |
||
| 486 | * @return mixed |
||
| 487 | */ |
||
| 488 | public function loadMedia(string $collectionName) |
||
| 505 | |||
| 506 | public function prepareToAttachMedia(Media $media, FileAdder $fileAdder) |
||
| 510 | |||
| 511 | public function processUnattachedMedia(callable $callable) |
||
| 519 | |||
| 520 | protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeTypes) |
||
| 537 | |||
| 538 | public function registerMediaConversions(Media $media = null) |
||
| 541 | |||
| 542 | public function registerMediaCollections() |
||
| 545 | |||
| 546 | public function registerAllMediaConversions(Media $media = null) |
||
| 569 | } |
||
| 570 |
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: