Passed
Push — master ( ff1fbd...266e25 )
by Jonas
08:06
created

DetectsChromeVersion::chromeVersion()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5.0291

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 37
ccs 17
cts 19
cp 0.8947
crap 5.0291
rs 9.3222
1
<?php
2
3
namespace Staudenmeir\DuskUpdater;
4
5
use Symfony\Component\Process\Process;
6
7
trait DetectsChromeVersion
8
{
9
    /**
10
     * The default commands to detect the installed Chrome/Chromium version.
11
     *
12
     * @var array
13
     */
14
    protected $chromeCommands = [
15
        'linux' => [
16
            '/usr/bin/google-chrome --version',
17
            '/usr/bin/chromium-browser --version',
18
            '/usr/bin/google-chrome-stable --version',
19
        ],
20
        'mac' => [
21
            '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
22
        ],
23
        'win' => [
24
            'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version',
25
        ],
26
    ];
27
28
    /**
29
     * Detect the installed Chrome/Chromium version.
30
     *
31
     * @param string $os
32
     * @return int|bool
33
     */
34 3
    protected function chromeVersion($os)
35
    {
36 3
        $path = $this->option('detect');
0 ignored issues
show
Bug introduced by
It seems like option() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        /** @scrutinizer ignore-call */ 
37
        $path = $this->option('detect');
Loading history...
37
38 3
        if ($path) {
39 2
            if ($os === 'win') {
40
                $this->error('Chrome version cannot be detected in custom installation path on Windows.');
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
                $this->/** @scrutinizer ignore-call */ 
41
                       error('Chrome version cannot be detected in custom installation path on Windows.');
Loading history...
41
42
                return false;
43
            }
44
45 2
            $commands = [$path.' --version'];
46
        } else {
47 1
            $commands = $this->chromeCommands[$os];
48
        }
49
50 3
        foreach ($commands as $command) {
51 3
            $process = Process::fromShellCommandline($command);
52
53 3
            $process->run();
54
55 3
            preg_match('/(\d+)(\.\d+){3}/', $process->getOutput(), $matches);
56
57 3
            if (!isset($matches[1])) {
58 1
                continue;
59
            }
60
61 2
            $this->comment(
0 ignored issues
show
Bug introduced by
It seems like comment() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            $this->/** @scrutinizer ignore-call */ 
62
                   comment(
Loading history...
62 2
                sprintf('Chrome version %s detected.', $matches[0])
63
            );
64
65 2
            return (int) $matches[1];
66
        }
67
68 1
        $this->error('Chrome version could not be detected. Please submit an issue: https://github.com/staudenmeir/dusk-updater');
69
70 1
        return false;
71
    }
72
}
73