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 the given disk. |
||
| 80 | * |
||
| 81 | * @param string $key |
||
| 82 | * @param string $disk |
||
| 83 | * |
||
| 84 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 85 | */ |
||
| 86 | public function addMediaFromDisk(string $key, string $disk = null) |
||
| 87 | { |
||
| 88 | return app(FileAdderFactory::class)->createFromDisk($this, $key, $disk ?: config('filesystems.default')); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Add a file from a request. |
||
| 93 | * |
||
| 94 | * @param string $key |
||
| 95 | * |
||
| 96 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 97 | */ |
||
| 98 | public function addMediaFromRequest(string $key) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Add multiple files from a request by keys. |
||
| 105 | * |
||
| 106 | * @param string[] $keys |
||
| 107 | * |
||
| 108 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 109 | */ |
||
| 110 | public function addMultipleMediaFromRequest(array $keys) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Add all files from a request. |
||
| 117 | * |
||
| 118 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 119 | */ |
||
| 120 | public function addAllMediaFromRequest() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Add a remote file to the medialibrary. |
||
| 127 | * |
||
| 128 | * @param string $url |
||
| 129 | * @param string|array ...$allowedMimeTypes |
||
| 130 | * |
||
| 131 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 132 | * |
||
| 133 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 134 | */ |
||
| 135 | public function addMediaFromUrl(string $url, ...$allowedMimeTypes) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Add a base64 encoded file to the medialibrary. |
||
| 167 | * |
||
| 168 | * @param string $base64data |
||
| 169 | * @param string|array ...$allowedMimeTypes |
||
| 170 | * |
||
| 171 | * @throws InvalidBase64Data |
||
| 172 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 173 | * |
||
| 174 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 175 | */ |
||
| 176 | public function addMediaFromBase64(string $base64data, ...$allowedMimeTypes): FileAdder |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Copy a file to the medialibrary. |
||
| 209 | * |
||
| 210 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 211 | * |
||
| 212 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 213 | */ |
||
| 214 | public function copyMedia($file) |
||
| 218 | |||
| 219 | /* |
||
| 220 | * Determine if there is media in the given collection. |
||
| 221 | */ |
||
| 222 | public function hasMedia(string $collectionName = 'default'): bool |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get media collection by its collectionName. |
||
| 229 | * |
||
| 230 | * @param string $collectionName |
||
| 231 | * @param array|callable $filters |
||
| 232 | * |
||
| 233 | * @return \Illuminate\Support\Collection |
||
| 234 | */ |
||
| 235 | public function getMedia(string $collectionName = 'default', $filters = []): Collection |
||
| 239 | |||
| 240 | public function getFirstMedia(string $collectionName = 'default', array $filters = []): ?Media |
||
| 246 | |||
| 247 | /* |
||
| 248 | * Get the url of the image for the given conversionName |
||
| 249 | * for first media for the given collectionName. |
||
| 250 | * If no profile is given, return the source's url. |
||
| 251 | */ |
||
| 252 | public function getFirstMediaUrl(string $collectionName = 'default', string $conversionName = ''): string |
||
| 262 | |||
| 263 | /* |
||
| 264 | * Get the url of the image for the given conversionName |
||
| 265 | * for first media for the given collectionName. |
||
| 266 | * |
||
| 267 | * If no profile is given, return the source's url. |
||
| 268 | */ |
||
| 269 | public function getFirstTemporaryUrl(DateTimeInterface $expiration, string $collectionName = 'default', string $conversionName = ''): string |
||
| 279 | |||
| 280 | public function getMediaCollection(string $collectionName = 'default'): ?MediaCollection |
||
| 289 | |||
| 290 | public function getFallbackMediaUrl(string $collectionName = 'default'): string |
||
| 294 | |||
| 295 | public function getFallbackMediaPath(string $collectionName = 'default'): string |
||
| 299 | |||
| 300 | /* |
||
| 301 | * Get the url of the image for the given conversionName |
||
| 302 | * for first media for the given collectionName. |
||
| 303 | * If no profile is given, return the source's url. |
||
| 304 | */ |
||
| 305 | public function getFirstMediaPath(string $collectionName = 'default', string $conversionName = ''): string |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Update a media collection by deleting and inserting again with new values. |
||
| 318 | * |
||
| 319 | * @param array $newMediaArray |
||
| 320 | * @param string $collectionName |
||
| 321 | * |
||
| 322 | * @return \Illuminate\Support\Collection |
||
| 323 | * |
||
| 324 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated |
||
| 325 | */ |
||
| 326 | public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection |
||
| 359 | |||
| 360 | protected function removeMediaItemsNotPresentInArray(array $newMediaArray, string $collectionName = 'default') |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Remove all media in the given collection. |
||
| 371 | * |
||
| 372 | * @param string $collectionName |
||
| 373 | * |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | public function clearMediaCollection(string $collectionName = 'default'): self |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Remove all media in the given collection except some. |
||
| 392 | * |
||
| 393 | * @param string $collectionName |
||
| 394 | * @param \Spatie\MediaLibrary\Models\Media[]|\Illuminate\Support\Collection $excludedMedia |
||
| 395 | * |
||
| 396 | * @return $this |
||
| 397 | */ |
||
| 398 | public function clearMediaCollectionExcept(string $collectionName = 'default', $excludedMedia = []) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Delete the associated media with the given id. |
||
| 429 | * You may also pass a media object. |
||
| 430 | * |
||
| 431 | * @param int|\Spatie\MediaLibrary\Models\Media $mediaId |
||
| 432 | * |
||
| 433 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeDeleted |
||
| 434 | */ |
||
| 435 | public function deleteMedia($mediaId) |
||
| 449 | |||
| 450 | /* |
||
| 451 | * Add a conversion. |
||
| 452 | */ |
||
| 453 | public function addMediaConversion(string $name): Conversion |
||
| 461 | |||
| 462 | public function addMediaCollection(string $name): MediaCollection |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Delete the model, but preserve all the associated media. |
||
| 473 | * |
||
| 474 | * @return bool |
||
| 475 | */ |
||
| 476 | public function deletePreservingMedia(): bool |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Determines if the media files should be preserved when the media object gets deleted. |
||
| 485 | * |
||
| 486 | * @return bool |
||
| 487 | */ |
||
| 488 | public function shouldDeletePreservingMedia() |
||
| 492 | |||
| 493 | protected function mediaIsPreloaded(): bool |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Cache the media on the object. |
||
| 500 | * |
||
| 501 | * @param string $collectionName |
||
| 502 | * |
||
| 503 | * @return mixed |
||
| 504 | */ |
||
| 505 | public function loadMedia(string $collectionName) |
||
| 522 | |||
| 523 | public function prepareToAttachMedia(Media $media, FileAdder $fileAdder) |
||
| 527 | |||
| 528 | public function processUnattachedMedia(callable $callable) |
||
| 536 | |||
| 537 | protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeTypes) |
||
| 554 | |||
| 555 | public function registerMediaConversions(Media $media = null) |
||
| 558 | |||
| 559 | public function registerMediaCollections() |
||
| 562 | |||
| 563 | public function registerAllMediaConversions(Media $media = null) |
||
| 586 | } |
||
| 587 |
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: