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 (#21)
by
unknown
04:51
created

CustomRequestBodyParsers::__construct()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 19
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 34
ccs 0
cts 23
cp 0
crap 12
rs 9.6333
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use function React\Promise\reject;
7
use function RingCentral\Psr7\stream_for;
8
9
final class CustomRequestBodyParsers
10
{
11
    /**
12
     * @var array
13
     */
14
    private $types = [];
15
16
    public function __construct()
17
    {
18
        /**
19
         * Via: https://github.com/reactphp/http/pull/220#discussion_r140863176.
20
         */
21
        $this->addType('application/json', function (ServerRequestInterface $request) {
22
            $body = (string)$request->getBody();
23
            $result = \json_decode($body, true);
24
            if (!\is_array($result)) {
25
                return $request;
26
            }
27
28
            return $request->withParsedBody($result)->withBody(stream_for($body));
29
        });
30
31
        /**
32
         * Via: https://github.com/reactphp/http/pull/220#discussion_r140863176.
33
         */
34
        $xmlParser = function (ServerRequestInterface $request) {
35
            $body = (string)$request->getBody();
36
            $backup = \libxml_disable_entity_loader(true);
37
            $backup_errors = \libxml_use_internal_errors(true);
38
            $result = \simplexml_load_string($body);
39
            \libxml_disable_entity_loader($backup);
40
            \libxml_clear_errors();
41
            \libxml_use_internal_errors($backup_errors);
42
            if ($result === false) {
43
                return $request;
44
            }
45
46
            return $request->withParsedBody($result)->withBody(stream_for($body));
47
        };
48
        $this->addType('application/xml', $xmlParser);
49
        $this->addType('text/xml', $xmlParser);
50
    }
51
52
    public function __invoke(ServerRequestInterface $request, $next)
53
    {
54
        $type = \strtolower($request->getHeaderLine('Content-Type'));
55
        list($type) = \explode(';', $type);
56
57
        if (!isset($this->types[$type])) {
58
            return $next($request);
59
        }
60
61
        try {
62
            $parser = $this->types[$type];
63
            /** @var ServerRequestInterface $request */
64
            $request = $parser($request);
65
        } catch (\Throwable $t) {
66
            return reject($t);
67
        }
68
69
        return $next($request);
70
    }
71
72
    public function addType($type, $callback): void
73
    {
74
        $this->types[$type] = $callback;
75
    }
76
}
77