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:04
created

IniCredential::inOpenBasedir()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 7
nop 1
dl 0
loc 22
ccs 8
cts 8
cp 1
crap 6
rs 9.2222
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
use Stringy\Stringy;
8
9
/**
10
 * Class IniCredential
11
 *
12
 * @package   AlibabaCloud\Client\Credentials\Ini
13
 */
14
class IniCredential
15
{
16
    use CreateTrait;
17
    use OptionsTrait;
18
19
    /**
20
     * @var array
21
     */
22
    private static $hasLoaded;
23
24
    /**
25
     * @var string
26
     */
27
    protected $filename;
28
29
    /**
30
     * IniCredential constructor.
31
     *
32
     * @param string $filename
33 68
     */
34
    public function __construct($filename = '')
35 68
    {
36 68
        $this->filename = $filename ?: $this->getDefaultFile();
37
    }
38
39
    /**
40
     * Get the default credential file.
41
     *
42
     * @return string
43 39
     */
44
    public function getDefaultFile()
45 39
    {
46
        return self::getHomeDirectory() . '/.alibabacloud/credentials';
47
    }
48
49
    /**
50
     * Get the credential file.
51
     *
52
     * @return string
53 39
     */
54
    public function getFilename()
55 39
    {
56
        return $this->filename;
57
    }
58
59
    /**
60
     * Gets the environment's HOME directory.
61
     *
62
     * @return null|string
63 40
     */
64
    private static function getHomeDirectory()
65 40
    {
66 39
        if (getenv('HOME')) {
67
            return getenv('HOME');
68
        }
69 1
70 1
        return (getenv('HOMEDRIVE') && getenv('HOMEPATH'))
71 1
            ? getenv('HOMEDRIVE') . getenv('HOMEPATH')
72
            : null;
73
    }
74
75
    /**
76
     * @param array  $array
77
     * @param string $key
78
     *
79
     * @return bool
80 16
     */
81
    protected static function isNotEmpty(array $array, $key)
82 16
    {
83
        return isset($array[$key]) && !empty($array[$key]);
84
    }
85
86
    /**
87
     * @param string $key
88
     * @param string $clientName
89
     *
90
     * @throws ClientException
91 28
     */
92
    public function missingRequired($key, $clientName)
93 28
    {
94 28
        throw new ClientException(
95
            "Missing required '$key' option for '$clientName' in " . $this->getFilename(),
96 28
            \ALIBABA_CLOUD_INVALID_CREDENTIAL
97
        );
98
    }
99
100
    /**
101
     * Clear credential cache.
102
     *
103
     * @return void
104 24
     */
105
    public static function forgetLoadedCredentialsFile()
106 24
    {
107 24
        self::$hasLoaded = [];
108
    }
109
110
    /**
111
     * @return array|mixed
112
     * @throws ClientException
113 28
     */
114
    public function load()
115
    {
116 28
        // If it has been loaded, assign the client directly.
117
        if (isset(self::$hasLoaded[$this->filename])) {
118
            /**
119
             * @var $client Client
120 3
             */
121 3
            foreach (self::$hasLoaded[$this->filename] as $projectName => $client) {
122 3
                $client->name($projectName);
123 3
            }
124
125 27
            return self::$hasLoaded[$this->filename];
126
        }
127
128
        return $this->loadFile();
129
    }
130
131
    /**
132
     * Exceptions will be thrown if the file is unreadable and not the default file.
133
     *
134 29
     * @return array|mixed
135
     * @throws ClientException
136 29
     */
137 6
    private function loadFile()
138
    {
139
        if (!self::inOpenBasedir($this->filename)) {
140
            return [];
141
        }
142 4
143 4
        if (!\is_readable($this->filename) || !\is_file($this->filename)) {
144
            if ($this->filename === $this->getDefaultFile()) {
145 4
                // @codeCoverageIgnoreStart
146
                return [];
147
                // @codeCoverageIgnoreEnd
148 23
            }
149
            throw new ClientException(
150
                'Credential file is not readable: ' . $this->getFilename(),
151
                \ALIBABA_CLOUD_INVALID_CREDENTIAL
152
            );
153
        }
154
155
        return $this->parseFile();
156
    }
157 27
158
    /**
159
     * @param $filename
160 27
     *
161 26
     * @return bool
162 23
     */
163
    public static function inOpenBasedir($filename)
164 3
    {
165 3
        $dir = ini_get('open_basedir');
166
        if (!$dir) {
167 3
            return true;
168 17
        }
169 17
170 17
        $dirs = explode(':', $dir);
171 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...
172
            return true;
173 17
        }
174
175
        foreach ($dirs as $dir) {
176
            if (!Stringy::create($dir)->endsWith(DIRECTORY_SEPARATOR)) {
177
                $dir .= DIRECTORY_SEPARATOR;
178
            }
179
            if (false !== strpos($filename, $dir)) {
180
                return true;
181
            }
182
        }
183
184
        return false;
185 23
    }
186
187 23
    /**
188 23
     * Decode the ini file into an array.
189 23
     *
190 10
     * @return array|mixed
191 9
     * @throws ClientException
192 9
     */
193 9
    private function parseFile()
194 9
    {
195 9
        try {
196 10
            $file = \parse_ini_file($this->filename, true);
197
            if (\is_array($file) && $file !== []) {
198 10
                return $this->initClients($file);
199 10
            }
200 10
            throw new ClientException(
201
                'Format error: ' . $this->getFilename(),
202
                \ALIBABA_CLOUD_INVALID_CREDENTIAL
203
            );
204
        } catch (\Exception $e) {
205
            throw new ClientException(
206
                $e->getMessage(),
207
                \ALIBABA_CLOUD_INVALID_CREDENTIAL,
208
                $e
209
            );
210
        }
211
    }
212
213
    /**
214
     * Initialize clients.
215
     *
216
     * @param array $array
217
     *
218
     * @return array|mixed
219
     * @throws ClientException
220
     */
221
    private function initClients($array)
222
    {
223
        foreach (\array_change_key_case($array) as $clientName => $configures) {
224
            $configures     = \array_change_key_case($configures);
225
            $clientInstance = $this->createClient($clientName, $configures);
226
            if ($clientInstance instanceof Client) {
227
                self::$hasLoaded[$this->filename][$clientName] = $clientInstance;
228
                self::setClientAttributes($configures, $clientInstance);
229
                self::setCert($configures, $clientInstance);
230
                self::setProxy($configures, $clientInstance);
231
            }
232
        }
233
234
        return isset(self::$hasLoaded[$this->filename])
235
            ? self::$hasLoaded[$this->filename]
236
            : [];
237
    }
238
}
239