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
Pull Request — master (#114)
by
unknown
01:02
created

OptimizerChain::setOptimizerStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\ImageOptimizer;
4
5
use Psr\Log\LoggerInterface;
6
use Spatie\ImageOptimizer\Exceptions\OptimizerNotInstalledException;
7
use Symfony\Component\Process\Process;
8
9
class OptimizerChain
10
{
11
    /* @var \Spatie\ImageOptimizer\Optimizer[] */
12
    protected $optimizers = [];
13
14
    /* @var \Spatie\ImageOptimizer\Optimizer[] */
15
    protected $optimized = [];
16
17
    /** @var \Psr\Log\LoggerInterface */
18
    protected $logger;
19
20
    /** @var int */
21
    protected $timeout = 60;
22
23
    public function __construct()
24
    {
25
        $this->useLogger(new DummyLogger());
26
    }
27
28
    public function getOptimizers(): array
29
    {
30
        return $this->optimizers;
31
    }
32
33
    public function addOptimizer(Optimizer $optimizer)
34
    {
35
        $this->optimizers[] = $optimizer;
36
37
        return $this;
38
    }
39
40
    public function setOptimizers(array $optimizers)
41
    {
42
        $this->optimizers = [];
43
44
        foreach ($optimizers as $optimizer) {
45
            $this->addOptimizer($optimizer);
46
        }
47
48
        return $this;
49
    }
50
51
    /*
52
     * Set the amount of seconds each separate optimizer may use.
53
     */
54
    public function setTimeout(int $timeoutInSeconds)
55
    {
56
        $this->timeout = $timeoutInSeconds;
57
58
        return $this;
59
    }
60
61
    public function useLogger(LoggerInterface $log)
62
    {
63
        $this->logger = $log;
64
65
        return $this;
66
    }
67
68
    public function optimize(string $pathToImage, string $pathToOutput = null)
69
    {
70
        if ($pathToOutput) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pathToOutput of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
71
            copy($pathToImage, $pathToOutput);
72
73
            $pathToImage = $pathToOutput;
74
        }
75
76
        $image = new Image($pathToImage);
77
78
        $this->logger->info("Start optimizing {$pathToImage}");
79
80
        foreach ($this->optimizers as $optimizer) {
81
            $this->applyOptimizer($optimizer, $image);
82
        }
83
84
        return $this->optimized;
85
    }
86
87
    protected function applyOptimizer(Optimizer $optimizer, Image $image)
88
    {
89
        if (! $optimizer->canHandle($image)) {
90
            return;
91
        }
92
93
        $optimizerClass = get_class($optimizer);
94
95
        $this->logger->info("Using optimizer: `{$optimizerClass}`");
96
97
        $optimizer->setImagePath($image->path());
98
99
        $command = $optimizer->getCommand();
100
101
        $this->logger->info("Executing `{$command}`");
102
103
        $process = Process::fromShellCommandline($command);
104
105
        $process
106
            ->setTimeout($this->timeout)
107
            ->run();
108
109
        $this->logResult($process, $optimizer);
110
    }
111
112
    protected function logResult(Process $process, Optimizer $optimizer)
113
    {
114
        if (! $process->isSuccessful()) {
115
            $optimized = false;
116
117
            $this->logger->error("Process errored with `{$process->getErrorOutput()}`");
118
119
            if (strpos($process->getErrorOutput(), $optimizer->binaryName().': '.strtolower(Process::$exitCodes[127])) !== false) {
120
                throw new OptimizerNotInstalledException("Optimized not installed!");
121
122
                return;
0 ignored issues
show
Unused Code introduced by
return; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
123
            }
124
        } else {
125
            $optimized = true;
126
127
            $this->logger->info("Process successfully ended with output `{$process->getOutput()}`");
128
        }
129
130
        $this->setOptimizerStatus($optimizer, $optimized);
131
    }
132
133
    private function setOptimizerStatus(Optimizer $optimizer, $status)
134
    {
135
        $this->optimized[$optimizer->binaryName()] = $status;
136
    }
137
}
138