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.
Passed
Push — master ( f5120a...b463c8 )
by Yong
04:02
created

IniCredential::inOpenBasedir()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 20.1953

Importance

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