We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 34 |
Paths | 2144 |
Total Lines | 149 |
Code Lines | 92 |
Lines | 50 |
Ratio | 33.56 % |
Changes | 2 | ||
Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
212 | public function uploadImageToDisk($value, $attribute_name, $disk, $destination_path, $variations = null) |
||
213 | { |
||
214 | if (! $variations || ! is_array($variations)) { |
||
215 | $variations = ['original' => null, 'thumb' => [150, 150]]; |
||
216 | } |
||
217 | |||
218 | //Needed for the original image |
||
219 | if (! array_key_exists('original', $variations)) { |
||
220 | $variations['original'] = null; |
||
221 | } |
||
222 | |||
223 | //Needed for admin thumbnails |
||
224 | if (! array_key_exists('thumb', $variations)) { |
||
225 | $variations['thumb'] = [150, 150]; |
||
226 | } |
||
227 | |||
228 | $request = \Request::instance(); |
||
229 | |||
230 | //We need to setup the disk paths as they're handled differently |
||
231 | //depending if you need a public path or internal storage |
||
232 | $disk_config = config('filesystems.disks.'.$disk); |
||
233 | $disk_root = $disk_config['root']; |
||
234 | |||
235 | //if the disk is public, we need to know the public path |
||
236 | View Code Duplication | if ($disk_config['visibility'] == 'public') { |
|
237 | $public_path = str_replace(public_path(), '', $disk_root); |
||
238 | } else { |
||
239 | $public_path = $disk_root; |
||
240 | } |
||
241 | |||
242 | // if a new file is uploaded, delete the file from the disk |
||
243 | if (($request->hasFile($attribute_name) || starts_with($value, 'data:image')) && $this->{$attribute_name}) { |
||
244 | View Code Duplication | foreach ($variations as $variant => $dimensions) { |
|
245 | $variant_name = str_replace('-original', '-'.$variant, $this->{$attribute_name}); |
||
246 | \Storage::disk($disk)->delete($variant_name); |
||
247 | } |
||
248 | $this->attributes[$attribute_name] = null; |
||
249 | } |
||
250 | |||
251 | // if the file input is empty, delete the file from the disk |
||
252 | if (empty($value)) { |
||
253 | View Code Duplication | foreach ($variations as $variant => $dimensions) { |
|
254 | $variant_name = str_replace('-original', '-'.$variant, $this->{$attribute_name}); |
||
255 | \Storage::disk($disk)->delete($variant_name); |
||
256 | } |
||
257 | |||
258 | return $this->attributes[$attribute_name] = null; |
||
259 | } |
||
260 | |||
261 | // if a new file is uploaded, store it on disk and its filename in the database |
||
262 | if ($request->hasFile($attribute_name) && $request->file($attribute_name)->isValid()) { |
||
263 | |||
264 | // 1. Generate a new file name |
||
265 | $file = $request->file($attribute_name); |
||
266 | $new_file_name = md5($file->getClientOriginalName().time()); |
||
267 | $new_file = $new_file_name.'.'.$file->getClientOriginalExtension(); |
||
268 | |||
269 | // 2. Move the new file to the correct path |
||
270 | $file_path = $file->storeAs($destination_path, $new_file, $disk); |
||
271 | $image_variations = []; |
||
272 | |||
273 | // 3. but only if they have the ability to crop/handle images |
||
274 | if (class_exists('\Intervention\Image\ImageManagerStatic')) { |
||
275 | $img = \Intervention\Image\ImageManagerStatic::make($file); |
||
276 | foreach ($variations as $variant => $dimensions) { |
||
277 | $variant_name = $new_file_name.'-'.$variant.'.'.$file->getClientOriginalExtension(); |
||
278 | $variant_file = $destination_path.'/'.$variant_name; |
||
279 | |||
280 | View Code Duplication | if ($dimensions) { |
|
281 | $width = $dimensions[0]; |
||
282 | $height = $dimensions[1]; |
||
283 | |||
284 | if ($img->width() > $width || $img->height() > $height) { |
||
285 | $img->resize($width, $height, function ($constraint) { |
||
286 | $constraint->aspectRatio(); |
||
287 | }) |
||
288 | ->save($disk_root.'/'.$variant_file); |
||
289 | } else { |
||
290 | $img->save($disk_root.'/'.$variant_file); |
||
291 | } |
||
292 | |||
293 | $image_variations[$variant] = $public_path.'/'.$variant_file; |
||
294 | } else { |
||
295 | $image_variations['original'] = $public_path.'/'.$file_path; |
||
296 | } |
||
297 | } |
||
298 | } else { |
||
299 | $image_variations['original'] = $public_path.'/'.$file_path; |
||
300 | $image_variations['thumb'] = $public_path.'/'.$file_path; |
||
301 | } |
||
302 | |||
303 | // 3. Save the complete path to the database |
||
304 | $this->attributes[$attribute_name] = $image_variations['original']; |
||
305 | } elseif (starts_with($value, 'data:image')) { |
||
306 | $img = \Intervention\Image\ImageManagerStatic::make($value); |
||
307 | $new_file_name = md5($value.time()); |
||
308 | |||
309 | if (! \Illuminate\Support\Facades\File::exists($disk_root.'/'.trim($destination_path, '/'))) { |
||
310 | \Illuminate\Support\Facades\File::makeDirectory($disk_root.'/'.trim($destination_path, '/'), 0775, true); |
||
311 | } |
||
312 | |||
313 | foreach ($variations as $variant => $dimensions) { |
||
314 | switch ($img->mime()) { |
||
315 | case 'image/bmp': |
||
316 | case 'image/ief': |
||
317 | case 'image/jpeg': |
||
318 | case 'image/pipeg': |
||
319 | case 'image/tiff': |
||
320 | case 'image/x-jps': |
||
321 | $extension = '.jpg'; |
||
322 | break; |
||
323 | case 'image/gif': |
||
324 | $extension = '.gif'; |
||
325 | break; |
||
326 | case 'image/x-icon': |
||
327 | case 'image/png': |
||
328 | $extension = '.png'; |
||
329 | break; |
||
330 | default: |
||
331 | $extension = '.jpg'; |
||
332 | break; |
||
333 | } |
||
334 | |||
335 | $variant_name = $new_file_name.'-'.$variant.$extension; |
||
336 | $variant_file = $destination_path.'/'.$variant_name; |
||
337 | |||
338 | View Code Duplication | if ($dimensions) { |
|
339 | $width = $dimensions[0]; |
||
340 | $height = $dimensions[1]; |
||
341 | |||
342 | if ($img->width() > $width || $img->height() > $height) { |
||
343 | $img->resize($width, $height, function ($constraint) { |
||
344 | $constraint->aspectRatio(); |
||
345 | }) |
||
346 | ->save($disk_root.'/'.$variant_file); |
||
347 | } else { |
||
348 | $img->save($disk_root.'/'.$variant_file); |
||
349 | } |
||
350 | |||
351 | $image_variations[$variant] = $public_path.'/'.$variant_file; |
||
352 | } else { |
||
353 | $img->save($disk_root.'/'.$variant_file); |
||
354 | $image_variations['original'] = $variant_file; |
||
355 | } |
||
356 | } |
||
357 | |||
358 | $this->attributes[$attribute_name] = $image_variations['original']; |
||
359 | } |
||
360 | } |
||
361 | |||
404 |
This check marks parameter names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.