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 Faton
01:28
created

BaseOptimizer::setImagePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Spatie\ImageOptimizer\Optimizers;
4
5
use Spatie\ImageOptimizer\Optimizer;
6
use Symfony\Component\Process\Process;
7
use Spatie\ImageOptimizer\OptimizerChain;
8
9
abstract class BaseOptimizer implements Optimizer
10
{
11
    /**
12
     * Options.
13
     *
14
     * @var array
15
     */
16
    public $options = [];
17
18
    /**
19
     * Image path.
20
     *
21
     * @var string
22
     */
23
    public $imagePath = '';
24
25
    /**
26
     * Binary path.
27
     *
28
     * @var string
29
     */
30
    protected $binaryPath = '';
31
32
    public function __construct($options = [])
33
    {
34
        $this->setOptions($options);
35
    }
36
37
38
    /**
39
     * Set binary Path.
40
     *
41
     * @param string|array $binaryPath
42
     * @return string
43
     */
44
    public function setBinaryPath($binaryPath)
45
    {
46
        $this->binaryPath = $binaryPath;
0 ignored issues
show
Documentation Bug introduced by
It seems like $binaryPath can also be of type array. However, the property $binaryPath is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
48
        return $this;
49
    }
50
51
    /**
52
     * Get binary path.
53
     *
54
     * @return string|array
55
     */
56
    public function binaryPath()
57
    {
58
        return $this->binaryPath;
59
    }
60
61
    public function binaryName(): string
62
    {
63
        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...
64
    }
65
66
    public function setImagePath(string $imagePath)
67
    {
68
        $this->imagePath = $imagePath;
69
70
        return $this;
71
    }
72
73
    public function setOptions(array $options = [])
74
    {
75
        $this->options = $options;
76
77
        return $this;
78
    }
79
80
81
82
    /**
83
     * Authomatically detect the path where the image optimizes is installed.
84
     *
85
     * @return $this
86
     */
87
    public function checkBinary()
88
    {
89
        // check binary by a given list of binary path
90
        if (is_array($this->binaryPath())) {
91
            foreach ($this->binaryPath() as $path) {
92
                $path = rtrim($path, '/').'/';
93
                $process = new Process('which -a '.$path.''.$this->binaryName());
94
                $process->setTimeout(null);
95
                $process->run();
96
97
                if ($process->isSuccessful()) {
98
                    $this->setBinaryPath($path);
99
                    return $this;
100
                }
101
            }
102
103
            $binaryPath = implode(',', array_values($this->binaryPath()));
104
105
            // if we come so far, it means the binary could not be found
106
            (new OptimizerChain())->getLogger()->error('Binary could not be found in any of the following configured paths: '. $binaryPath .'');
107
108
            // Although a given list of possible binary path has been given, the binary may exists
109
            // in the global environment. Therefore, we will unset binary path list so we can later
110
            // check if it exists the global environment
111
            $this->setBinaryPath('');
112
        }
113
114
        // check if binary exists in the global environment
115
        $process = new Process('which -a '.$this->binaryName());
116
        $process->setTimeout(null);
117
        $process->run();
118
        if ($process->isSuccessful()) {
119
            return $this;
120
        }else{
121
            (new OptimizerChain())->getLogger()->error('Binary could not be found: `'.$this->binaryName().'`');
122
        }
123
124
        return $this;
125
    }
126
127
    public function getCommand(): string
128
    {
129
        $optionString = implode(' ', $this->options);
130
        $fullBinaryPath = $this->checkBinary()->binaryPath().$this->binaryName();
131
132
        return "\"{$fullBinaryPath}\" {$optionString} ".escapeshellarg($this->imagePath);
133
    }
134
}
135