Platform::getOS()   B
last analyzed

Complexity

Conditions 11
Paths 11

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 16
nc 11
nop 0
dl 0
loc 18
ccs 0
cts 15
cp 0
crap 132
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cecil\Util;
15
16
/**
17
 * Platform utility class.
18
 *
19
 * This class provides methods to detect the platform (OS) on which the application is running,
20
 * check if it is running from a Phar archive, and open URLs in the system's default browser.
21
 */
22
class Platform
23
{
24
    public const OS_UNKNOWN = 1;
25
    public const OS_WIN = 2;
26
    public const OS_LINUX = 3;
27
    public const OS_OSX = 4;
28
29
    /** @var string */
30
    protected static $pharPath;
31
32
    /**
33
     * Running from Phar or not?
34
     */
35 1
    public static function isPhar(): bool
36
    {
37 1
        if (!empty(\Phar::running())) {
38
            self::$pharPath = \Phar::running();
39
40
            return true;
41
        }
42
43 1
        return false;
44
    }
45
46
    /**
47
     * Returns the full path on disk to the currently executing Phar archive.
48
     */
49
    public static function getPharPath(): string
50
    {
51
        if (!isset(self::$pharPath)) {
52
            if (!self::isPhar()) {
53
                throw new \Exception('Can\'t get Phar path.');
54
            }
55
        }
56
57
        return self::$pharPath;
58
    }
59
60
    /**
61
     * Whether the host machine is running a Windows OS.
62
     */
63
    public static function isWindows(): bool
64
    {
65
        return \defined('PHP_WINDOWS_VERSION_BUILD');
66
    }
67
68
    /**
69
     * Opens a URL in the system default browser.
70
     */
71
    public static function openBrowser(string $url): void
72
    {
73
        if (self::isWindows()) {
74
            passthru('start "web" explorer "' . $url . '"');
75
76
            return;
77
        }
78
        passthru('which xdg-open', $linux);
79
        passthru('which open', $osx);
80
        if (0 === $linux) {
81
            passthru('xdg-open ' . $url);
82
        } elseif (0 === $osx) {
83
            passthru('open ' . $url);
84
        }
85
    }
86
87
    /**
88
     * Search for system OS in PHP_OS constant.
89
     */
90
    public static function getOS(): int
91
    {
92
        switch (PHP_OS) {
93
            case 'Unix':
94
            case 'FreeBSD':
95
            case 'NetBSD':
96
            case 'OpenBSD':
97
            case 'Linux':
98
                return self::OS_LINUX;
99
            case 'WINNT':
100
            case 'WIN32':
101
            case 'Windows':
102
            case 'CYGWIN_NT':
103
                return self::OS_WIN;
104
            case 'Darwin':
105
                return self::OS_OSX;
106
            default:
107
                return self::OS_UNKNOWN;
108
        }
109
    }
110
}
111