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 ( 9a1478...d878dd )
by
unknown
05:23
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
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 65
    public function __construct($filename = '')
34
    {
35 65
        $this->filename = $filename ?: $this->getDefaultFile();
36 65
    }
37
38
    /**
39
     * Get the default credential file.
40
     *
41
     * @return string
42
     */
43 37
    public function getDefaultFile()
44
    {
45 37
        return self::getHomeDirectory() . '/.alibabacloud/credentials';
46
    }
47
48
    /**
49
     * Get the credential file.
50
     *
51
     * @return string
52
     */
53 36
    public function getFilename()
54
    {
55 36
        return $this->filename;
56
    }
57
58
    /**
59
     * Gets the environment's HOME directory.
60
     *
61
     * @return null|string
62
     */
63 38
    private static function getHomeDirectory()
64
    {
65 38
        if (getenv('HOME')) {
66 37
            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 27
    public function load()
114
    {
115
        // If it has been loaded, assign the client directly.
116 27
        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
124 3
            return self::$hasLoaded[$this->filename];
125
        }
126
127 26
        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
     * @throws ClientException
135
     */
136 28
    private function loadFile()
137
    {
138 28
        if (!\AlibabaCloud\Client\inOpenBasedir($this->filename)) {
139 2
            return [];
140
        }
141
142 26
        if (!\is_readable($this->filename) || !\is_file($this->filename)) {
143 3
            if ($this->filename === $this->getDefaultFile()) {
144
                // @codeCoverageIgnoreStart
145
                return [];
146
                // @codeCoverageIgnoreEnd
147
            }
148 2
            throw new ClientException(
149 2
                'Credential file is not readable: ' . $this->getFilename(),
150
                \ALIBABA_CLOUD_INVALID_CREDENTIAL
151 2
            );
152
        }
153
154 23
        return $this->parseFile();
155
    }
156
157
    /**
158
     * Decode the ini file into an array.
159
     *
160
     * @return array|mixed
161
     * @throws ClientException
162
     */
163 25
    private function parseFile()
164
    {
165
        try {
166 25
            $file = \parse_ini_file($this->filename, true);
167 25
            if (\is_array($file) && $file !== []) {
168 23
                return $this->initClients($file);
169
            }
170 2
            throw new ClientException(
171 2
                'Format error: ' . $this->getFilename(),
172
                \ALIBABA_CLOUD_INVALID_CREDENTIAL
173 2
            );
174 15
        } catch (\Exception $e) {
175 15
            throw new ClientException(
176 15
                $e->getMessage(),
177 15
                \ALIBABA_CLOUD_INVALID_CREDENTIAL,
178
                $e
179 15
            );
180
        }
181
    }
182
183
    /**
184
     * Initialize clients.
185
     *
186
     * @param array $array
187
     *
188
     * @return array|mixed
189
     * @throws ClientException
190
     */
191 23
    private function initClients($array)
192
    {
193 23
        foreach (\array_change_key_case($array) as $clientName => $configures) {
194 23
            $configures     = \array_change_key_case($configures);
195 23
            $clientInstance = $this->createClient($clientName, $configures);
196 10
            if ($clientInstance instanceof Client) {
197 9
                self::$hasLoaded[$this->filename][$clientName] = $clientInstance;
198 9
                self::setClientAttributes($configures, $clientInstance);
199 9
                self::setCert($configures, $clientInstance);
200 9
                self::setProxy($configures, $clientInstance);
201 9
            }
202 10
        }
203
204 10
        return isset(self::$hasLoaded[$this->filename])
205 10
            ? self::$hasLoaded[$this->filename]
206 10
            : [];
207
    }
208
}
209