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
Push — master ( 234643...f31931 )
by Cees-Jan
01:53
created

psr7_uploaded_file_decode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 18
ccs 8
cts 8
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WyriHaximus;
4
5
use Cake\Utility\Hash;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Message\UploadedFileInterface;
10
use React\Http\Io\UploadedFile;
11
use RingCentral\Psr7\Request;
12
use RingCentral\Psr7\Response;
13
use RingCentral\Psr7\ServerRequest;
14
use function RingCentral\Psr7\stream_for;
15
16
function psr7_response_json_encode(ResponseInterface $response): string
17
{
18 1
    return json_try_encode(psr7_response_encode($response));
19
}
20
21
function psr7_response_encode(ResponseInterface $response): array
22
{
23 2
    $json = [];
24 2
    $json['protocol_version'] = $response->getProtocolVersion();
25 2
    $json['status_code'] = $response->getStatusCode();
26 2
    $json['reason_phrase'] = $response->getReasonPhrase();
27 2
    $json['headers'] = $response->getHeaders();
28 2
    $json['body'] = base64_encode($response->getBody()->getContents());
29
30 2
    return $json;
31
}
32
33
/**
34
 * @throws NotAnEncodedResponseException
35
 */
36
function psr7_response_json_decode(string $json): ResponseInterface
37
{
38 1
    return psr7_response_decode(json_try_decode($json, true));
39
}
40
41
/**
42
 * @throws NotAnEncodedResponseException
43
 */
44
function psr7_response_decode(array $json): ResponseInterface
45
{
46
    $properties = [
47 2
        'protocol_version',
48
        'status_code',
49
        'reason_phrase',
50
        'headers',
51
        'body',
52
    ];
53
54 2
    validate_array($json, $properties, NotAnEncodedResponseException::class);
55
56 2
    return new Response(
57 2
        $json['status_code'],
58 2
        $json['headers'],
59 2
        base64_decode($json['body'], true),
60 2
        $json['protocol_version'],
61 2
        $json['reason_phrase']
62
    );
63
}
64
65
function psr7_request_json_encode(RequestInterface $request): string
66
{
67 1
    return json_try_encode(psr7_request_encode($request));
68
}
69
70
function psr7_request_encode(RequestInterface $request): array
71
{
72 2
    $json = [];
73 2
    $json['protocol_version'] = $request->getProtocolVersion();
74 2
    $json['method'] = $request->getMethod();
75 2
    $json['uri'] = (string)$request->getUri();
76 2
    $json['headers'] = $request->getHeaders();
77 2
    $json['body'] = base64_encode($request->getBody()->getContents());
78
79 2
    return $json;
80
}
81
82
/**
83
 * @throws NotAnEncodedRequestException
84
 */
85
function psr7_request_json_decode(string $json): RequestInterface
86
{
87 1
    return psr7_request_decode(json_try_decode($json, true));
88
}
89
90
/**
91
 * @throws NotAnEncodedRequestException
92
 */
93
function psr7_request_decode(array $json): RequestInterface
94
{
95
    $properties = [
96 2
        'protocol_version',
97
        'method',
98
        'uri',
99
        'headers',
100
        'body',
101
    ];
102
103 2
    validate_array($json, $properties, NotAnEncodedRequestException::class);
104
105 2
    return new Request(
106 2
        $json['method'],
107 2
        $json['uri'],
108 2
        $json['headers'],
109 2
        base64_decode($json['body'], true),
110 2
        $json['protocol_version']
111
    );
112
}
113
114
function psr7_uploaded_file_json_encode(UploadedFileInterface $uploadedFile): string
115
{
116 1
    return json_try_encode(psr7_uploaded_file_encode($uploadedFile));
117
}
118
119
function psr7_uploaded_file_encode(UploadedFileInterface $uploadedFile): array
120
{
121 4
    $json = [];
122 4
    $json['filename'] = $uploadedFile->getClientFilename();
123 4
    $json['media_type'] = $uploadedFile->getClientMediaType();
124 4
    $json['error'] = $uploadedFile->getError();
125 4
    $json['size'] = $uploadedFile->getSize();
126 4
    $json['stream'] = base64_encode($uploadedFile->getStream()->getContents());
127
128 4
    return $json;
129
}
130
131
/**
132
 * @throws NotAnEncodedUploadedFileException
133
 */
134
function psr7_uploaded_file_json_decode(string $json): UploadedFileInterface
135
{
136 1
    return psr7_uploaded_file_decode(json_try_decode($json, true));
137
}
138
139
/**
140
 * @throws NotAnEncodedUploadedFileException
141
 */
142
function psr7_uploaded_file_decode(array $json): UploadedFileInterface
143
{
144
    $properties = [
145 4
        'stream',
146
        'size',
147
        'error',
148
        'filename',
149
        'media_type',
150
    ];
151
152 4
    validate_array($json, $properties, NotAnEncodedUploadedFileException::class);
153
154 4
    return new UploadedFile(
155 4
        stream_for(base64_decode($json['stream'], true)),
156 4
        $json['size'],
157 4
        $json['error'],
158 4
        $json['filename'],
159 4
        $json['media_type']
160
    );
161
}
162
163
function psr7_server_request_json_encode(ServerRequestInterface $request): string
164
{
165 1
    return json_try_encode(psr7_server_request_encode($request));
166
}
167
168
function psr7_server_request_encode(ServerRequestInterface $request): array
169
{
170 2
    $json = [];
171 2
    $json['protocol_version'] = $request->getProtocolVersion();
172 2
    $json['method'] = $request->getMethod();
173 2
    $json['uri'] = (string)$request->getUri();
174 2
    $json['query_params'] = $request->getQueryParams();
175 2
    $json['cookie_params'] = $request->getCookieParams();
176 2
    $json['server_params'] = $request->getServerParams();
177 2
    $json['headers'] = $request->getHeaders();
178 2
    $json['attributes'] = $request->getAttributes();
179 2
    $json['body'] = base64_encode($request->getBody()->getContents());
180 2
    $json['parsed_body'] = $request->getParsedBody();
181 2
    $json['files'] = $request->getUploadedFiles();
182 2
    $json['files'] = Hash::flatten($json['files']);
183 2
    foreach ($json['files'] as $key => $file) {
184 2
        $json['files'][$key] = psr7_uploaded_file_encode($file);
185
    }
186
187 2
    return $json;
188
}
189
190
/**
191
 * @throws NotAnEncodedServerRequestException
192
 * @throws NotAnEncodedUploadedFileException
193
 */
194
function psr7_server_request_json_decode(string $json): ServerRequestInterface
195
{
196 1
    return psr7_server_request_decode(json_try_decode($json, true));
197
}
198
199
/**
200
 * @throws NotAnEncodedServerRequestException
201
 * @throws NotAnEncodedUploadedFileException
202
 */
203
function psr7_server_request_decode(array $json): ServerRequestInterface
204
{
205
    $properties = [
206 2
        'protocol_version',
207
        'method',
208
        'uri',
209
        'query_params',
210
        'cookie_params',
211
        'server_params',
212
        'headers',
213
        'attributes',
214
        'body',
215
        'parsed_body',
216
        'files',
217
    ];
218
219 2
    validate_array($json, $properties, NotAnEncodedServerRequestException::class);
220
221 2
    $request = (new ServerRequest(
222 2
        $json['method'],
223 2
        $json['uri'],
224 2
        $json['headers'],
225 2
        base64_decode($json['body'], true),
226 2
        $json['protocol_version'],
227 2
        $json['server_params']
228
    ))->
229 2
        withParsedBody($json['parsed_body'])->
230 2
        withUploadedFiles($json['files'])->
231 2
        withQueryParams($json['query_params'])->
232 2
        withCookieParams($json['cookie_params'])
233
    ;
234
235 2
    foreach ($json['attributes'] as $key => $value) {
236 2
        $request = $request->withAttribute($key, $value);
237
    }
238
239 2
    if (count($json['files']) > 0) {
240 2
        foreach ($json['files'] as $key => $file) {
241 2
            $json['files'][$key] = psr7_uploaded_file_decode($file);
242
        }
243 2
        $json['files'] = Hash::expand($json['files']);
244 2
        $request = $request->withUploadedFiles($json['files']);
245
    }
246
247 2
    return $request;
248
}
249