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 (#72)
by Yong
05:27
created

CredentialsProvider::compatibleWithGlobal()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

99
                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

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

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