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

UserAgent::toString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3.0105

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 1
dl 0
loc 29
ccs 17
cts 19
cp 0.8947
crap 3.0105
rs 9.7333
c 0
b 0
f 0
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 76
    private static function defaultFields()
35
    {
36 76
        if (self::$userAgent === []) {
37 10
            self::$userAgent = [
38 10
                'Client'     => AlibabaCloud::VERSION,
39 10
                'GuzzleHttp' => Client::VERSION,
40 10
                'curl'       => isset(\curl_version()['version'])
41 10
                    ? \curl_version()['version']
42 10
                    : 'none',
43 10
                'PHP'        => \PHP_VERSION,
44 10
            ];
45 10
        }
46 76
    }
47
48
    /**
49
     * @param array $append
50
     *
51
     * @return string
52
     */
53 76
    public static function toString(array $append = [])
54
    {
55 76
        self::defaultFields();
56
57 76
        $os         = \PHP_OS;
58 76
        $os_version = php_uname('r');
59 76
        $os_mode    = php_uname('m');
60 76
        $userAgent  = "AlibabaCloud ($os $os_version; $os_mode) ";
61
62 76
        $newUserAgent = [];
63
64 76
        $append = self::clean($append);
65
66 76
        $append = \AlibabaCloud\Client\arrayMerge(
67
            [
68 76
                self::$userAgent,
69 76
                $append,
70
            ]
71 76
        );
72
73 76
        foreach ($append as $key => $value) {
74 76
            if ($value === null) {
75
                $newUserAgent[] = $key;
76
                continue;
77
            }
78 76
            $newUserAgent[] = "$key/$value";
79 76
        }
80
81 76
        return $userAgent . \implode(' ', $newUserAgent);
82
    }
83
84
    /**
85
     * @param array $append
86
     *
87
     * @return array
88
     */
89 76
    public static function clean(array $append)
90
    {
91 76
        foreach ($append as $key => $value) {
92 6
            if (self::isGuarded($key)) {
93 1
                unset($append[$key]);
94 1
                continue;
95
            }
96 76
        }
97
98 76
        return $append;
99
    }
100
101
    /**
102
     * set User Agent of Alibaba Cloud.
103
     *
104
     * @param string $name
105
     * @param string $value
106
     */
107 6
    public static function append($name, $value)
108
    {
109 6
        self::defaultFields();
110
111 6
        if (!self::isGuarded($name)) {
112 5
            self::$userAgent[$name] = $value;
113 5
        }
114 6
    }
115
116
    /**
117
     * @param array $userAgent
118
     */
119 2
    public static function with(array $userAgent)
120
    {
121 2
        self::$userAgent = self::clean($userAgent);
122 2
    }
123
124
    /**
125
     * Clear all of the User Agent.
126
     */
127 10
    public static function clear()
128
    {
129 10
        self::$userAgent = [];
130 10
    }
131
132
    /**
133
     * @param $name
134
     *
135
     * @return bool
136
     */
137 9
    public static function isGuarded($name)
138
    {
139 9
        return in_array(strtolower($name), self::$guard, true);
140
    }
141
}
142