GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( adfb0b...993433 )
by Freek
05:10
created

Manipulations::apply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Image;
4
5
class Manipulations
6
{
7
    const CROP_TOP_LEFT = 'crop-top-left';
8
    const CROP_TOP = 'crop-top';
9
    const CROP_TOP_RIGHT = 'crop-top-right';
10
    const CROP_LEFT = 'crop-left';
11
    const CROP_CENTER = 'crop-center';
12
    const CROP_RIGHT = 'crop-right';
13
    const CROP_BOTTOM_LEFT = 'crop-bottom-left';
14
    const CROP_BOTTOM = 'crop-bottom';
15
    const CROP_BOTTOM_RIGHT = 'crop-bottom-right';
16
17
    const ORIENTATION_AUTO = 'auto';
18
    const ORIENTATION_90 = 90;
19
    const ORIENTATION_180 = 180;
20
    const ORIENTATION_270 = 270;
21
22
    const FIT_CONTAIN = 'contain';
23
    const FIT_MAX = 'max';
24
    const FIT_FILL = 'fill';
25
    const FIT_STRETCH = 'stretch';
26
    const FIT_CROP = 'crop';
27
28
    const BORDER_OVERLAY = 'overlay';
29
    const BORDER_SHRINK = 'shrink';
30
    const BORDER_EXPAND = 'expand';
31
32
    const FORMAT_JPG = 'jpg';
33
    const FORMAT_PJPG = 'pjpg';
34
    const FORMAT_PNG = 'png';
35
    const FORMAT_GIF = 'gif';
36
37
    /** @var array */
38
    protected $manipulationSets = [];
39
40
    public function __construct()
41
    {
42
        $this->manipulationSets = new ManipulationSets();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Spatie\Image\ManipulationSets() of type object<Spatie\Image\ManipulationSets> is incompatible with the declared type array of property $manipulationSets.

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...
43
    }
44
45
46
    public function __construct(array $manipulations = [])
47
    {
48
        $this->manipulations = $manipulations;
0 ignored issues
show
Bug introduced by
The property manipulations does not seem to exist. Did you mean manipulationSets?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
49
    }
50
51
    /**
52
     * @param string $orientation
53
     * @return $this
54
     */
55
    public function orientation(string $orientation)
0 ignored issues
show
Unused Code introduced by
The parameter $orientation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
        return $this->setManipulation(func_get_args());
58
    }
59
60
    /**
61
     * @param string $cropMethod
62
     * @param int $width
63
     * @param int $height
64
     * @return $this
65
     *
66
     * @internal param string $method
67
     */
68
    public function crop(string $cropMethod, int $width, int $height)
69
    {
70
        return $this
71
            ->setManipulation([$cropMethod], 'crop')
72
            ->setManipulation([$width], 'width')
73
            ->setManipulation([$height], 'height');
74
    }
75
76
    /**
77
     * @param int $width
78
     *
79
     * @return $this
80
     */
81
    public function width(int $width)
0 ignored issues
show
Unused Code introduced by
The parameter $width is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        return $this->setManipulation(func_get_args());
84
    }
85
86
    /**
87
     * @param int $height
88
     *
89
     * @return $this
90
     */
91
    public function height(int $height)
0 ignored issues
show
Unused Code introduced by
The parameter $height is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
        return $this->setManipulation(func_get_args());
94
    }
95
96
    public function fit(string $fitMethod, int $width, int $height)
97
    {
98
        return $this
99
            ->setManipulation($fitMethod, 'fit')
0 ignored issues
show
Documentation introduced by
$fitMethod is of type string, but the function expects a array.

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...
100
            ->setManipulation($width, 'width')
0 ignored issues
show
Documentation introduced by
$width is of type integer, but the function expects a array.

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...
101
            ->setManipulation($height, 'height');
0 ignored issues
show
Documentation introduced by
$height is of type integer, but the function expects a array.

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...
102
    }
103
104
    public function devicePixelRatio(int $ratio)
0 ignored issues
show
Unused Code introduced by
The parameter $ratio is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
    {
106
        return $this->setManipulation(func_get_args());
107
    }
108
109
    public function brightness(int $brightness)
0 ignored issues
show
Unused Code introduced by
The parameter $brightness is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        return $this->setManipulation(func_get_args());
112
    }
113
114
    public function gamma(float $gamma)
0 ignored issues
show
Unused Code introduced by
The parameter $gamma is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116
        return $this->setManipulation(func_get_args());
117
    }
118
119
    public function contrast(int $contrast)
0 ignored issues
show
Unused Code introduced by
The parameter $contrast is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121
        return $this->setManipulation(func_get_args());
122
    }
123
124
    public function sharpen(int $sharpen)
0 ignored issues
show
Unused Code introduced by
The parameter $sharpen is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126
        return $this->setManipulation(func_get_args());
127
    }
128
129
    public function blur(int $blur)
0 ignored issues
show
Unused Code introduced by
The parameter $blur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
    {
131
        return $this->setManipulation(func_get_args());
132
    }
133
134
    public function pixelate(int $pixelate)
0 ignored issues
show
Unused Code introduced by
The parameter $pixelate is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
    {
136
        return $this->setManipulation(func_get_args());
137
    }
138
139
    public function greyscale()
140
    {
141
        return $this->filter('greyscale');
142
    }
143
144
    public function sepia()
145
    {
146
        return $this->filter('sepia');
147
    }
148
149
    public function background(string $colorName)
0 ignored issues
show
Unused Code introduced by
The parameter $colorName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
    {
151
        return $this->setManipulation(func_get_args());
152
    }
153
154
    public function border(int $width, string $color, string $borderType = 'overlay')
155
    {
156
        return $this->setManipulation(["{$width},{$color},{$borderType}"], 'border');
157
    }
158
159
    public function quality(int $quality)
0 ignored issues
show
Unused Code introduced by
The parameter $quality is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
    {
161
        return $this->setManipulation(func_get_args());
162
    }
163
164
    public function format(string $format)
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
165
    {
166
        return $this->setManipulation(func_get_args());
167
    }
168
169
    protected function filter(string $filterName)
0 ignored issues
show
Unused Code introduced by
The parameter $filterName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
170
    {
171
        return $this->setManipulation(func_get_args());
172
    }
173
174
175
    public function apply()
176
    {
177
        $this-$this->manipulationSets->startNewSet();
0 ignored issues
show
Bug introduced by
The method startNewSet cannot be called on $this->manipulationSets (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
178
    }
179
180
181
    public function hasManipulation($manipulationName): bool
182
    {
183
        return ! is_null($this->getManipulation($manipulationName));
184
    }
185
186
    /**
187
     * @param string $manipulationName
188
     * @return arrau|null
189
     */
190
    public function getManipulation(string $manipulationName)
191
    {
192
        foreach($this->manipulations as $manipulation) {
0 ignored issues
show
Bug introduced by
The property manipulations does not seem to exist. Did you mean manipulationSets?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
193
            if ($manipulation[0] === $manipulationName) {
194
                return $manipulation;
195
            }
196
        }
197
198
        return null;
199
    }
200
201
202
203
    protected function setManipulation(array $arguments, string $operation = null)
204
    {
205
        $operation = $operation ?? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
206
207
        $this->manipulationSets->addManipulation($operation, $arguments);
0 ignored issues
show
Bug introduced by
The method addManipulation cannot be called on $this->manipulationSets (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
208
209
        return $this;
210
    }
211
212
    public function mergeManipulations(Manipulations $manipulations)
213
    {
214
        $this->manipulations = array_merge($this->manipulations, $manipulations->toArray());
0 ignored issues
show
Bug introduced by
The property manipulations does not seem to exist. Did you mean manipulationSets?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The method toArray() does not seem to exist on object<Spatie\Image\Manipulations>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
215
216
        return $this;
217
    }
218
219
    public function getManipulationSets(): ManipulationSets
220
    {
221
        return $this->manipulationSets;
222
    }
223
}
224