Complex classes like Asset 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 Asset, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Asset extends Model implements HasMediaConversions |
||
| 18 | { |
||
| 19 | use HasMediaTrait; |
||
| 20 | |||
| 21 | private $order; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Attaches this asset instance to the given model and |
||
| 25 | * sets the type and locale to the given values and |
||
| 26 | * returns the model with the asset relationship. |
||
| 27 | * |
||
| 28 | * @param Model $model |
||
| 29 | * @param string $type |
||
| 30 | * @param null|string $locale |
||
| 31 | * @return Model |
||
| 32 | * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException |
||
| 33 | */ |
||
| 34 | 35 | public function attachToModel(Model $model, $type = '', $locale = null): Model |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @return bool |
||
| 51 | */ |
||
| 52 | 1 | public function hasFile(): bool |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @param string $size |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | 13 | public function getFilename($size = ''): string |
|
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $size |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | 44 | public function getFileUrl($size = ''): string |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the image url or a fallback specific per filetype. |
||
| 85 | * |
||
| 86 | * @param string $type |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | 9 | public function getImageUrl($type = ''): string |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @return bool|string |
||
| 106 | */ |
||
| 107 | 2 | public function getExtensionForFilter() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @return string|null |
||
| 118 | */ |
||
| 119 | 10 | public function getExtensionType(): ?string |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | 2 | public function getMimeType(): string |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | 5 | public function isMediaEmpty(): bool |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | 2 | public function getSize(): string |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param null $size |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | 3 | public function getDimensions($size = null): string |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Removes one or more assets by their ids. |
||
| 183 | * @param $imageIds |
||
| 184 | */ |
||
| 185 | 6 | public static function remove($imageIds) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Returns a collection of all the assets in the library. |
||
| 228 | * @return \Illuminate\Support\Collection |
||
| 229 | */ |
||
| 230 | 6 | public static function getAllAssets(): Collection |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @param $width |
||
| 237 | * @param $height |
||
| 238 | * @param $x |
||
| 239 | * @param $y |
||
| 240 | * @return $this |
||
| 241 | * @throws ConfigException |
||
| 242 | */ |
||
| 243 | 2 | public function crop($width, $height, $x, $y) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Register the conversions that should be performed. |
||
| 261 | * |
||
| 262 | * @param Media|null $media |
||
| 263 | * @throws \Spatie\Image\Exceptions\InvalidManipulation |
||
| 264 | */ |
||
| 265 | 58 | public function registerMediaConversions(Media $media = null): void |
|
| 292 | |||
| 293 | 4 | public function setOrder($order) |
|
| 299 | } |
||
| 300 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: