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 | if ($filename == '') { |
||
133 | $mediaExt = explode('/', mime_content_type($tmpFile)); |
||
134 | $filename = 'file.'.$mediaExt[1]; |
||
135 | } |
||
136 | |||
137 | return app(FileAdderFactory::class) |
||
138 | ->create($this, $tmpFile) |
||
139 | ->usingName(pathinfo($filename, PATHINFO_FILENAME)) |
||
140 | ->usingFileName($filename); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Add a base64 encoded file to the medialibrary. |
||
145 | * |
||
146 | * @param string $base64data |
||
147 | * @param string|array ...$allowedMimeTypes |
||
148 | * |
||
149 | * @throws InvalidBase64Data |
||
150 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
151 | * |
||
152 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
153 | */ |
||
154 | public function addMediaFromBase64(string $base64data, ...$allowedMimeTypes): FileAdder |
||
155 | { |
||
156 | // strip out data uri scheme information (see RFC 2397) |
||
157 | if (strpos($base64data, ';base64') !== false) { |
||
158 | [$_, $base64data] = explode(';', $base64data); |
||
159 | [$_, $base64data] = explode(',', $base64data); |
||
160 | } |
||
161 | |||
162 | // strict mode filters for non-base64 alphabet characters |
||
163 | if (base64_decode($base64data, true) === false) { |
||
164 | throw InvalidBase64Data::create(); |
||
165 | } |
||
166 | |||
167 | // decoding and then reeconding should not change the data |
||
168 | if (base64_encode(base64_decode($base64data)) !== $base64data) { |
||
169 | throw InvalidBase64Data::create(); |
||
170 | } |
||
171 | |||
172 | $binaryData = base64_decode($base64data); |
||
173 | |||
174 | // temporarily store the decoded data on the filesystem to be able to pass it to the fileAdder |
||
175 | $tmpFile = tempnam(sys_get_temp_dir(), 'medialibrary'); |
||
176 | file_put_contents($tmpFile, $binaryData); |
||
177 | |||
178 | $this->guardAgainstInvalidMimeType($tmpFile, $allowedMimeTypes); |
||
179 | |||
180 | $file = app(FileAdderFactory::class)->create($this, $tmpFile); |
||
181 | |||
182 | return $file; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Copy a file to the medialibrary. |
||
187 | * |
||
188 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
189 | * |
||
190 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
191 | */ |
||
192 | public function copyMedia($file) |
||
193 | { |
||
194 | return $this->addMedia($file)->preservingOriginal(); |
||
195 | } |
||
196 | |||
197 | /* |
||
198 | * Determine if there is media in the given collection. |
||
199 | */ |
||
200 | public function hasMedia(string $collectionName = 'default'): bool |
||
201 | { |
||
202 | return count($this->getMedia($collectionName)) ? true : false; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Get media collection by its collectionName. |
||
207 | * |
||
208 | * @param string $collectionName |
||
209 | * @param array|callable $filters |
||
210 | * |
||
211 | * @return \Illuminate\Support\Collection |
||
212 | */ |
||
213 | public function getMedia(string $collectionName = 'default', $filters = []): Collection |
||
214 | { |
||
215 | return app(MediaRepository::class)->getCollection($this, $collectionName, $filters); |
||
216 | } |
||
217 | |||
218 | public function getFirstMedia(string $collectionName = 'default', array $filters = []): ?Media |
||
224 | |||
225 | /* |
||
226 | * Get the url of the image for the given conversionName |
||
227 | * for first media for the given collectionName. |
||
228 | * If no profile is given, return the source's url. |
||
229 | */ |
||
230 | public function getFirstMediaUrl(string $collectionName = 'default', string $conversionName = ''): string |
||
240 | |||
241 | /* |
||
242 | * Get the url of the image for the given conversionName |
||
243 | * for first media for the given collectionName. |
||
244 | * If no profile is given, return the source's url. |
||
245 | */ |
||
246 | public function getFirstTemporaryUrl(DateTimeInterface $expiration, string $collectionName = 'default', string $conversionName = ''): string |
||
247 | { |
||
248 | $media = $this->getFirstMedia($collectionName); |
||
249 | |||
250 | if (! $media) { |
||
251 | return ''; |
||
256 | |||
257 | /* |
||
258 | * Get the url of the image for the given conversionName |
||
259 | * for first media for the given collectionName. |
||
260 | * If no profile is given, return the source's url. |
||
261 | */ |
||
262 | public function getFirstMediaPath(string $collectionName = 'default', string $conversionName = ''): string |
||
272 | |||
273 | /** |
||
274 | * Update a media collection by deleting and inserting again with new values. |
||
275 | * |
||
276 | * @param array $newMediaArray |
||
277 | * @param string $collectionName |
||
278 | * |
||
279 | * @return \Illuminate\Support\Collection |
||
280 | * |
||
281 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated |
||
282 | */ |
||
283 | public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection |
||
313 | |||
314 | protected function removeMediaItemsNotPresentInArray(array $newMediaArray, string $collectionName = 'default') |
||
322 | |||
323 | /** |
||
324 | * Remove all media in the given collection. |
||
325 | * |
||
326 | * @param string $collectionName |
||
327 | * |
||
328 | * @return $this |
||
329 | */ |
||
330 | public function clearMediaCollection(string $collectionName = 'default'): self |
||
343 | |||
344 | /** |
||
345 | * Remove all media in the given collection except some. |
||
346 | * |
||
347 | * @param string $collectionName |
||
348 | * @param \Spatie\MediaLibrary\Media[]|\Illuminate\Support\Collection $excludedMedia |
||
349 | * |
||
350 | * @return $this |
||
351 | */ |
||
352 | public function clearMediaCollectionExcept(string $collectionName = 'default', $excludedMedia = []) |
||
376 | |||
377 | /** |
||
378 | * Delete the associated media with the given id. |
||
379 | * You may also pass a media object. |
||
380 | * |
||
381 | * @param int|\Spatie\MediaLibrary\Models\Media $mediaId |
||
382 | * |
||
383 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeDeleted |
||
384 | */ |
||
385 | public function deleteMedia($mediaId) |
||
399 | |||
400 | /* |
||
401 | * Add a conversion. |
||
402 | */ |
||
403 | public function addMediaConversion(string $name): Conversion |
||
411 | |||
412 | public function addMediaCollection(string $name): MediaCollection |
||
420 | |||
421 | /** |
||
422 | * Delete the model, but preserve all the associated media. |
||
423 | * |
||
424 | * @return bool |
||
425 | */ |
||
426 | public function deletePreservingMedia(): bool |
||
432 | |||
433 | /** |
||
434 | * Determines if the media files should be preserved when the media object gets deleted. |
||
435 | * |
||
436 | * @return \Spatie\MediaLibrary\Media |
||
437 | */ |
||
438 | public function shouldDeletePreservingMedia() |
||
442 | |||
443 | protected function mediaIsPreloaded(): bool |
||
447 | |||
448 | /** |
||
449 | * Cache the media on the object. |
||
450 | * |
||
451 | * @param string $collectionName |
||
452 | * |
||
453 | * @return mixed |
||
454 | */ |
||
455 | public function loadMedia(string $collectionName) |
||
472 | |||
473 | public function prepareToAttachMedia(Media $media, FileAdder $fileAdder) |
||
477 | |||
478 | public function processUnattachedMedia(callable $callable) |
||
486 | |||
487 | protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeTypes) |
||
504 | |||
505 | public function registerMediaConversions(Media $media = null) |
||
508 | |||
509 | public function registerMediaCollections() |
||
512 | |||
513 | public function registerAllMediaConversions(Media $media = null) |
||
536 | } |
||
537 |
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: