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 bool */ |
||
| 29 | protected $deletePreservingMedia = false; |
||
| 30 | |||
| 31 | /** @var array */ |
||
| 32 | protected $unAttachedMediaLibraryItems = []; |
||
| 33 | |||
| 34 | public static function bootHasMediaTrait() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Set the polymorphic relation. |
||
| 53 | * |
||
| 54 | * @return mixed |
||
| 55 | */ |
||
| 56 | public function media() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Add a file to the medialibrary. |
||
| 63 | * |
||
| 64 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 65 | * |
||
| 66 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 67 | */ |
||
| 68 | public function addMedia($file) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Add a file from a request. |
||
| 75 | * |
||
| 76 | * @param string $key |
||
| 77 | * |
||
| 78 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 79 | */ |
||
| 80 | public function addMediaFromRequest(string $key) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Add multiple files from a request by keys. |
||
| 87 | * |
||
| 88 | * @param string[] $keys |
||
| 89 | * |
||
| 90 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 91 | */ |
||
| 92 | public function addMultipleMediaFromRequest(array $keys) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Add all files from a request. |
||
| 99 | * |
||
| 100 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 101 | */ |
||
| 102 | public function addAllMediaFromRequest() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Add a remote file to the medialibrary. |
||
| 109 | * |
||
| 110 | * @param string $url |
||
| 111 | * @param string|array ...$allowedMimeTypes |
||
| 112 | * |
||
| 113 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 114 | * |
||
| 115 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 116 | */ |
||
| 117 | public function addMediaFromUrl(string $url, ...$allowedMimeTypes) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Add a base64 encoded file to the medialibrary. |
||
| 138 | * |
||
| 139 | * @param string $base64data |
||
| 140 | * @param string|array ...$allowedMimeTypes |
||
| 141 | * |
||
| 142 | * @throws InvalidBase64Data |
||
| 143 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 144 | * |
||
| 145 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 146 | */ |
||
| 147 | public function addMediaFromBase64(string $base64data, ...$allowedMimeTypes) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Copy a file to the medialibrary. |
||
| 181 | * |
||
| 182 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 183 | * |
||
| 184 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 185 | */ |
||
| 186 | public function copyMedia($file) |
||
| 190 | |||
| 191 | /* |
||
| 192 | * Determine if there is media in the given collection. |
||
| 193 | */ |
||
| 194 | public function hasMedia(string $collectionName = 'default'): bool |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get media collection by its collectionName. |
||
| 201 | * |
||
| 202 | * @param string $collectionName |
||
| 203 | * @param array|callable $filters |
||
| 204 | * |
||
| 205 | * @return \Illuminate\Support\Collection |
||
| 206 | */ |
||
| 207 | public function getMedia(string $collectionName = 'default', $filters = []): Collection |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get the first media item of a media collection. |
||
| 214 | * |
||
| 215 | * @param string $collectionName |
||
| 216 | * @param array $filters |
||
| 217 | * |
||
| 218 | * @return Media|null |
||
| 219 | */ |
||
| 220 | public function getFirstMedia(string $collectionName = 'default', array $filters = []) |
||
| 226 | |||
| 227 | /* |
||
| 228 | * Get the url of the image for the given conversionName |
||
| 229 | * for first media for the given collectionName. |
||
| 230 | * If no profile is given, return the source's url. |
||
| 231 | */ |
||
| 232 | public function getFirstMediaUrl(string $collectionName = 'default', string $conversionName = ''): string |
||
| 242 | |||
| 243 | /* |
||
| 244 | * Get the url of the image for the given conversionName |
||
| 245 | * for first media for the given collectionName. |
||
| 246 | * If no profile is given, return the source's url. |
||
| 247 | */ |
||
| 248 | public function getFirstTemporyUrl(DateTimeInterface $expiration, string $collectionName = 'default', string $conversionName = ''): string |
||
| 258 | |||
| 259 | /* |
||
| 260 | * Get the url of the image for the given conversionName |
||
| 261 | * for first media for the given collectionName. |
||
| 262 | * If no profile is given, return the source's url. |
||
| 263 | */ |
||
| 264 | public function getFirstMediaPath(string $collectionName = 'default', string $conversionName = ''): string |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Update a media collection by deleting and inserting again with new values. |
||
| 277 | * |
||
| 278 | * @param array $newMediaArray |
||
| 279 | * @param string $collectionName |
||
| 280 | * |
||
| 281 | * @return \Illuminate\Support\Collection |
||
| 282 | * |
||
| 283 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated |
||
| 284 | */ |
||
| 285 | public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param array $newMediaArray |
||
| 318 | * @param string $collectionName |
||
| 319 | */ |
||
| 320 | protected function removeMediaItemsNotPresentInArray(array $newMediaArray, string $collectionName = 'default') |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Remove all media in the given collection. |
||
| 331 | * |
||
| 332 | * @param string $collectionName |
||
| 333 | * |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | public function clearMediaCollection(string $collectionName = 'default') |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Remove all media in the given collection except some. |
||
| 352 | * |
||
| 353 | * @param string $collectionName |
||
| 354 | * @param \Spatie\MediaLibrary\Media[]|\Illuminate\Support\Collection $excludedMedia |
||
| 355 | * |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | public function clearMediaCollectionExcept(string $collectionName = 'default', $excludedMedia = []) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Delete the associated media with the given id. |
||
| 381 | * You may also pass a media object. |
||
| 382 | * |
||
| 383 | * @param int|\Spatie\MediaLibrary\Media $mediaId |
||
| 384 | * |
||
| 385 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeDeleted |
||
| 386 | */ |
||
| 387 | public function deleteMedia($mediaId) |
||
| 401 | |||
| 402 | /* |
||
| 403 | * Add a conversion. |
||
| 404 | */ |
||
| 405 | public function addMediaConversion(string $name): Conversion |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Delete the model, but preserve all the associated media. |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | public function deletePreservingMedia(): bool |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Determines if the media files should be preserved when the media object gets deleted. |
||
| 428 | * |
||
| 429 | * @return \Spatie\MediaLibrary\Media |
||
| 430 | */ |
||
| 431 | public function shouldDeletePreservingMedia() |
||
| 435 | |||
| 436 | protected function mediaIsPreloaded(): bool |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Cache the media on the object. |
||
| 443 | * |
||
| 444 | * @param string $collectionName |
||
| 445 | * |
||
| 446 | * @return mixed |
||
| 447 | */ |
||
| 448 | public function loadMedia(string $collectionName) |
||
| 465 | |||
| 466 | public function prepareToAttachMedia(Media $media, FileAdder $fileAdder) |
||
| 470 | |||
| 471 | public function processUnattachedMedia(callable $callable) |
||
| 479 | |||
| 480 | protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeTypes) |
||
| 497 | } |
||
| 498 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: