| Total Complexity | 56 | 
| Total Lines | 280 | 
| Duplicated Lines | 0 % | 
| Changes | 2 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like HasMedias 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.
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 HasMedias, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 9 | trait HasMedias  | 
            ||
| 10 | { | 
            ||
| 11 | protected $cropParamsKeys = [  | 
            ||
| 12 | 'crop_x',  | 
            ||
| 13 | 'crop_y',  | 
            ||
| 14 | 'crop_w',  | 
            ||
| 15 | 'crop_h',  | 
            ||
| 16 | ];  | 
            ||
| 17 | |||
| 18 | public function medias()  | 
            ||
| 19 |     { | 
            ||
| 20 | return $this->morphToMany(  | 
            ||
| 21 | Media::class,  | 
            ||
| 22 | 'mediable',  | 
            ||
| 23 |             config('twill.mediables_table', 'twill_mediables') | 
            ||
| 24 | )->withPivot(array_merge([  | 
            ||
| 25 | 'crop',  | 
            ||
| 26 | 'role',  | 
            ||
| 27 | 'crop_w',  | 
            ||
| 28 | 'crop_h',  | 
            ||
| 29 | 'crop_x',  | 
            ||
| 30 | 'crop_y',  | 
            ||
| 31 | 'lqip_data',  | 
            ||
| 32 | 'ratio',  | 
            ||
| 33 | 'metadatas',  | 
            ||
| 34 |         ], config('twill.media_library.translated_form_fields', false) ? ['locale'] : [])) | 
            ||
| 35 |             ->withTimestamps()->orderBy(config('twill.mediables_table', 'twill_mediables') . '.id', 'asc'); | 
            ||
| 36 | }  | 
            ||
| 37 | |||
| 38 | private function findMedia($role, $crop = "default")  | 
            ||
| 55 | }  | 
            ||
| 56 | |||
| 57 | public function hasImage($role, $crop = "default")  | 
            ||
| 58 |     { | 
            ||
| 59 | $media = $this->findMedia($role, $crop);  | 
            ||
| 60 | |||
| 61 | return !empty($media);  | 
            ||
| 62 | }  | 
            ||
| 63 | |||
| 64 | public function image($role, $crop = "default", $params = [], $has_fallback = false, $cms = false, $media = null)  | 
            ||
| 65 |     { | 
            ||
| 66 | |||
| 67 |         if (!$media) { | 
            ||
| 68 | $media = $this->findMedia($role, $crop);  | 
            ||
| 69 | }  | 
            ||
| 70 | |||
| 71 |         if ($media) { | 
            ||
| 72 | |||
| 73 | $crop_params = Arr::only($media->pivot->toArray(), $this->cropParamsKeys);  | 
            ||
| 74 | |||
| 75 |             if ($cms) { | 
            ||
| 76 | |||
| 77 | return ImageService::getCmsUrl($media->uuid, $crop_params + $params);  | 
            ||
| 78 | }  | 
            ||
| 79 | |||
| 80 | return ImageService::getUrlWithCrop($media->uuid, $crop_params, $params);  | 
            ||
| 81 | }  | 
            ||
| 82 | |||
| 83 |         if ($has_fallback) { | 
            ||
| 84 | return null;  | 
            ||
| 85 | }  | 
            ||
| 86 | |||
| 87 | return ImageService::getTransparentFallbackUrl();  | 
            ||
| 88 | }  | 
            ||
| 89 | |||
| 90 | public function images($role, $crop = "default", $params = [])  | 
            ||
| 103 | }  | 
            ||
| 104 | |||
| 105 | public function imagesWithCrops($role, $params = [])  | 
            ||
| 106 |     { | 
            ||
| 107 |         $medias = $this->medias->filter(function ($media) use ($role) { | 
            ||
| 108 | return $media->pivot->role === $role;  | 
            ||
| 109 | });  | 
            ||
| 110 | |||
| 111 | $urls = [];  | 
            ||
| 112 | |||
| 113 |         foreach ($medias as $media) { | 
            ||
| 114 | $paramsForCrop = $params[$media->pivot->crop] ?? [];  | 
            ||
| 115 | $urls[$media->id][$media->pivot->crop] = $this->image($role, $media->pivot->crop, $paramsForCrop, false, false, $media);  | 
            ||
| 116 | }  | 
            ||
| 117 | |||
| 118 | return $urls;  | 
            ||
| 119 | }  | 
            ||
| 120 | |||
| 121 | public function imageAsArray($role, $crop = "default", $params = [], $media = null)  | 
            ||
| 122 |     { | 
            ||
| 123 |         if (!$media) { | 
            ||
| 124 | $media = $this->findMedia($role, $crop);  | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 |         if ($media) { | 
            ||
| 128 | return [  | 
            ||
| 129 | 'src' => $this->image($role, $crop, $params, false, false, $media),  | 
            ||
| 130 | 'width' => $media->pivot->crop_w ?? $media->width,  | 
            ||
| 131 | 'height' => $media->pivot->crop_h ?? $media->height,  | 
            ||
| 132 | 'alt' => $this->imageAltText($role, $media),  | 
            ||
| 133 | 'caption' => $this->imageCaption($role, $media),  | 
            ||
| 134 | 'video' => $this->imageVideo($role, $media),  | 
            ||
| 135 | ];  | 
            ||
| 136 | }  | 
            ||
| 137 | |||
| 138 | return [];  | 
            ||
| 139 | }  | 
            ||
| 140 | |||
| 141 | public function imagesAsArrays($role, $crop = "default", $params = [])  | 
            ||
| 142 |     { | 
            ||
| 143 |         $medias = $this->medias->filter(function ($media) use ($role, $crop) { | 
            ||
| 144 | return $media->pivot->role === $role && $media->pivot->crop === $crop;  | 
            ||
| 145 | });  | 
            ||
| 146 | |||
| 147 | $arrays = [];  | 
            ||
| 148 | |||
| 149 |         foreach ($medias as $media) { | 
            ||
| 150 | $arrays[] = $this->imageAsArray($role, $crop, $params, $media);  | 
            ||
| 151 | }  | 
            ||
| 152 | |||
| 153 | return $arrays;  | 
            ||
| 154 | }  | 
            ||
| 155 | |||
| 156 | public function imagesAsArraysWithCrops($role, $params = [])  | 
            ||
| 157 |     { | 
            ||
| 158 |         $medias = $this->medias->filter(function ($media) use ($role) { | 
            ||
| 159 | return $media->pivot->role === $role;  | 
            ||
| 160 | });  | 
            ||
| 161 | |||
| 162 | $arrays = [];  | 
            ||
| 163 | |||
| 164 |         foreach ($medias as $media) { | 
            ||
| 165 | $paramsForCrop = $params[$media->pivot->crop] ?? [];  | 
            ||
| 166 | $arrays[$media->id][$media->pivot->crop] = $this->imageAsArray($role, $media->pivot->crop, $paramsForCrop, $media);  | 
            ||
| 167 | }  | 
            ||
| 168 | |||
| 169 | return $arrays;  | 
            ||
| 170 | }  | 
            ||
| 171 | |||
| 172 | public function imageAltText($role, $media = null)  | 
            ||
| 173 |     { | 
            ||
| 174 |         if (!$media) { | 
            ||
| 175 |             $media = $this->medias->first(function ($media) use ($role) { | 
            ||
| 176 |                 if (config('twill.media_library.translated_form_fields', false)) { | 
            ||
| 177 | $localeScope = $media->pivot->locale === app()->getLocale();  | 
            ||
| 178 | }  | 
            ||
| 179 | |||
| 180 | return $media->pivot->role === $role && ($localeScope ?? true);;  | 
            ||
| 181 | });  | 
            ||
| 182 | }  | 
            ||
| 183 | |||
| 184 |         if ($media) { | 
            ||
| 185 |             return $media->getMetadata('altText', 'alt_text'); | 
            ||
| 186 | }  | 
            ||
| 187 | |||
| 188 | return '';  | 
            ||
| 189 | }  | 
            ||
| 190 | |||
| 191 | public function imageCaption($role, $media = null)  | 
            ||
| 192 |     { | 
            ||
| 193 |         if (!$media) { | 
            ||
| 194 |             $media = $this->medias->first(function ($media) use ($role) { | 
            ||
| 195 |                 if (config('twill.media_library.translated_form_fields', false)) { | 
            ||
| 196 | $localeScope = $media->pivot->locale === app()->getLocale();  | 
            ||
| 197 | }  | 
            ||
| 198 | |||
| 199 | return $media->pivot->role === $role && ($localeScope ?? true);;  | 
            ||
| 200 | });  | 
            ||
| 201 | }  | 
            ||
| 202 | |||
| 203 |         if ($media) { | 
            ||
| 204 |             return $media->getMetadata('caption'); | 
            ||
| 205 | }  | 
            ||
| 206 | |||
| 207 | return '';  | 
            ||
| 208 | }  | 
            ||
| 209 | |||
| 210 | public function imageVideo($role, $media = null)  | 
            ||
| 211 |     { | 
            ||
| 212 |         if (!$media) { | 
            ||
| 213 |             $media = $this->medias->first(function ($media) use ($role) { | 
            ||
| 214 |                 if (config('twill.media_library.translated_form_fields', false)) { | 
            ||
| 215 | $localeScope = $media->pivot->locale === app()->getLocale();  | 
            ||
| 216 | }  | 
            ||
| 217 | |||
| 218 | return $media->pivot->role === $role && ($localeScope ?? true);;  | 
            ||
| 219 | });  | 
            ||
| 220 | }  | 
            ||
| 221 | |||
| 222 |         if ($media) { | 
            ||
| 223 | $metadatas = (object) json_decode($media->pivot->metadatas);  | 
            ||
| 224 | $language = app()->getLocale();  | 
            ||
| 225 | return $metadatas->video->$language ?? (is_object($metadatas->video) ? '' : ($metadatas->video ?? ''));  | 
            ||
| 226 | }  | 
            ||
| 227 | |||
| 228 | return '';  | 
            ||
| 229 | }  | 
            ||
| 230 | |||
| 231 | public function imageObject($role, $crop = "default")  | 
            ||
| 232 |     { | 
            ||
| 233 | return $this->findMedia($role, $crop);  | 
            ||
| 234 | }  | 
            ||
| 235 | |||
| 236 | public function lowQualityImagePlaceholder($role, $crop = "default", $params = [], $has_fallback = false)  | 
            ||
| 237 |     { | 
            ||
| 238 | $media = $this->findMedia($role, $crop);  | 
            ||
| 239 | |||
| 240 |         if ($media) { | 
            ||
| 241 | return $media->pivot->lqip_data ?? ImageService::getTransparentFallbackUrl();  | 
            ||
| 242 | }  | 
            ||
| 243 | |||
| 244 |         if ($has_fallback) { | 
            ||
| 245 | return null;  | 
            ||
| 246 | }  | 
            ||
| 247 | |||
| 248 | return ImageService::getTransparentFallbackUrl();  | 
            ||
| 249 | |||
| 250 | }  | 
            ||
| 251 | |||
| 252 | public function socialImage($role, $crop = "default", $params = [], $has_fallback = false)  | 
            ||
| 253 |     { | 
            ||
| 254 | $media = $this->findMedia($role, $crop);  | 
            ||
| 255 | |||
| 256 |         if ($media) { | 
            ||
| 257 | $crop_params = Arr::only($media->pivot->toArray(), $this->cropParamsKeys);  | 
            ||
| 258 | |||
| 259 | return ImageService::getSocialUrl($media->uuid, $crop_params + $params);  | 
            ||
| 260 | }  | 
            ||
| 261 | |||
| 262 |         if ($has_fallback) { | 
            ||
| 263 | return null;  | 
            ||
| 264 | }  | 
            ||
| 265 | |||
| 266 | return ImageService::getSocialFallbackUrl();  | 
            ||
| 267 | }  | 
            ||
| 268 | |||
| 269 | public function cmsImage($role, $crop = "default", $params = [])  | 
            ||
| 270 |     { | 
            ||
| 271 | return $this->image($role, $crop, $params, false, true, false) ?? ImageService::getTransparentFallbackUrl($params);  | 
            ||
| 272 | }  | 
            ||
| 273 | |||
| 274 | public function defaultCmsImage($params = [])  | 
            ||
| 283 | }  | 
            ||
| 284 | |||
| 285 | public function imageObjects($role, $crop = "default")  | 
            ||
| 289 | });  | 
            ||
| 290 | }  | 
            ||
| 291 | }  | 
            ||
| 292 | 
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths