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 array */ |
||
| 29 | public $mediaCollections = []; |
||
| 30 | |||
| 31 | /** @var bool */ |
||
| 32 | protected $deletePreservingMedia = false; |
||
| 33 | |||
| 34 | /** @var array */ |
||
| 35 | protected $unAttachedMediaLibraryItems = []; |
||
| 36 | |||
| 37 | public static function bootHasMediaTrait() |
||
| 38 | { |
||
| 39 | static::deleting(function (HasMedia $entity) { |
||
| 40 | if ($entity->shouldDeletePreservingMedia()) { |
||
| 41 | return; |
||
| 42 | } |
||
| 43 | |||
| 44 | if (in_array(SoftDeletes::class, trait_uses_recursive($entity))) { |
||
|
|
|||
| 45 | if (!$entity->forceDeleting) { |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | $entity->media()->get()->each->delete(); |
||
| 51 | }); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Set the polymorphic relation. |
||
| 56 | * |
||
| 57 | * @return mixed |
||
| 58 | */ |
||
| 59 | public function media() |
||
| 60 | { |
||
| 61 | return $this->morphMany(config('medialibrary.media_model'), 'model'); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Add a file to the medialibrary. |
||
| 66 | * |
||
| 67 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 68 | * |
||
| 69 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 70 | */ |
||
| 71 | public function addMedia($file) |
||
| 72 | { |
||
| 73 | return app(FileAdderFactory::class)->create($this, $file); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Add a file from a request. |
||
| 78 | * |
||
| 79 | * @param string $key |
||
| 80 | * |
||
| 81 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 82 | */ |
||
| 83 | public function addMediaFromRequest(string $key) |
||
| 84 | { |
||
| 85 | return app(FileAdderFactory::class)->createFromRequest($this, $key); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Add multiple files from a request by keys. |
||
| 90 | * |
||
| 91 | * @param string[] $keys |
||
| 92 | * |
||
| 93 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 94 | */ |
||
| 95 | public function addMultipleMediaFromRequest(array $keys) |
||
| 96 | { |
||
| 97 | return app(FileAdderFactory::class)->createMultipleFromRequest($this, $keys); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Add all files from a request. |
||
| 102 | * |
||
| 103 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder[] |
||
| 104 | */ |
||
| 105 | public function addAllMediaFromRequest() |
||
| 106 | { |
||
| 107 | return app(FileAdderFactory::class)->createAllFromRequest($this); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Add a remote file to the medialibrary. |
||
| 112 | * |
||
| 113 | * @param string $url |
||
| 114 | * @param string|array ...$allowedMimeTypes |
||
| 115 | * |
||
| 116 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 117 | * |
||
| 118 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 119 | */ |
||
| 120 | public function addMediaFromUrl(string $url, ...$allowedMimeTypes) |
||
| 121 | { |
||
| 122 | if (!$stream = @fopen($url, 'r')) { |
||
| 123 | throw UnreachableUrl::create($url); |
||
| 124 | } |
||
| 125 | |||
| 126 | $tmpFile = tempnam(sys_get_temp_dir(), 'media-library'); |
||
| 127 | file_put_contents($tmpFile, $stream); |
||
| 128 | |||
| 129 | $this->guardAgainstInvalidMimeType($tmpFile, $allowedMimeTypes); |
||
| 130 | |||
| 131 | $filename = basename(parse_url($url, PHP_URL_PATH)); |
||
| 132 | |||
| 133 | return app(FileAdderFactory::class) |
||
| 134 | ->create($this, $tmpFile) |
||
| 135 | ->usingName(pathinfo($filename, PATHINFO_FILENAME)) |
||
| 136 | ->usingFileName($filename); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Add a base64 encoded file to the medialibrary. |
||
| 141 | * |
||
| 142 | * @param string $base64data |
||
| 143 | * @param string|array ...$allowedMimeTypes |
||
| 144 | * |
||
| 145 | * @throws InvalidBase64Data |
||
| 146 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 147 | * |
||
| 148 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 149 | */ |
||
| 150 | public function addMediaFromBase64(string $base64data, ...$allowedMimeTypes): FileAdder |
||
| 151 | { |
||
| 152 | // strip out data uri scheme information (see RFC 2397) |
||
| 153 | if (strpos($base64data, ';base64') !== false) { |
||
| 154 | [$_, $base64data] = explode(';', $base64data); |
||
| 155 | [$_, $base64data] = explode(',', $base64data); |
||
| 156 | } |
||
| 157 | |||
| 158 | // strict mode filters for non-base64 alphabet characters |
||
| 159 | if (base64_decode($base64data, true) === false) { |
||
| 160 | throw InvalidBase64Data::create(); |
||
| 161 | } |
||
| 162 | |||
| 163 | // decoding and then reeconding should not change the data |
||
| 164 | if (base64_encode(base64_decode($base64data)) !== $base64data) { |
||
| 165 | throw InvalidBase64Data::create(); |
||
| 166 | } |
||
| 167 | |||
| 168 | $binaryData = base64_decode($base64data); |
||
| 169 | |||
| 170 | // temporarily store the decoded data on the filesystem to be able to pass it to the fileAdder |
||
| 171 | $tmpFile = tempnam(sys_get_temp_dir(), 'medialibrary'); |
||
| 172 | file_put_contents($tmpFile, $binaryData); |
||
| 173 | |||
| 174 | $this->guardAgainstInvalidMimeType($tmpFile, $allowedMimeTypes); |
||
| 175 | |||
| 176 | $file = app(FileAdderFactory::class)->create($this, $tmpFile); |
||
| 177 | |||
| 178 | return $file; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Copy a file to the medialibrary. |
||
| 183 | * |
||
| 184 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
| 185 | * |
||
| 186 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
| 187 | */ |
||
| 188 | public function copyMedia($file) |
||
| 189 | { |
||
| 190 | return $this->addMedia($file)->preservingOriginal(); |
||
| 191 | } |
||
| 192 | |||
| 193 | /* |
||
| 194 | * Determine if there is media in the given collection. |
||
| 195 | */ |
||
| 196 | public function hasMedia(string $collectionName = 'default'): bool |
||
| 197 | { |
||
| 198 | return count($this->getMedia($collectionName)) ? true : false; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get media collection by its collectionName. |
||
| 203 | * |
||
| 204 | * @param string $collectionName |
||
| 205 | * @param array|callable $filters |
||
| 206 | * |
||
| 207 | * @return \Illuminate\Support\Collection |
||
| 208 | */ |
||
| 209 | public function getMedia(string $collectionName = 'default', $filters = []): Collection |
||
| 210 | { |
||
| 211 | return app(MediaRepository::class)->getCollection($this, $collectionName, $filters); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function getFirstMedia(string $collectionName = 'default', array $filters = []): ?Media |
||
| 220 | |||
| 221 | /* |
||
| 222 | * Get the url of the image for the given conversionName |
||
| 223 | * for first media for the given collectionName. |
||
| 224 | * If no profile is given, return the source's url. |
||
| 225 | */ |
||
| 226 | public function getFirstMediaUrl(string $collectionName = 'default', string $conversionName = ''): string |
||
| 236 | |||
| 237 | /* |
||
| 238 | * Get the url of the image for the given conversionName |
||
| 239 | * for first media for the given collectionName. |
||
| 240 | * If no profile is given, return the source's url. |
||
| 241 | */ |
||
| 242 | public function getFirstTemporaryUrl(DateTimeInterface $expiration, string $collectionName = 'default', string $conversionName = ''): string |
||
| 243 | { |
||
| 244 | $media = $this->getFirstMedia($collectionName); |
||
| 245 | |||
| 246 | if (!$media) { |
||
| 247 | return ''; |
||
| 252 | |||
| 253 | /* |
||
| 254 | * Get the url of the image for the given conversionName |
||
| 255 | * for first media for the given collectionName. |
||
| 256 | * If no profile is given, return the source's url. |
||
| 257 | */ |
||
| 258 | public function getFirstMediaPath(string $collectionName = 'default', string $conversionName = ''): string |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Update a media collection by deleting and inserting again with new values. |
||
| 271 | * |
||
| 272 | * @param array $newMediaArray |
||
| 273 | * @param string $collectionName |
||
| 274 | * |
||
| 275 | * @return \Illuminate\Support\Collection |
||
| 276 | * |
||
| 277 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated |
||
| 278 | */ |
||
| 279 | public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection |
||
| 309 | |||
| 310 | protected function removeMediaItemsNotPresentInArray(array $newMediaArray, string $collectionName = 'default') |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Remove all media in the given collection. |
||
| 321 | * |
||
| 322 | * @param string $collectionName |
||
| 323 | * |
||
| 324 | * @return $this |
||
| 325 | */ |
||
| 326 | public function clearMediaCollection(string $collectionName = 'default'): self |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Remove all media in the given collection except some. |
||
| 342 | * |
||
| 343 | * @param string $collectionName |
||
| 344 | * @param \Spatie\MediaLibrary\Media[]|\Illuminate\Support\Collection $excludedMedia |
||
| 345 | * |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | public function clearMediaCollectionExcept(string $collectionName = 'default', $excludedMedia = []) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Delete the associated media with the given id. |
||
| 375 | * You may also pass a media object. |
||
| 376 | * |
||
| 377 | * @param int|\Spatie\MediaLibrary\Models\Media $mediaId |
||
| 378 | * |
||
| 379 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeDeleted |
||
| 380 | */ |
||
| 381 | public function deleteMedia($mediaId) |
||
| 395 | |||
| 396 | /* |
||
| 397 | * Add a conversion. |
||
| 398 | */ |
||
| 399 | public function addMediaConversion(string $name): Conversion |
||
| 407 | |||
| 408 | public function addMediaCollection(string $name): MediaCollection |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Delete the model, but preserve all the associated media. |
||
| 419 | * |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | public function deletePreservingMedia(): bool |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Determines if the media files should be preserved when the media object gets deleted. |
||
| 431 | * |
||
| 432 | * @return \Spatie\MediaLibrary\Media |
||
| 433 | */ |
||
| 434 | public function shouldDeletePreservingMedia() |
||
| 438 | |||
| 439 | protected function mediaIsPreloaded(): bool |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Cache the media on the object. |
||
| 446 | * |
||
| 447 | * @param string $collectionName |
||
| 448 | * |
||
| 449 | * @return mixed |
||
| 450 | */ |
||
| 451 | public function loadMedia(string $collectionName) |
||
| 468 | |||
| 469 | public function prepareToAttachMedia(Media $media, FileAdder $fileAdder) |
||
| 473 | |||
| 474 | public function processUnattachedMedia(callable $callable) |
||
| 482 | |||
| 483 | protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeTypes) |
||
| 500 | |||
| 501 | public function registerMediaConversions(Media $media = null) |
||
| 504 | |||
| 505 | public function registerMediaCollections() |
||
| 508 | |||
| 509 | public function registerAllMediaConversions(Media $media = null) |
||
| 532 | } |
||
| 533 |
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: