Passed
Pull Request — master (#19)
by Jonathan
08:43
created

DetectsChromeVersion::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 static $platforms = [
15
        'linux' => [
16
            'slug' => 'linux64',
17
            'commands' => [
18
                '/usr/bin/google-chrome --version',
19
                '/usr/bin/chromium-browser --version',
20
                '/usr/bin/chromium --version',
21
                '/usr/bin/google-chrome-stable --version',
22
            ],
23
        ],
24
        'mac' => [
25
            'slug' => 'mac-x64',
26
            'commands' => [
27
                '/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version',
28
                '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
29
            ],
30
        ],
31
        'mac-intel' => [
32
            'slug' => 'mac-x64',
33
            'commands' => [
34 3
                '/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version',
35
                '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
36 3
            ],
37
        ],
38 3
        'mac-arm' => [
39 2
            'slug' => 'mac-arm64',
40
            'commands' => [
41
                '/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version',
42
                '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
43
            ],
44
        ],
45 2
        'win' => [
46
            'slug' => 'win32',
47 1
            'commands' => [
48
                'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version',
49
            ],
50 3
        ],
51 3
    ];
52
53 3
    /**
54
     * Detect the installed Chrome/Chromium version.
55 3
     *
56
     * @param string $os
57 3
     * @return int|bool
58 1
     */
59
    protected function chromeVersion($os)
60
    {
61 2
        $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

61
        /** @scrutinizer ignore-call */ 
62
        $path = $this->option('detect');
Loading history...
62 2
63
        if ($path) {
64
            if ($os === 'win') {
65 2
                $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

65
                $this->/** @scrutinizer ignore-call */ 
66
                       error('Chrome version cannot be detected in custom installation path on Windows.');
Loading history...
66
67
                return false;
68 1
            }
69
70 1
            $commands = [$path.' --version'];
71
        } else {
72
            $commands = static::$platforms[$os]['commands'];
73
        }
74
75
        foreach ($commands as $command) {
76
            $process = Process::fromShellCommandline($command);
77
78
            $process->run();
79
80
            preg_match('/(\d+)(\.\d+){3}/', $process->getOutput(), $matches);
81
82
            if (!isset($matches[1])) {
83
                continue;
84
            }
85
86
            $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

86
            $this->/** @scrutinizer ignore-call */ 
87
                   comment(
Loading history...
87
                sprintf('Chrome version %s detected.', $matches[0])
88
            );
89
90
            return (int) $matches[1];
91
        }
92
93
        $this->error('Chrome version could not be detected. Please submit an issue: https://github.com/staudenmeir/dusk-updater');
94
95
        return false;
96
    }
97
98
    /**
99
     * Resolve the ChromeDriver slug for the given operating system.
100
     *
101
     * @param string $operatingSystem
102
     * @param string|null $version
103
     * @return string
104
     */
105
    public static function chromeDriverSlug($operatingSystem, $version = null)
106
    {
107
        $slug = static::$platforms[$operatingSystem]['slug'] ?? null;
108
109
        if (!is_null($version) && version_compare($version, '115.0', '<')) {
110
            if ($slug === 'mac-arm64') {
111
                return version_compare($version, '106.0.5249', '<') ? 'mac64_m1' : 'mac_arm64';
112
            } elseif ($slug === 'mac-x64') {
113
                return 'mac64';
114
            }
115
        }
116
117
        return $slug;
118
    }
119
120
    /**
121
     * Get all supported operating systems.
122
     *
123
     * @return array
124
     */
125
    public static function all()
126
    {
127
        return array_keys(static::$platforms);
128
    }
129
130
    /**
131
     * Get the current operating system identifier.
132
     *
133
     * @return string
134
     */
135
    public static function os()
136
    {
137
        if (static::onWindows()) {
138
            return 'win';
139
        } elseif (static::onMac()) {
140
            return static::macArchitectureId();
141
        }
142
143
        return 'linux';
144
    }
145
146
    /**
147
     * Determine if the operating system is Windows or Windows Subsystem for Linux.
148
     *
149
     * @return bool
150
     */
151
    public static function onWindows()
152
    {
153
        return PHP_OS === 'WINNT' || Str::contains(php_uname(), 'Microsoft');
0 ignored issues
show
Bug introduced by
The type Staudenmeir\DuskUpdater\Str was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
154
    }
155
156
    /**
157
     * Determine if the operating system is macOS.
158
     *
159
     * @return bool
160
     */
161
    public static function onMac()
162
    {
163
        return PHP_OS === 'Darwin';
164
    }
165
166
    /**
167
     * Get the current macOS platform architecture.
168
     *
169
     * @return string
170
     */
171
    public static function macArchitectureId()
172
    {
173
        switch (php_uname('m')) {
174
            case 'arm64':
175
                return 'mac-arm';
176
            case 'x86_64':
177
                return 'mac-intel';
178
            default:
179
                return 'mac';
180
        }
181
    }
182
}
183