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 |
||
| 21 | trait HasMediaTrait |
||
| 22 | { |
||
| 23 | /** @var array */ |
||
| 24 | public $mediaConversions = []; |
||
| 25 | |||
| 26 | /** @var bool */ |
||
| 27 | protected $deletePreservingMedia = false; |
||
| 28 | |||
| 29 | /** @var array */ |
||
| 30 | protected $unAttachedMediaLibraryItems = []; |
||
| 31 | |||
| 32 | public static function bootHasMediaTrait() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Set the polymorphic relation. |
||
| 45 | * |
||
| 46 | * @return mixed |
||
| 47 | */ |
||
| 48 | public function media() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Add a file to the medialibrary. |
||
| 55 | * |
||
| 56 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 57 | * |
||
| 58 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 59 | */ |
||
| 60 | public function addMedia($file) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Add a file from a request. |
||
| 67 | * |
||
| 68 | * @param string $key |
||
| 69 | * |
||
| 70 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 71 | */ |
||
| 72 | public function addMediaFromRequest(string $key) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Add multiple files from a request by keys. |
||
| 79 | * |
||
| 80 | * @param string[] $keys |
||
| 81 | * |
||
| 82 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 83 | */ |
||
| 84 | public function addMultipleMediaFromRequest(array $keys) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Add all files from a request. |
||
| 91 | * |
||
| 92 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 93 | */ |
||
| 94 | public function addAllMediaFromRequest() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Add a remote file to the medialibrary. |
||
| 101 | * |
||
| 102 | * @param string $url |
||
| 103 | * @param array $allowedMimeTypes |
||
| 104 | * |
||
| 105 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 106 | * |
||
| 107 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 108 | */ |
||
| 109 | public function addMediaFromUrl(string $url, array $allowedMimeTypes = []) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Add a base64 encoded file to the medialibrary. |
||
| 130 | * |
||
| 131 | * @param string $base64data |
||
| 132 | * @param array $allowedMimeTypes |
||
| 133 | * |
||
| 134 | * @throws InvalidBase64Data |
||
| 135 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 136 | * |
||
| 137 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 138 | */ |
||
| 139 | public function addMediaFromBase64(string $base64data, array $allowedMimeTypes = []) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Copy a file to the medialibrary. |
||
| 173 | * |
||
| 174 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 175 | * |
||
| 176 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 177 | */ |
||
| 178 | public function copyMedia($file) |
||
| 182 | |||
| 183 | /* |
||
| 184 | * Determine if there is media in the given collection. |
||
| 185 | */ |
||
| 186 | public function hasMedia(string $collectionName = 'default'): bool |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get media collection by its collectionName. |
||
| 193 | * |
||
| 194 | * @param string $collectionName |
||
| 195 | * @param array|callable $filters |
||
| 196 | * |
||
| 197 | * @return \Illuminate\Support\Collection |
||
| 198 | */ |
||
| 199 | public function getMedia(string $collectionName = 'default', $filters = []): Collection |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get the first media item of a media collection. |
||
| 206 | * |
||
| 207 | * @param string $collectionName |
||
| 208 | * @param array $filters |
||
| 209 | * |
||
| 210 | * @return Media|null |
||
| 211 | */ |
||
| 212 | public function getFirstMedia(string $collectionName = 'default', array $filters = []) |
||
| 218 | |||
| 219 | /* |
||
| 220 | * Get the url of the image for the given conversionName |
||
| 221 | * for first media for the given collectionName. |
||
| 222 | * If no profile is given, return the source's url. |
||
| 223 | */ |
||
| 224 | public function getFirstMediaUrl(string $collectionName = 'default', string $conversionName = ''): string |
||
| 234 | |||
| 235 | /* |
||
| 236 | * Get the url of the image for the given conversionName |
||
| 237 | * for first media for the given collectionName. |
||
| 238 | * If no profile is given, return the source's url. |
||
| 239 | */ |
||
| 240 | public function getFirstMediaPath(string $collectionName = 'default', string $conversionName = ''): string |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Update a media collection by deleting and inserting again with new values. |
||
| 253 | * |
||
| 254 | * @param array $newMediaArray |
||
| 255 | * @param string $collectionName |
||
| 256 | * |
||
| 257 | * @return \Illuminate\Support\Collection |
||
| 258 | * |
||
| 259 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated |
||
| 260 | */ |
||
| 261 | public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param array $newMediaArray |
||
| 294 | * @param string $collectionName |
||
| 295 | */ |
||
| 296 | protected function removeMediaItemsNotPresentInArray(array $newMediaArray, string $collectionName = 'default') |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Remove all media in the given collection. |
||
| 307 | * |
||
| 308 | * @param string $collectionName |
||
| 309 | * |
||
| 310 | * @return $this |
||
| 311 | */ |
||
| 312 | public function clearMediaCollection(string $collectionName = 'default') |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Remove all media in the given collection except some. |
||
| 328 | * |
||
| 329 | * @param string $collectionName |
||
| 330 | * @param \Spatie\MediaLibrary\Media[]|\Illuminate\Support\Collection $excludedMedia |
||
| 331 | * |
||
| 332 | * @return $this |
||
| 333 | */ |
||
| 334 | public function clearMediaCollectionExcept(string $collectionName = 'default', $excludedMedia = []) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Delete the associated media with the given id. |
||
| 357 | * You may also pass a media object. |
||
| 358 | * |
||
| 359 | * @param int|\Spatie\MediaLibrary\Media $mediaId |
||
| 360 | * |
||
| 361 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeDeleted |
||
| 362 | */ |
||
| 363 | public function deleteMedia($mediaId) |
||
| 377 | |||
| 378 | /* |
||
| 379 | * Add a conversion. |
||
| 380 | */ |
||
| 381 | public function addMediaConversion(string $name): Conversion |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Delete the model, but preserve all the associated media. |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | public function deletePreservingMedia(): bool |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Determines if the media files should be preserved when the media object gets deleted. |
||
| 404 | * |
||
| 405 | * @return \Spatie\MediaLibrary\Media |
||
| 406 | */ |
||
| 407 | public function shouldDeletePreservingMedia() |
||
| 411 | |||
| 412 | protected function mediaIsPreloaded(): bool |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Cache the media on the object. |
||
| 419 | * |
||
| 420 | * @param string $collectionName |
||
| 421 | * |
||
| 422 | * @return mixed |
||
| 423 | */ |
||
| 424 | public function loadMedia(string $collectionName) |
||
| 441 | |||
| 442 | public function prepareToAttachMedia(Media $media, FileAdder $fileAdder) |
||
| 446 | |||
| 447 | public function processUnattachedMedia(callable $callable) |
||
| 455 | |||
| 456 | protected function guardAgainstInvalidMimeType(string $file, array $allowedMimeTypes) |
||
| 468 | } |
||
| 469 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.