GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Pull Request — master (#59)
by Yong
04:20
created

IniCredential::inOpenBasedir()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 1
dl 0
loc 19
ccs 9
cts 9
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Credentials\Ini;
4
5
use AlibabaCloud\Client\Clients\Client;
6
use AlibabaCloud\Client\Exception\ClientException;
7
8
/**
9
 * Class IniCredential
10
 *
11
 * @package   AlibabaCloud\Client\Credentials\Ini
12
 */
13
class IniCredential
14
{
15
    use CreateTrait;
16
    use OptionsTrait;
17
18
    /**
19
     * @var array
20
     */
21
    private static $hasLoaded;
22
23
    /**
24
     * @var string
25
     */
26
    protected $filename;
27
28
    /**
29
     * IniCredential constructor.
30
     *
31
     * @param string $filename
32
     */
33 68
    public function __construct($filename = '')
34
    {
35 68
        $this->filename = $filename ?: $this->getDefaultFile();
36 68
    }
37
38
    /**
39
     * Get the default credential file.
40
     *
41
     * @return string
42
     */
43 39
    public function getDefaultFile()
44
    {
45 39
        return self::getHomeDirectory() . '/.alibabacloud/credentials';
46
    }
47
48
    /**
49
     * Get the credential file.
50
     *
51
     * @return string
52
     */
53 39
    public function getFilename()
54
    {
55 39
        return $this->filename;
56
    }
57
58
    /**
59
     * Gets the environment's HOME directory.
60
     *
61
     * @return null|string
62
     */
63 40
    private static function getHomeDirectory()
64
    {
65 40
        if (getenv('HOME')) {
66 39
            return getenv('HOME');
67
        }
68
69 1
        return (getenv('HOMEDRIVE') && getenv('HOMEPATH'))
70 1
            ? getenv('HOMEDRIVE') . getenv('HOMEPATH')
71 1
            : null;
72
    }
73
74
    /**
75
     * @param array  $array
76
     * @param string $key
77
     *
78
     * @return bool
79
     */
80 16
    protected static function isNotEmpty(array $array, $key)
81
    {
82 16
        return isset($array[$key]) && !empty($array[$key]);
83
    }
84
85
    /**
86
     * @param string $key
87
     * @param string $clientName
88
     *
89
     * @throws ClientException
90
     */
91 28
    public function missingRequired($key, $clientName)
92
    {
93 28
        throw new ClientException(
94 28
            "Missing required '$key' option for '$clientName' in " . $this->getFilename(),
95
            \ALIBABA_CLOUD_INVALID_CREDENTIAL
96 28
        );
97
    }
98
99
    /**
100
     * Clear credential cache.
101
     *
102
     * @return void
103
     */
104 24
    public static function forgetLoadedCredentialsFile()
105
    {
106 24
        self::$hasLoaded = [];
107 24
    }
108
109
    /**
110
     * @return array|mixed
111
     * @throws ClientException
112
     */
113 28
    public function load()
114
    {
115
        // If it has been loaded, assign the client directly.
116 28
        if (isset(self::$hasLoaded[$this->filename])) {
117
            /**
118
             * @var $client Client
119
             */
120 3
            foreach (self::$hasLoaded[$this->filename] as $projectName => $client) {
121 3
                $client->name($projectName);
122 3
            }
123 3
124
            return self::$hasLoaded[$this->filename];
125 27
        }
126
127
        return $this->loadFile();
128
    }
129
130
    /**
131
     * Exceptions will be thrown if the file is unreadable and not the default file.
132
     *
133
     * @return array|mixed
134 29
     * @throws ClientException
135
     */
136 29
    private function loadFile()
137 6
    {
138
        if (!self::inOpenBasedir($this->filename)) {
139
            return [];
140
        }
141
142 4
        if (!\is_readable($this->filename) || !\is_file($this->filename)) {
143 4
            if ($this->filename === $this->getDefaultFile()) {
144
                // @codeCoverageIgnoreStart
145 4
                return [];
146
                // @codeCoverageIgnoreEnd
147
            }
148 23
            throw new ClientException(
149
                'Credential file is not readable: ' . $this->getFilename(),
150
                \ALIBABA_CLOUD_INVALID_CREDENTIAL
151
            );
152
        }
153
154
        return $this->parseFile();
155
    }
156
157 27
    /**
158
     * @param $filename
159
     *
160 27
     * @return bool
161 26
     */
162 23
    public static function inOpenBasedir($filename)
163
    {
164 3
        $dir = ini_get('open_basedir');
165 3
        if (!$dir) {
166
            return true;
167 3
        }
168 17
169 17
        $dirs = explode(':', $dir);
170 17
        if (!$dirs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dirs of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
171 17
            return true;
172
        }
173 17
174
        foreach ($dirs as $dir) {
175
            if (false !== strpos($filename, $dir)) {
176
                return true;
177
            }
178
        }
179
180
        return false;
181
    }
182
183
    /**
184
     * Decode the ini file into an array.
185 23
     *
186
     * @return array|mixed
187 23
     * @throws ClientException
188 23
     */
189 23
    private function parseFile()
190 10
    {
191 9
        try {
192 9
            $file = \parse_ini_file($this->filename, true);
193 9
            if (\is_array($file) && $file !== []) {
194 9
                return $this->initClients($file);
195 9
            }
196 10
            throw new ClientException(
197
                'Format error: ' . $this->getFilename(),
198 10
                \ALIBABA_CLOUD_INVALID_CREDENTIAL
199 10
            );
200 10
        } catch (\Exception $e) {
201
            throw new ClientException(
202
                $e->getMessage(),
203
                \ALIBABA_CLOUD_INVALID_CREDENTIAL,
204
                $e
205
            );
206
        }
207
    }
208
209
    /**
210
     * Initialize clients.
211
     *
212
     * @param array $array
213
     *
214
     * @return array|mixed
215
     * @throws ClientException
216
     */
217
    private function initClients($array)
218
    {
219
        foreach (\array_change_key_case($array) as $clientName => $configures) {
220
            $configures     = \array_change_key_case($configures);
221
            $clientInstance = $this->createClient($clientName, $configures);
222
            if ($clientInstance instanceof Client) {
223
                self::$hasLoaded[$this->filename][$clientName] = $clientInstance;
224
                self::setClientAttributes($configures, $clientInstance);
225
                self::setCert($configures, $clientInstance);
226
                self::setProxy($configures, $clientInstance);
227
            }
228
        }
229
230
        return isset(self::$hasLoaded[$this->filename])
231
            ? self::$hasLoaded[$this->filename]
232
            : [];
233
    }
234
}
235