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

UserAgent   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 127
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultFields() 0 10 3
A isGuarded() 0 3 1
A append() 0 6 2
A toString() 0 29 3
A clean() 0 10 3
A clear() 0 3 1
A with() 0 3 1
1
<?php
2
3
namespace AlibabaCloud\Client\Request;
4
5
use AlibabaCloud\Client\AlibabaCloud;
6
use GuzzleHttp\Client;
7
8
/**
9
 * Class UserAgent
10
 *
11
 * @package AlibabaCloud\Client\Request
12
 */
13
class UserAgent
14
{
15
16
    /**
17
     * @var array
18
     */
19
    private static $userAgent = [];
20
21
    /**
22
     * @var array
23
     */
24
    private static $guard = [
25
        'client',
26
        'guzzlehttp',
27
        'curl',
28
        'php',
29
    ];
30
31
    /**
32
     * UserAgent constructor.
33
     */
34
    private static function defaultFields()
35 70
    {
36
        if (self::$userAgent === []) {
37 70
            self::$userAgent = [
38
                'Client'     => AlibabaCloud::VERSION,
39 70
                'GuzzleHttp' => Client::VERSION,
40 70
                'curl'       => isset(\curl_version()['version'])
41 70
                    ? \curl_version()['version']
42 70
                    : 'none',
43
                'PHP'        => \PHP_VERSION,
44 70
            ];
45 70
        }
46 70
    }
47
48
    /**
49
     * @param array $append
50 70
     *
51 70
     * @return string
52 70
     */
53
    public static function toString(array $append = [])
54
    {
55
        self::defaultFields();
56
57
        $os         = \PHP_OS;
58
        $os_version = php_uname('r');
59
        $os_mode    = php_uname('m');
60
        $userAgent  = "AlibabaCloud ($os $os_version; $os_mode) ";
61 3
62
        $newUserAgent = [];
63 3
64
        $append = self::clean($append);
65 3
66 2
        $append = \AlibabaCloud\Client\arrayMerge(
67 2
            [
68 3
                self::$userAgent,
69
                $append,
70
            ]
71
        );
72
73 70
        foreach ($append as $key => $value) {
74
            if ($value === null) {
75 70
                $newUserAgent[] = $key;
76 1
                continue;
77 1
            }
78 1
            $newUserAgent[] = "$key/$value";
79 1
        }
80 1
81 1
        return $userAgent . \implode(' ', $newUserAgent);
82 1
    }
83 1
84 1
    /**
85 1
     * @param array $append
86 70
     *
87
     * @return array
88
     */
89
    public static function clean(array $append)
90
    {
91
        foreach ($append as $key => $value) {
92
            if (self::isGuarded($key)) {
93 3
                unset($append[$key]);
94
                continue;
95 3
            }
96
        }
97
98
        return $append;
99
    }
100
101
    /**
102
     * set User Agent of Alibaba Cloud.
103
     *
104
     * @param string $name
105
     * @param string $value
106
     */
107
    public static function append($name, $value)
108
    {
109
        self::defaultFields();
110
111
        if (!self::isGuarded($name)) {
112
            self::$userAgent[$name] = $value;
113
        }
114
    }
115
116
    /**
117
     * @param array $userAgent
118
     */
119
    public static function with(array $userAgent)
120
    {
121
        self::$userAgent = self::clean($userAgent);
122
    }
123
124
    /**
125
     * Clear all of the User Agent.
126
     */
127
    public static function clear()
128
    {
129
        self::$userAgent = [];
130
    }
131
132
    /**
133
     * @param $name
134
     *
135
     * @return bool
136
     */
137
    public static function isGuarded($name)
138
    {
139
        return in_array(strtolower($name), self::$guard, true);
140
    }
141
}
142