Completed
Push — master ( f06f95...cfadd2 )
by Song
03:17
created

Image::preview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Intervention\Image\ImageManagerStatic;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
8
class Image extends File
9
{
10
    /**
11
     *  Validation rules.
12
     *
13
     * @var string
14
     */
15
    protected $rules = 'image';
16
17
    /**
18
     * Intervention calls.
19
     *
20
     * @var array
21
     */
22
    protected $calls = [];
23
24
    /**
25
     * Get default storage path.
26
     *
27
     * @return mixed
28
     */
29
    public function defaultStorePath()
30
    {
31
        return config('admin.upload.directory.image');
32
    }
33
34
    /**
35
     * Prepare for single upload file.
36
     *
37
     * @param UploadedFile|null $image
38
     *
39
     * @return string
40
     */
41
    protected function prepareForSingle(UploadedFile $image = null)
42
    {
43
        $this->directory = $this->directory ?: $this->defaultStorePath();
44
45
        $this->name = $this->getStoreName($image);
0 ignored issues
show
Bug introduced by
It seems like $image defined by parameter $image on line 41 can be null; however, Encore\Admin\Form\Field\File::getStoreName() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
Documentation Bug introduced by
It seems like $this->getStoreName($image) of type string is incompatible with the declared type null of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
47
        $this->executeCalls($image->getRealPath());
0 ignored issues
show
Bug introduced by
It seems like $image is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
48
49
        $target = $this->uploadAndDeleteOriginal($image);
0 ignored issues
show
Bug introduced by
It seems like $image defined by parameter $image on line 41 can be null; however, Encore\Admin\Form\Field\...loadAndDeleteOriginal() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
50
51
        return $target;
52
    }
53
54
    /**
55
     * Execute Intervention calls.
56
     *
57
     * @param string $target
58
     *
59
     * @return mixed
60
     */
61
    public function executeCalls($target)
62
    {
63
        if (!empty($this->calls)) {
64
            $image = ImageManagerStatic::make($target);
65
66
            foreach ($this->calls as $call) {
67
                call_user_func_array([$image, $call['method']], $call['arguments'])->save($target);
68
            }
69
        }
70
71
        return $target;
72
    }
73
74
    /**
75
     * Build a preview item.
76
     *
77
     * @param string $image
78
     * @return string
79
     */
80
    protected function buildPreviewItem($image)
81
    {
82
        return '<img src="'.$this->objectUrl($image).'" class="file-preview-image">';
83
    }
84
85
    /**
86
     * Render a image form field.
87
     *
88
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
89
     */
90
    public function render()
91
    {
92
        $this->options(['allowedFileTypes' => ['image']]);
93
94
        return parent::render();
95
    }
96
97
    /**
98
     * Call intervention methods.
99
     *
100
     * @param string $method
101
     * @param array  $arguments
102
     * @return $this
103
     */
104
    public function __call($method, $arguments)
105
    {
106
        $this->calls[] = [
107
            'method'    => $method,
108
            'arguments' => $arguments,
109
        ];
110
111
        return $this;
112
    }
113
}
114