Optimizer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 0
cbo 6
dl 0
loc 65
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B resizepost() 0 25 5
A resizeremote() 0 21 4
A getName() 0 11 3
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Approached\LaravelImageOptimizer\ImageOptimizer;
6
use File;
7
use GuzzleHttp\Client;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Storage;
10
use Intervention\Image\Facades\Image;
11
12
class Optimizer extends Controller
13
{
14
15
    const EXTENSION_DOT_LENGTH = -4;
16
17
    public function resizepost(Request $request, ImageOptimizer $imageOptimizer)
18
    {
19
        try {
20
            $pic = $request->file('picture');
21
            $width = ($request->get('width')) ? $request->get('width') : 640;
22
            $height = ($request->get('height')) ? $request->get('height') : null;
23
            $imageOptimizer->optimizeUploadedImageFile($pic);
0 ignored issues
show
Bug introduced by
It seems like $pic defined by $request->file('picture') on line 20 can also be of type array or null; however, Approached\LaravelImageO...mizeUploadedImageFile() does only seem to accept object<Symfony\Component...tion\File\UploadedFile>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
24
            $newName = substr_replace($pic->getClientOriginalName(),
25
                $this->getName($width, $height) . '.' . $pic->getClientOriginalExtension(), self::EXTENSION_DOT_LENGTH);
26
            Storage::put('compressed/' . $newName, File::get($pic));
0 ignored issues
show
Documentation introduced by
$pic is of type object<Illuminate\Http\UploadedFile>|array|null, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
27
            $img = Image::make(storage_path('app/compressed/') . $newName);
28
            $resize = $img->resize($width, $height, function ($constraint) {
29
                $constraint->aspectRatio();
30
                $constraint->upsize();
31
            });
32
            if ($resize) {
33
                $img->save(storage_path('app/resized/') . $newName);
34
                unlink(storage_path('app/compressed/') . $newName);
35
                return $img->response();
36
            }
37
        } catch (\Exception $e) {
38
            return response()->json(['bad_input' => 'The input image is corrupted'], 204);
39
        }
40
        return response()->json(['bad_input' => 'The input image is corrupted'], 204);
41
    }
42
43
    public function resizeremote(Request $request, ImageOptimizer $imageOptimizer)
44
    {
45
        $pic = $request->get('image');
46
        $width = ($request->get('width')) ? $request->get('width') : 640;
47
        $height = ($request->get('height')) ? $request->get('height') : null;
48
        $file = storage_path('app/remote/') . basename($pic);
49
        $client = new Client();
50
        $client->request('GET', $pic, [
51
            'sink' => storage_path('app/remote/') . basename($pic),
52
        ]);
53
        $imageOptimizer->optimizeImage($file);
54
        $img = Image::make($file);
55
        $resize = $img->resize($width, $height, function ($constraint) {
56
            $constraint->aspectRatio();
57
            $constraint->upsize();
58
        });
59
        if ($resize) {
60
            return $img->response();
61
        }
62
        return response()->json(['bad_input' => 'The input image is corrupted']);
63
    }
64
65
    private function getName($w, $h)
66
    {
67
        $postfix = '_';
68
        if ($w) {
69
            $postfix .= 'w' . $w;
70
        }
71
        if ($h) {
72
            $postfix .= 'h' . $h;
73
        }
74
        return $postfix;
75
    }
76
}
77