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.

UserAgent::append()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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