Complex classes like Media 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 Media, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Media extends Model implements Responsable, Htmlable |
||
26 | { |
||
27 | use IsSorted, |
||
28 | CustomMediaProperties; |
||
29 | |||
30 | const TYPE_OTHER = 'other'; |
||
31 | |||
32 | protected $guarded = []; |
||
33 | |||
34 | protected $casts = [ |
||
35 | 'manipulations' => 'array', |
||
36 | 'custom_properties' => 'array', |
||
37 | 'responsive_images' => 'array', |
||
38 | ]; |
||
39 | |||
40 | public function model(): MorphTo |
||
44 | |||
45 | /* |
||
46 | * Get the full url to a original media file. |
||
47 | */ |
||
48 | public function getFullUrl(string $conversionName = ''): string |
||
52 | |||
53 | /* |
||
54 | * Get the url to a original media file. |
||
55 | */ |
||
56 | public function getUrl(string $conversionName = ''): string |
||
62 | |||
63 | public function getTemporaryUrl(DateTimeInterface $expiration, string $conversionName = '', array $options = []): string |
||
69 | |||
70 | /* |
||
71 | * Get the path to the original media file. |
||
72 | */ |
||
73 | public function getPath(string $conversionName = ''): string |
||
79 | |||
80 | public function getImageGenerators(): Collection |
||
84 | |||
85 | public function getTypeAttribute(): string |
||
95 | |||
96 | public function getTypeFromExtension(): string |
||
108 | |||
109 | public function getTypeFromMime(): string |
||
121 | |||
122 | public function getExtensionAttribute(): string |
||
126 | |||
127 | public function getHumanReadableSizeAttribute(): string |
||
131 | |||
132 | public function getDiskDriverName(): string |
||
136 | |||
137 | /* |
||
138 | * Determine if the media item has a custom property with the given name. |
||
139 | */ |
||
140 | public function hasCustomProperty(string $propertyName): bool |
||
144 | |||
145 | /** |
||
146 | * Get the value of custom property with the given name. |
||
147 | * |
||
148 | * @param string $propertyName |
||
149 | * @param mixed $default |
||
150 | * |
||
151 | * @return mixed |
||
152 | */ |
||
153 | public function getCustomProperty(string $propertyName, $default = null) |
||
157 | |||
158 | /** |
||
159 | * @param string $name |
||
160 | * @param mixed $value |
||
161 | * |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function setCustomProperty(string $name, $value): self |
||
174 | |||
175 | public function forgetCustomProperty(string $name): self |
||
185 | |||
186 | /* |
||
187 | * Get all the names of the registered media conversions. |
||
188 | */ |
||
189 | public function getMediaConversionNames(): array |
||
197 | |||
198 | public function hasGeneratedConversion(string $conversionName): bool |
||
204 | |||
205 | public function markAsConversionGenerated(string $conversionName, bool $generated): self |
||
213 | |||
214 | public function getGeneratedConversions(): Collection |
||
218 | |||
219 | /** |
||
220 | * Create an HTTP response that represents the object. |
||
221 | * |
||
222 | * @param \Illuminate\Http\Request $request |
||
223 | * |
||
224 | * @return \Illuminate\Http\Response |
||
225 | */ |
||
226 | public function toResponse($request) |
||
246 | |||
247 | public function getResponsiveImageUrls(string $conversionName = ''): array |
||
251 | |||
252 | public function hasResponsiveImages(string $conversionName = ''): bool |
||
256 | |||
257 | public function getSrcset(string $conversionName = ''): string |
||
261 | |||
262 | public function toHtml() |
||
266 | |||
267 | /** |
||
268 | * @param string|array $conversion |
||
269 | * @param array $extraAttributes |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | public function img($conversion = '', array $extraAttributes = []): string |
||
319 | |||
320 | public function move(HasMedia $model, $collectionName = 'default', string $diskName = ''): self |
||
321 | { |
||
322 | $newMedia = $this->copy($model, $collectionName, $diskName); |
||
323 | |||
324 | $this->delete(); |
||
325 | |||
326 | return $newMedia; |
||
327 | } |
||
328 | |||
329 | public function copy(HasMedia $model, $collectionName = 'default', string $diskName = ''): self |
||
330 | { |
||
331 | $temporaryDirectory = TemporaryDirectory::create(); |
||
332 | |||
333 | $temporaryFile = $temporaryDirectory->path('/').DIRECTORY_SEPARATOR.$this->file_name; |
||
334 | |||
335 | app(Filesystem::class)->copyFromMediaLibrary($this, $temporaryFile); |
||
336 | |||
337 | $newMedia = $model |
||
338 | ->addMedia($temporaryFile) |
||
339 | ->usingName($this->name) |
||
340 | ->withCustomProperties($this->custom_properties) |
||
341 | ->toMediaCollection($collectionName, $diskName); |
||
342 | |||
343 | $temporaryDirectory->delete(); |
||
344 | |||
345 | return $newMedia; |
||
346 | } |
||
347 | |||
348 | public function responsiveImages(string $conversionName = ''): RegisteredResponsiveImages |
||
352 | |||
353 | public function stream() |
||
359 | |||
360 | public function __invoke(...$arguments) |
||
364 | } |
||
365 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: