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 |
||
| 18 | trait HasMediaTrait |
||
| 19 | { |
||
| 20 | /** @var array */ |
||
| 21 | public $mediaConversions = []; |
||
| 22 | |||
| 23 | /** @var bool */ |
||
| 24 | protected $deletePreservingMedia = false; |
||
| 25 | |||
| 26 | public static function bootHasMediaTrait() |
||
| 27 | { |
||
| 28 | static::deleted(function (HasMedia $entity) { |
||
| 29 | if ($entity->shouldDeletePreservingMedia()) { |
||
| 30 | return; |
||
| 31 | } |
||
| 32 | |||
| 33 | $entity->media()->get()->each->delete(); |
||
| 34 | }); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Set the polymorphic relation. |
||
| 39 | * |
||
| 40 | * @return mixed |
||
| 41 | */ |
||
| 42 | public function media() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Add a file to the medialibrary. |
||
| 49 | * |
||
| 50 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 51 | * |
||
| 52 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 53 | */ |
||
| 54 | public function addMedia($file) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Add a file from a request. |
||
| 61 | * |
||
| 62 | * @param string $key |
||
| 63 | * |
||
| 64 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 65 | */ |
||
| 66 | public function addMediaFromRequest(string $key) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Add multiple files from a request by keys. |
||
| 73 | * |
||
| 74 | * @param string[] $keys |
||
| 75 | * |
||
| 76 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 77 | */ |
||
| 78 | public function addMultipleMediaFromRequest(array $keys) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Add all files from a request. |
||
| 85 | * |
||
| 86 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 87 | */ |
||
| 88 | public function addAllMediaFromRequest() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Add a remote file to the medialibrary. |
||
| 95 | * |
||
| 96 | * @param string $url |
||
| 97 | * |
||
| 98 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 99 | * |
||
| 100 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 101 | */ |
||
| 102 | public function addMediaFromUrl(string $url) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Add a base64 encoded file to the medialibrary. |
||
| 121 | * |
||
| 122 | * @param string $base64data |
||
| 123 | * |
||
| 124 | * @throws InvalidBase64Data |
||
| 125 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 126 | * |
||
| 127 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 128 | */ |
||
| 129 | public function addMediaFromBase64(string $base64data) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Copy a file to the medialibrary. |
||
| 161 | * |
||
| 162 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 163 | * |
||
| 164 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 165 | */ |
||
| 166 | public function copyMedia($file) |
||
| 170 | |||
| 171 | /* |
||
| 172 | * Determine if there is media in the given collection. |
||
| 173 | */ |
||
| 174 | public function hasMedia(string $collectionName = 'default'): bool |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get media collection by its collectionName. |
||
| 181 | * |
||
| 182 | * @param string $collectionName |
||
| 183 | * @param array|callable $filters |
||
| 184 | * |
||
| 185 | * @return \Illuminate\Support\Collection |
||
| 186 | */ |
||
| 187 | public function getMedia(string $collectionName = 'default', $filters = []): Collection |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get the first media item of a media collection. |
||
| 194 | * |
||
| 195 | * @param string $collectionName |
||
| 196 | * @param array $filters |
||
| 197 | * |
||
| 198 | * @return Media|null |
||
| 199 | */ |
||
| 200 | public function getFirstMedia(string $collectionName = 'default', array $filters = []) |
||
| 206 | |||
| 207 | /* |
||
| 208 | * Get the url of the image for the given conversionName |
||
| 209 | * for first media for the given collectionName. |
||
| 210 | * If no profile is given, return the source's url. |
||
| 211 | */ |
||
| 212 | public function getFirstMediaUrl(string $collectionName = 'default', string $conversionName = ''): string |
||
| 222 | |||
| 223 | /* |
||
| 224 | * Get the url of the image for the given conversionName |
||
| 225 | * for first media for the given collectionName. |
||
| 226 | * If no profile is given, return the source's url. |
||
| 227 | */ |
||
| 228 | public function getFirstMediaPath(string $collectionName = 'default', string $conversionName = ''): string |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Update a media collection by deleting and inserting again with new values. |
||
| 241 | * |
||
| 242 | * @param array $newMediaArray |
||
| 243 | * @param string $collectionName |
||
| 244 | * |
||
| 245 | * @return \Illuminate\Support\Collection |
||
| 246 | * |
||
| 247 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated |
||
| 248 | */ |
||
| 249 | public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param array $newMediaArray |
||
| 282 | * @param string $collectionName |
||
| 283 | */ |
||
| 284 | protected function removeMediaItemsNotPresentInArray(array $newMediaArray, string $collectionName = 'default') |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Remove all media in the given collection. |
||
| 295 | * |
||
| 296 | * @param string $collectionName |
||
| 297 | * |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | public function clearMediaCollection(string $collectionName = 'default') |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Remove all media in the given collection except some. |
||
| 316 | * |
||
| 317 | * @param string $collectionName |
||
| 318 | * @param \Spatie\MediaLibrary\Media[]|\Illuminate\Support\Collection $excludedMedia |
||
| 319 | * |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | public function clearMediaCollectionExcept(string $collectionName = 'default', $excludedMedia = []) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Delete the associated media with the given id. |
||
| 345 | * You may also pass a media object. |
||
| 346 | * |
||
| 347 | * @param int|\Spatie\MediaLibrary\Media $mediaId |
||
| 348 | * |
||
| 349 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeDeleted |
||
| 350 | */ |
||
| 351 | public function deleteMedia($mediaId) |
||
| 365 | |||
| 366 | /* |
||
| 367 | * Add a conversion. |
||
| 368 | */ |
||
| 369 | public function addMediaConversion(string $name): Conversion |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Delete the model, but preserve all the associated media. |
||
| 380 | * |
||
| 381 | * @return bool |
||
| 382 | */ |
||
| 383 | public function deletePreservingMedia(): bool |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Determines if the media files should be preserved when the media object gets deleted. |
||
| 392 | * |
||
| 393 | * @return \Spatie\MediaLibrary\Media |
||
| 394 | */ |
||
| 395 | public function shouldDeletePreservingMedia() |
||
| 399 | |||
| 400 | protected function mediaIsPreloaded(): bool |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Cache the media on the object. |
||
| 407 | * |
||
| 408 | * @param string $collectionName |
||
| 409 | * |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | public function loadMedia(string $collectionName) |
||
| 437 | } |
||
| 438 |
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.