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 Setup Failed
Pull Request — master (#162)
by Yong
03:00
created

Sign::percentEncode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Support;
4
5
use GuzzleHttp\Psr7\Request;
6
use Psr\Http\Message\UriInterface;
7
8
/**
9
 * Class Sign
10
 *
11
 * @package AlibabaCloud\Client\Support
12
 */
13
class Sign
14
{
15
    /**
16
     * @var string
17
     */
18
    private static $headerSeparator = "\n";
19
20
    /**
21
     * Construct standard Header for Alibaba Cloud.
22
     *
23
     * @param array $headers
24
     *
25
     * @return string
26
     */
27
    private static function acsHeaderString(array $headers)
28
    {
29
        $array = [];
30
        foreach ($headers as $headerKey => $headerValue) {
31
            $key = strtolower($headerKey);
32
            if (strncmp($key, 'x-acs-', 6) === 0) {
33
                $array[$key] = $headerValue;
34
            }
35
        }
36
        ksort($array);
37
        $string = '';
38
        foreach ($array as $sortMapKey => $sortMapValue) {
39
            $string .= $sortMapKey . ':' . $sortMapValue[0] . self::$headerSeparator;
40
        }
41
42
        return $string;
43
    }
44
45
    /**
46
     * @param UriInterface $uri
47
     *
48
     * @return string
49
     */
50
    private static function resourceString(UriInterface $uri)
51
    {
52
        return $uri->getPath() . '?' . $uri->getQuery();
53
    }
54
55
    /**
56
     * @param string $method
57
     * @param array  $headers
58
     *
59
     * @return string
60
     */
61
    private static function headerString($method, array $headers)
62
    {
63
        $string = $method . self::$headerSeparator;
64
        if (isset($headers['Accept'][0])) {
65
            $string .= $headers['Accept'][0];
66
        }
67
        $string .= self::$headerSeparator;
68
69
        if (isset($headers['Content-MD5'][0])) {
70
            $string .= $headers['Content-MD5'][0];
71
        }
72
        $string .= self::$headerSeparator;
73
74
        if (isset($headers['Content-Type'][0])) {
75
            $string .= $headers['Content-Type'][0];
76
        }
77
        $string .= self::$headerSeparator;
78
79
        if (isset($headers['Date'][0])) {
80
            $string .= $headers['Date'][0];
81
        }
82
        $string .= self::$headerSeparator;
83
84
        $string .= self::acsHeaderString($headers);
85
86
        return $string;
87
    }
88
89
    /**
90
     * @param string $string
91
     *
92
     * @return null|string|string[]
93
     */
94
    private static function percentEncode($string)
95
    {
96
        $result = urlencode($string);
97
        $result = str_replace(['+', '*'], ['%20', '%2A'], $result);
98
        $result = preg_replace('/%7E/', '~', $result);
99
100
        return $result;
101
    }
102
103
    /**
104
     * @param string $method
105
     * @param array  $parameters
106
     *
107
     * @return string
108
     */
109
    public static function rpcString($method, array $parameters)
110
    {
111
        ksort($parameters);
112
        $canonicalized = '';
113
        foreach ($parameters as $key => $value) {
114
            $canonicalized .= '&' . self::percentEncode($key) . '=' . self::percentEncode($value);
115
        }
116
117
        return $method . '&%2F&' . self::percentEncode(substr($canonicalized, 1));
118
    }
119
120
    /**
121
     * @param Request $request
122
     *
123
     * @return string
124
     */
125
    public static function roaString(Request $request)
126
    {
127
        return self::headerString($request->getMethod(), $request->getHeaders()) .
128
               self::resourceString($request->getUri());
129
    }
130
}
131