VisualDiff::buildSaveCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\VisualDiff;
4
5
use Symfony\Component\Process\Process;
6
7
class VisualDiff
8
{
9
    protected $nodeBinary = null;
10
11
    protected $npmBinary = null;
12
13
    protected $includePath = '$PATH:/usr/local/bin';
14
15
    protected $binPath = null;
16
17
    protected $nodeModulePath = null;
18
19
    protected $newImage;
20
21
    protected $comparisonImage;
22
23
    protected $threshold = 0.1;
24
25
    protected $antialias = false;
26
27
28
    public function setNodeBinary(string $nodeBinary)
29
    {
30
        $this->nodeBinary = $nodeBinary;
31
32
        return $this;
33
    }
34
35
    public function setNpmBinary(string $npmBinary)
36
    {
37
        $this->npmBinary = $npmBinary;
38
39
        return $this;
40
    }
41
42
    public function setIncludePath(string $includePath)
43
    {
44
        $this->includePath = $includePath;
45
46
        return $this;
47
    }
48
49
    public function setBinPath(string $binPath)
50
    {
51
        $this->binPath = $binPath;
52
53
        return $this;
54
    }
55
56
    public function setNodeModulePath(string $nodeModulePath)
57
    {
58
        $this->nodeModulePath = $nodeModulePath;
59
60
        return $this;
61
    }
62
63
    public function setAntialias(bool $antialias)
64
    {
65
        $this->antialias = $antialias;
66
67
        return $this;
68
    }
69
70
    public function setThreshold(float $threshold)
71
    {
72
        $this->threshold = $threshold;
73
74
        return $this;
75
    }
76
77
    protected function getNodePathCommand(string $nodeBinary): string
78
    {
79
        if ($this->nodeModulePath) {
80
            return "NODE_PATH='{$this->nodeModulePath}'";
81
        }
82
        if ($this->npmBinary) {
83
            return "NODE_PATH=`{$nodeBinary} {$this->npmBinary} root -g`";
84
        }
85
        return 'NODE_PATH=`npm root -g`';
86
    }
87
88
    public function __construct(string $newImage, string $comparisonImage)
89
    {
90
        $this->newImage = $newImage;
91
        $this->comparisonImage = $comparisonImage;
92
    }
93
94
    public static function diff(string $newImage, string $comparisonImage)
95
    {
96
        return new static($newImage, $comparisonImage);
97
    }
98
99
    public function buildSaveCommand($filename): array
100
    {
101
        return [
102
            'image_1' => $this->newImage,
103
            'image_2' => $this->comparisonImage,
104
            'output' => $filename,
105
            'threshold' => $this->threshold,
106
            'antialias' => $this->antialias,
107
        ];
108
    }
109
110
    public function save($filename)
111
    {
112
        $output = $this->callDiff($this->buildSaveCommand($filename));
113
114
        return json_decode($output);
115
    }
116
117
    protected function callDiff(array $command)
118
    {
119
        $setIncludePathCommand = "PATH={$this->includePath}";
120
121
        $nodeBinary = $this->nodeBinary ?: 'node';
122
123
        $setNodePathCommand = $this->getNodePathCommand($nodeBinary);
124
125
        $binPath = $this->binPath ?: __DIR__ . '/../bin/diff.js';
126
127
        $fullCommand =
128
            $setIncludePathCommand . ' '
129
            . $setNodePathCommand . ' '
130
            . $nodeBinary . ' '
131
            . escapeshellarg($binPath) . ' '
132
            . escapeshellarg(json_encode($command));
133
134
        $process = new Process($fullCommand);
0 ignored issues
show
Documentation introduced by
$fullCommand 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...
135
        $process->run();
136
137
        if ($process->isSuccessful()) {
138
            return rtrim($process->getOutput());
139
        }
140
    }
141
}