Completed
Pull Request — develop (#1033)
by Rafael Corrêa
02:49
created

OperatingSystem::isBashCompatibleShell()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * this file is part of magerun
4
 *
5
 * @author Tom Klingenberg <https://github.com/ktomk>
6
 */
7
8
namespace N98\Util;
9
10
/**
11
 * Class OperatingSystem
12
 *
13
 * @package N98\Util
14
 */
15
class OperatingSystem
16
{
17
    /**
18
     * @var int
19
     */
20
    const UID_ROOT = 0;
21
22
    /**
23
     * Returns true if operating system is
24
     * based on GNU linux.
25
     *
26
     * @return boolean
27
     */
28
    public static function isLinux()
29
    {
30
        return (bool) stristr(PHP_OS, 'linux');
31
    }
32
33
    /**
34
     * Returns true if operating system is
35
     * based on Microsoft Windows.
36
     *
37
     * @return boolean
38
     */
39
    public static function isWindows()
40
    {
41
        return strtolower(substr(PHP_OS, 0, 3)) === 'win';
42
    }
43
44
    /**
45
     * Returns true if operating system is
46
     * based on novell netware.
47
     *
48
     * @return boolean
49
     */
50
    public static function isNetware()
51
    {
52
        return (bool) stristr(PHP_OS, 'netware');
53
    }
54
55
    /**
56
     * Returns true if operating system is
57
     * based on apple MacOS.
58
     *
59
     * @return boolean
60
     */
61
    public static function isMacOs()
62
    {
63
        return stristr(PHP_OS, 'darwin') || stristr(PHP_OS, 'mac');
64
    }
65
66
    /**
67
     * @param string $program
68
     * @return bool
69
     */
70
    public static function isProgramInstalled($program)
71
    {
72
        if (self::isWindows()) {
73
            return WindowsSystem::isProgramInstalled($program);
74
        }
75
76
        $out = null;
77
        $return = null;
78
        @exec('which ' . $program, $out, $return);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
79
80
        return $return === 0;
81
    }
82
83
    /**
84
     * Home directory of the current user
85
     *
86
     * @return string|false false in case there is no environment variable related to the home directory
87
     */
88
    public static function getHomeDir()
89
    {
90
        if (self::isWindows()) {
91
            return getenv('USERPROFILE');
92
        }
93
94
        return getenv('HOME');
95
    }
96
97
    /**
98
     * Test for Root UID on a POSIX system if posix_getuid() is available.
99
     *
100
     * Returns false negatives if posix_getuid() is not available.
101
     *
102
     * @return bool
103
     */
104
    public static function isRoot()
105
    {
106
        return function_exists('posix_getuid') && posix_getuid() === self::UID_ROOT;
107
    }
108
109
    /**
110
     * get current working directory
111
     *
112
     * @return string the current working directory on success, or false on failure.
113
     */
114
    public static function getCwd()
115
    {
116
        return getcwd();
117
    }
118
119
    /**
120
     * Retrieve path to php binary
121
     *
122
     * @return string
123
     */
124
    public static function getPhpBinary()
125
    {
126
        // PHP_BINARY (>= php 5.4)
127
        if (defined('PHP_BINARY')) {
128
            return PHP_BINARY;
129
        }
130
131
        if (self::isWindows()) {
132
            return 'php';
133
        }
134
135
        return '/usr/bin/env php';
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public static function isBashCompatibleShell()
142
    {
143
        return in_array(
144
            basename(getenv('SHELL')),
145
            ['bash', 'zsh']
146
        );
147
    }
148
}
149