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 ( b027da...71edf5 )
by Yong
09:39
created

CredentialsProvider::customProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 3
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 Closure;
8
9
/**
10
 * Class CredentialsProvider
11
 *
12
 * @package AlibabaCloud\Client\Credentials\Providers
13
 */
14
class CredentialsProvider
15
{
16
    /**
17
     * @var array
18
     */
19
    private static $hasCustomChain;
20
21
    /**
22
     * @throws ClientException
23
     */
24 4
    public static function chain()
25
    {
26 4
        $providers = func_get_args();
27
28 4
        if (empty($providers)) {
29 1
            throw new ClientException('No providers in chain', \ALIBABA_CLOUD_INVALID_ARGUMENT);
30
        }
31
32 3
        foreach ($providers as $provider) {
33 3
            if (!$provider instanceof Closure) {
34 1
                throw new ClientException('Providers must all be Closures', \ALIBABA_CLOUD_INVALID_ARGUMENT);
35
            }
36 3
        }
37
38 2
        self::$hasCustomChain = $providers;
39 2
    }
40
41 3
    public static function flush()
42
    {
43 3
        self::$hasCustomChain = [];
44 3
    }
45
46
    /**
47
     * @return bool
48
     */
49 5
    public static function hasCustomChain()
50
    {
51 5
        return (bool)self::$hasCustomChain;
52
    }
53
54
    /**
55
     * @param string $clientName
56
     *
57
     * @throws ClientException
58
     */
59 2
    public static function customProvider($clientName)
60
    {
61 2
        foreach (self::$hasCustomChain as $provider) {
62 2
            $provider();
63 2
            if (AlibabaCloud::has($clientName)) {
64 1
                break;
65
            }
66 2
        }
67 2
    }
68
69
    /**
70
     * @param string $clientName
71
     *
72
     * @throws ClientException
73
     */
74 4
    public static function defaultProvider($clientName)
75
    {
76
        $providers = [
77 4
            self::env(),
78 4
            self::ini(),
79 4
            self::instance(),
80 4
        ];
81
82 4
        foreach ($providers as $provider) {
83 4
            $provider();
84 4
            if (AlibabaCloud::has($clientName)) {
85 3
                break;
86
            }
87 4
        }
88 4
    }
89
90
    /**
91
     * @return Closure
92
     */
93 8
    public static function env()
94
    {
95
        return function () {
96 7
            $accessKeyId     = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_ID');
97 6
            $accessKeySecret = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
98
99 5
            if ($accessKeyId && $accessKeySecret) {
100 5
                AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)->asDefaultClient();
0 ignored issues
show
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

100
                AlibabaCloud::accessKeyClient(/** @scrutinizer ignore-type */ $accessKeyId, $accessKeySecret)->asDefaultClient();
Loading history...
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

100
                AlibabaCloud::accessKeyClient($accessKeyId, /** @scrutinizer ignore-type */ $accessKeySecret)->asDefaultClient();
Loading history...
101 5
            }
102 8
        };
103
    }
104
105
    /**
106
     * @return Closure
107
     */
108 10
    public static function ini()
109
    {
110
        return function () {
111 7
            $ini = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_CREDENTIALS_FILE');
112
113 6
            if ($ini) {
114 5
                AlibabaCloud::load($ini);
115 5
            } else {
116 1
                AlibabaCloud::load();
117
            }
118
119 6
            self::compatibleWithGlobal();
120 10
        };
121
    }
122
123
    /**
124
     * @codeCoverageIgnore
125
     *
126
     * Compatible with global
127
     *
128
     * @throws ClientException
129
     */
130
    private static function compatibleWithGlobal()
131
    {
132
        if (AlibabaCloud::has('global') && !AlibabaCloud::has(self::getDefaultName())) {
133
            AlibabaCloud::get('global')->name(self::getDefaultName());
134
        }
135
    }
136
137
    /**
138
     * @return Closure
139
     */
140
    public static function instance()
141
    {
142 6
        return function () {
143 3
            $instance = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ECS_METADATA');
144 3
            if ($instance) {
145 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

145
                AlibabaCloud::ecsRamRoleClient(/** @scrutinizer ignore-type */ $instance)->asDefaultClient();
Loading history...
146 3
            }
147 6
        };
148
    }
149
150
    /**
151
     * @return array|false|string
152
     * @throws ClientException
153
     */
154 219
    public static function getDefaultName()
155
    {
156 219
        $name = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_PROFILE');
157
158 218
        if ($name) {
159 160
            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...
160
        }
161
162 58
        return 'default';
163
    }
164
}
165