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 | * If no profile is given, return the source's url. |
||
| 254 | */ |
||
| 255 | public function getFirstTemporaryUrl(DateTimeInterface $expiration, string $collectionName = 'default', string $conversionName = ''): string |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Gets the given media collection. |
||
| 268 | * |
||
| 269 | * @return \Spatie\MediaLibrary\MediaCollection\MediaCollection |
||
| 270 | */ |
||
| 271 | public function getMediaCollection(string $collectionName = 'default'): ?MediaCollection |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Gets the default path to return for the given collection. |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function getDefaultMediaPath(string $collectionName = 'default') |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Gets the default URL to return for the given collection. |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function getDefaultMediaUrl(string $collectionName = 'default') |
||
| 300 | |||
| 301 | /* |
||
| 302 | * Get the url of the image for the given conversionName |
||
| 303 | * for first media for the given collectionName. |
||
| 304 | * If no profile is given, return the source's url. |
||
| 305 | */ |
||
| 306 | public function getFirstMediaPath(string $collectionName = 'default', string $conversionName = ''): string |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Update a media collection by deleting and inserting again with new values. |
||
| 319 | * |
||
| 320 | * @param array $newMediaArray |
||
| 321 | * @param string $collectionName |
||
| 322 | * |
||
| 323 | * @return \Illuminate\Support\Collection |
||
| 324 | * |
||
| 325 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated |
||
| 326 | */ |
||
| 327 | public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection |
||
| 360 | |||
| 361 | protected function removeMediaItemsNotPresentInArray(array $newMediaArray, string $collectionName = 'default') |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Remove all media in the given collection. |
||
| 372 | * |
||
| 373 | * @param string $collectionName |
||
| 374 | * |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function clearMediaCollection(string $collectionName = 'default'): self |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Remove all media in the given collection except some. |
||
| 393 | * |
||
| 394 | * @param string $collectionName |
||
| 395 | * @param \Spatie\MediaLibrary\Models\Media[]|\Illuminate\Support\Collection $excludedMedia |
||
| 396 | * |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | public function clearMediaCollectionExcept(string $collectionName = 'default', $excludedMedia = []) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Delete the associated media with the given id. |
||
| 426 | * You may also pass a media object. |
||
| 427 | * |
||
| 428 | * @param int|\Spatie\MediaLibrary\Models\Media $mediaId |
||
| 429 | * |
||
| 430 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeDeleted |
||
| 431 | */ |
||
| 432 | public function deleteMedia($mediaId) |
||
| 446 | |||
| 447 | /* |
||
| 448 | * Add a conversion. |
||
| 449 | */ |
||
| 450 | public function addMediaConversion(string $name): Conversion |
||
| 458 | |||
| 459 | public function addMediaCollection(string $name): MediaCollection |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Delete the model, but preserve all the associated media. |
||
| 470 | * |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | public function deletePreservingMedia(): bool |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Determines if the media files should be preserved when the media object gets deleted. |
||
| 482 | * |
||
| 483 | * @return bool |
||
| 484 | */ |
||
| 485 | public function shouldDeletePreservingMedia() |
||
| 489 | |||
| 490 | protected function mediaIsPreloaded(): bool |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Cache the media on the object. |
||
| 497 | * |
||
| 498 | * @param string $collectionName |
||
| 499 | * |
||
| 500 | * @return mixed |
||
| 501 | */ |
||
| 502 | public function loadMedia(string $collectionName) |
||
| 519 | |||
| 520 | public function prepareToAttachMedia(Media $media, FileAdder $fileAdder) |
||
| 524 | |||
| 525 | public function processUnattachedMedia(callable $callable) |
||
| 533 | |||
| 534 | protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeTypes) |
||
| 551 | |||
| 552 | public function registerMediaConversions(Media $media = null) |
||
| 555 | |||
| 556 | public function registerMediaCollections() |
||
| 559 | |||
| 560 | public function registerAllMediaConversions(Media $media = null) |
||
| 583 | } |
||
| 584 |
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: