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 ( 0fca6a...7e109b )
by Freek
01:16
created

Image::shouldOptimize()   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
use BadMethodCallException;
6
use ImageOptimizer\OptimizerFactory;
7
use Spatie\Image\Exceptions\InvalidImageDriver;
8
9
/** @mixin \Spatie\Image\Manipulations */
10
class Image
11
{
12
    /** @var string */
13
    protected $pathToImage;
14
15
    /** @var \Spatie\Image\Manipulations */
16
    protected $manipulations;
17
18
    protected $imageDriver = 'gd';
19
20
    /**
21
     * @param string $pathToImage
22
     *
23
     * @return static
24
     */
25
    public static function load(string $pathToImage)
26
    {
27
        return new static($pathToImage);
28
    }
29
30
    public function __construct(string $pathToImage)
31
    {
32
        $this->pathToImage = $pathToImage;
33
34
        $this->manipulations = new Manipulations();
35
    }
36
37
    /**
38
     * @param string $imageDriver
39
     *
40
     * @return $this
41
     *
42
     * @throws InvalidImageDriver
43
     */
44
    public function useImageDriver(string $imageDriver)
45
    {
46
        if (! in_array($imageDriver, ['gd', 'imagick'])) {
47
            throw InvalidImageDriver::driver($imageDriver);
48
        }
49
50
        $this->imageDriver = $imageDriver;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param callable|$manipulations
57
     *
58
     * @return $this
59
     */
60
    public function manipulate($manipulations)
61
    {
62
        if (is_callable($manipulations)) {
63
            $manipulations($this->manipulations);
64
        }
65
66
        if ($manipulations instanceof Manipulations) {
67
            $this->manipulations->mergeManipulations($manipulations);
68
        }
69
70
        return $this;
71
    }
72
73
    public function __call($name, $arguments)
74
    {
75
        if (! method_exists($this->manipulations, $name)) {
76
            throw new BadMethodCallException("Manipulation `{$name}` does not exist");
77
        }
78
79
        $this->manipulations->$name(...$arguments);
80
81
        return $this;
82
    }
83
84
    public function getManipulationSequence(): ManipulationSequence
85
    {
86
        return $this->manipulations->getManipulationSequence();
87
    }
88
89
    public function save($outputPath = '')
90
    {
91
        if ($outputPath == '') {
92
            $outputPath = $this->pathToImage;
93
        }
94
95
        $this->addFormatManipulation($outputPath);
96
97
        GlideConversion::create($this->pathToImage)
98
            ->useImageDriver($this->imageDriver)
99
            ->performManipulations($this->manipulations)
100
            ->save($outputPath);
101
102
        if ($this->shouldOptimize()) {
103
            $opmitizationOptions = $this->manipulations->getFirstManipulationArgument('optimize');
0 ignored issues
show
Unused Code introduced by
$opmitizationOptions is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
104
105
            $opmitizationOptions = json_decode("{}", true);
106
107
            $this->performOptimization($outputPath, $opmitizationOptions);
108
        }
109
    }
110
111
    protected function shouldOptimize(): bool
112
    {
113
        return ! is_null($this->manipulations->getFirstManipulationArgument('optimize'));
114
    }
115
116
    protected function performOptimization($path, array $optimizationOptions)
117
    {
118
        $factory = new OptimizerFactory($optimizationOptions);
119
120
        $optimizer = $factory->get();
121
122
        $optimizer->optimize($path);
123
    }
124
125
    protected function addFormatManipulation($outputPath)
126
    {
127
        if ($this->manipulations->hasManipulation('format')) {
128
            return;
129
        }
130
131
        $inputExtension = strtolower(pathinfo($this->pathToImage, PATHINFO_EXTENSION));
132
        $outputExtension = strtolower(pathinfo($outputPath, PATHINFO_EXTENSION));
133
134
        if ($inputExtension === $outputExtension) {
135
            return;
136
        }
137
138
        $supportedFormats = ['jpg', 'png', 'gif'];
139
140
        if (in_array($outputExtension, $supportedFormats)) {
141
            $this->manipulations->format($outputExtension);
142
        }
143
    }
144
}
145