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 (#98)
by
unknown
02:37
created

OptimizerChain::isCli()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
namespace Spatie\ImageOptimizer;
4
5
use Psr\Log\LoggerInterface;
6
use Symfony\Component\Process\Process;
7
8
class OptimizerChain
9
{
10
    /* @var \Spatie\ImageOptimizer\Optimizer[] */
11
    protected $optimizers = [];
12
13
    /** @var \Psr\Log\LoggerInterface */
14
    protected $logger;
15
16
    /** @var int */
17
    protected $timeout = 60;
18
19
    public function __construct()
20
    {
21
        $this->useLogger(new DummyLogger());
22
    }
23
24
    public function getOptimizers(): array
25
    {
26
        return $this->optimizers;
27
    }
28
29
    public function addOptimizer(Optimizer $optimizer)
30
    {
31
        $this->optimizers[] = $optimizer;
32
33
        return $this;
34
    }
35
36
    public function setOptimizers(array $optimizers)
37
    {
38
        $this->optimizers = [];
39
40
        foreach ($optimizers as $optimizer) {
41
            $this->addOptimizer($optimizer);
42
        }
43
44
        return $this;
45
    }
46
47
    /*
48
     * Set the amount of seconds each separate optimizer may use.
49
     */
50
    public function setTimeout(int $timeoutInSeconds)
51
    {
52
        $this->timeout = $timeoutInSeconds;
53
54
        return $this;
55
    }
56
57
    public function useLogger(LoggerInterface $log)
58
    {
59
        $this->logger = $log;
60
61
        return $this;
62
    }
63
64
    public function optimize(string $pathToImage, string $pathToOutput = null)
65
    {
66
        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...
67
            copy($pathToImage, $pathToOutput);
68
69
            $pathToImage = $pathToOutput;
70
        }
71
72
        $image = new Image($pathToImage);
73
74
        $this->logger->info("Start optimizing {$pathToImage}");
75
76
        foreach ($this->optimizers as $optimizer) {
77
            $this->applyOptimizer($optimizer, $image);
78
        }
79
    }
80
81
    protected function applyOptimizer(Optimizer $optimizer, Image $image)
82
    {
83
        if (! $optimizer->canHandle($image)) {
84
            return;
85
        }
86
87
        $optimizerClass = get_class($optimizer);
88
89
        $this->logger->info("Using optimizer: `{$optimizerClass}`");
90
91
        $optimizer->setImagePath($image->path());
92
93
        $command = $optimizer->getCommand();
94
   
95
        $this->logger->info("Executing `{$command}`");
96
97
        if ($this->is_cli()) {
0 ignored issues
show
Bug introduced by
The method is_cli() does not seem to exist on object<Spatie\ImageOptimizer\OptimizerChain>.

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...
98
            $process = new Process($command);
0 ignored issues
show
Documentation introduced by
$command 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...
99
            } else {
100
            $process = Process::fromShellCommandline($command);
101
        }
102
        
103
        $process
104
            ->setTimeout($this->timeout)
105
            ->run();
106
107
        $this->logResult($process);
108
    }
109
     
110
    /*
111
     * Confirms cli execution environment with no server, cron, or cgi context
112
     */
113
    protected function isCli() {
114
115
        return (! isset($_SERVER['SERVER_SOFTWARE']) && 
116
        (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && 
117
        $_SERVER['argc'] > 0)));
118
    }
119
120
    protected function logResult(Process $process)
121
    {
122
        if (! $process->isSuccessful()) {
123
            $this->logger->error("Process errored with `{$process->getErrorOutput()}`");
124
125
            return;
126
        }
127
128
        $this->logger->info("Process successfully ended with output `{$process->getOutput()}`");
129
    }
130
}
131