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
04:12
created

UserAgent::toString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

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