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 (#57)
by
unknown
06:41
created

BaseOptimizer::detectBinaryPath()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 7
nop 0
1
<?php
2
3
namespace Spatie\ImageOptimizer\Optimizers;
4
5
use Psr\Log\LoggerInterface;
6
use Spatie\ImageOptimizer\DummyLogger;
7
use Spatie\ImageOptimizer\Optimizer;
8
use Symfony\Component\Process\Process;
9
10
abstract class BaseOptimizer implements Optimizer
11
{
12
    public $options = [];
13
14
    public $imagePath = '';
15
16
    /**
17
     * List of binary paths to check for commands
18
     * @var array $binaryPathList
19
     */
20
    protected $binaryPathList = [
21
      '/usr/local',
22
      '/usr/local/bin',
23
      '/usr/bin',
24
      '/usr/sbin',
25
      '/usr/local/bin',
26
      '/usr/local/sbin',
27
      '/bin',
28
      '/sbin'
29
    ];
30
31
    /**
32
     * Binary path
33
     * @var string $binaryPath
34
     */
35
    protected $binaryPath = '';
36
37
    public function __construct($options = [])
38
    {
39
        $this->useLogger(new DummyLogger());
40
        $this->setOptions($options);
41
    }
42
43
44
    /**
45
     * Set binary Path
46
     *
47
     * Useful in case your commands are not accessible by global environment. ex. /usr/bin/local
48
     *
49
     * @param string $binaryName
0 ignored issues
show
Bug introduced by
There is no parameter named $binaryName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
50
     * @return string
51
     */
52
    public function setBinaryPath(string $binaryPath)
53
    {
54
        $this->binaryPath = $binaryPath;
55
56
        return $this;
57
    }
58
59
    /**
60
     * Get binary path
61
     *
62
     * @return string
63
     */
64
    public function binaryPath(): string
65
    {
66
        return $this->binaryPath;
67
    }
68
69
70
    public function binaryName(): string
71
    {
72
        return $this->binaryName;
0 ignored issues
show
Bug introduced by
The property binaryName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
73
    }
74
75
    public function setImagePath(string $imagePath)
76
    {
77
        $this->imagePath = $imagePath;
78
79
        return $this;
80
    }
81
82
    public function setOptions(array $options = [])
83
    {
84
        $this->options = $options;
85
86
        return $this;
87
    }
88
89
    public function useLogger(LoggerInterface $log)
90
    {
91
        $this->logger = $log;
0 ignored issues
show
Bug introduced by
The property logger does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
92
93
        return $this;
94
    }
95
96
97
    /**
98
     * Get Binary path list
99
     *
100
     * @return array
101
     */
102
    public function binaryPathList(){
103
        return $this->binaryPathList;
104
    }
105
106
    /**
107
     * Authomatically detect the path where the image optimizes is installed
108
     *
109
     * @return $this
110
     */
111
    public function detectBinaryPath(){
112
        // first check if comman is executed in a global environment
113
        $process = new Process("which -a " .$this->binaryName());
114
        $process->setTimeout(null);
115
        $process->run();
116
        if ($process->isSuccessful()) {
117
            return $this;
118
        }
119
120
        // add custom path (if given in config.php)
121
        if($this->binaryPath()) {
122
            $this->binaryPathList = [
123
              $this->binaryPath()
124
            ];
125
        }
126
127
        // check if command is found in every given path
128
        foreach ($this->binaryPathList() as $path) {
129
            $path = rtrim($path, '/') . '/';
130
            $process = new Process("which -a " . $path . '' . $this->binaryName());
131
            $process->setTimeout(null);
132
            $process->run();
133
            if ($process->isSuccessful()) {
134
                $this->setBinaryPath($path);
135
                return $this;
136
            }
137
        }
138
139
        $this->logger->error("Command could not be executed in any of the following binary path: `".implode(",", array_values($this->binaryPathList))."`}");
140
141
        return $this;
142
    }
143
144
    public function getCommand(): string
145
    {
146
        $optionString = implode(' ', $this->options);
147
        $fullBinaryPath = $this->detectBinaryPath()->binaryPath().$this->binaryName();
148
149
        return "\"{$fullBinaryPath}\" {$optionString} ".escapeshellarg($this->imagePath);
150
    }
151
}
152