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
Pull Request — master (#77)
by Yong
04:48
created

CredentialsProvider::hasCustomChain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Credentials\Providers;
4
5
use AlibabaCloud\Client\AlibabaCloud;
6
use AlibabaCloud\Client\Exception\ClientException;
7
use AlibabaCloud\Client\SDK;
8
use Closure;
9
10
/**
11
 * Class CredentialsProvider
12
 *
13
 * @package AlibabaCloud\Client\Credentials\Providers
14
 */
15
class CredentialsProvider
16
{
17
    /**
18
     * @var array
19
     */
20
    private static $customChains;
21
22
    /**
23
     * @throws ClientException
24
     */
25 4
    public static function chain()
26
    {
27 4
        $providers = func_get_args();
28
29 4
        if (empty($providers)) {
30 1
            throw new ClientException('No providers in chain', SDK::INVALID_ARGUMENT);
31
        }
32
33 3
        foreach ($providers as $provider) {
34 3
            if (!$provider instanceof Closure) {
35 1
                throw new ClientException('Providers must all be Closures', SDK::INVALID_ARGUMENT);
36
            }
37 3
        }
38
39 2
        self::$customChains = $providers;
40 2
    }
41
42
    /**
43
     * Forget the custom providers chain.
44
     */
45 3
    public static function flush()
46
    {
47 3
        self::$customChains = [];
48 3
    }
49
50
    /**
51
     * @return bool
52
     */
53 5
    public static function hasCustomChain()
54
    {
55 5
        return (bool)self::$customChains;
56
    }
57
58
    /**
59
     * @param string $clientName
60
     *
61
     * @throws ClientException
62
     */
63 2
    public static function customProvider($clientName)
64
    {
65 2
        foreach (self::$customChains as $provider) {
66 2
            $provider();
67 2
            if (AlibabaCloud::has($clientName)) {
68 1
                break;
69
            }
70 2
        }
71 2
    }
72
73
    /**
74
     * @param string $clientName
75
     *
76
     * @throws ClientException
77
     */
78 4
    public static function defaultProvider($clientName)
79
    {
80
        $providers = [
81 4
            self::env(),
82 4
            self::ini(),
83 4
            self::instance(),
84 4
        ];
85
86 4
        foreach ($providers as $provider) {
87 4
            $provider();
88 4
            if (AlibabaCloud::has($clientName)) {
89 3
                break;
90
            }
91 4
        }
92 4
    }
93
94
    /**
95
     * @return Closure
96
     */
97 8
    public static function env()
98
    {
99
        return function() {
100 7
            $accessKeyId     = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_ID');
101 6
            $accessKeySecret = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
102
103 5
            if ($accessKeyId && $accessKeySecret) {
104 5
                AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)->asDefaultClient();
0 ignored issues
show
Bug introduced by
It seems like $accessKeySecret can also be of type true; however, parameter $accessKeySecret of AlibabaCloud\Client\Alib...loud::accessKeyClient() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
                AlibabaCloud::accessKeyClient($accessKeyId, /** @scrutinizer ignore-type */ $accessKeySecret)->asDefaultClient();
Loading history...
Bug introduced by
It seems like $accessKeyId can also be of type true; however, parameter $accessKeyId of AlibabaCloud\Client\Alib...loud::accessKeyClient() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
                AlibabaCloud::accessKeyClient(/** @scrutinizer ignore-type */ $accessKeyId, $accessKeySecret)->asDefaultClient();
Loading history...
105 5
            }
106 8
        };
107
    }
108
109
    /**
110
     * @return Closure
111
     */
112 10
    public static function ini()
113
    {
114
        return function() {
115 7
            $ini = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_CREDENTIALS_FILE');
116
117 6
            if ($ini) {
118 5
                AlibabaCloud::load($ini);
119 5
            } else {
120 1
                AlibabaCloud::load();
121
            }
122
123 6
            self::compatibleWithGlobal();
124 10
        };
125
    }
126
127
    /**
128
     * @codeCoverageIgnore
129
     *
130
     * Compatible with global
131
     *
132
     * @throws ClientException
133
     */
134
    private static function compatibleWithGlobal()
135
    {
136
        if (AlibabaCloud::has('global') && !AlibabaCloud::has(self::getDefaultName())) {
137
            AlibabaCloud::get('global')->name(self::getDefaultName());
138
        }
139
    }
140
141
    /**
142
     * @return array|false|string
143
     * @throws ClientException
144
     */
145 199
    public static function getDefaultName()
146
    {
147 199
        $name = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_PROFILE');
148
149 198
        if ($name) {
150 162
            return $name;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $name also could return the type true which is incompatible with the documented return type array|false|string.
Loading history...
151
        }
152
153 36
        return 'default';
154
    }
155
156
    /**
157
     * @return Closure
158
     */
159
    public static function instance()
160
    {
161 6
        return function() {
162 3
            $instance = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ECS_METADATA');
163 3
            if ($instance) {
164 3
                AlibabaCloud::ecsRamRoleClient($instance)->asDefaultClient();
0 ignored issues
show
Bug introduced by
It seems like $instance can also be of type true; however, parameter $roleName of AlibabaCloud\Client\Alib...oud::ecsRamRoleClient() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

164
                AlibabaCloud::ecsRamRoleClient(/** @scrutinizer ignore-type */ $instance)->asDefaultClient();
Loading history...
165 3
            }
166 6
        };
167
    }
168
}
169