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 ( 9d90d7...d05834 )
by Cees-Jan
01:36
created

CustomRequestBodyParsers   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 80.56%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 68
ccs 29
cts 36
cp 0.8056
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addType() 0 3 1
A __invoke() 0 20 4
B __construct() 0 34 3
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use function RingCentral\Psr7\stream_for;
0 ignored issues
show
Bug introduced by
The type RingCentral\Psr7\stream_for was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
final class CustomRequestBodyParsers
9
{
10
    /**
11
     * @var array
12
     */
13
    private $types = [];
14
15
    public function __construct()
16
    {
17
        /**
18
         * Via: https://github.com/reactphp/http/pull/220#discussion_r140863176.
19
         */
20 5
        $this->addType('application/json', function (ServerRequestInterface $request) {
21 1
            $body = (string)$request->getBody();
22 1
            $result = json_decode($body, true);
23 1
            if (!is_array($result)) {
24
                return $request;
25
            }
26
27 1
            return $request->withParsedBody($result)->withBody(stream_for($body));
28 5
        });
29
30
        /**
31
         * Via: https://github.com/reactphp/http/pull/220#discussion_r140863176.
32
         */
33 5
        $xmlParser = function (ServerRequestInterface $request) {
34 3
            $body = (string)$request->getBody();
35 3
            $backup = libxml_disable_entity_loader(true);
36 3
            $backup_errors = libxml_use_internal_errors(true);
37 3
            $result = simplexml_load_string($body);
38 3
            libxml_disable_entity_loader($backup);
39 3
            libxml_clear_errors();
40 3
            libxml_use_internal_errors($backup_errors);
41 3
            if ($result === false) {
42
                return $request;
43
            }
44
45 3
            return $request->withParsedBody($result)->withBody(stream_for($body));
46 5
        };
47 5
        $this->addType('application/xml', $xmlParser);
48 5
        $this->addType('text/xml', $xmlParser);
49 5
    }
50
51 5
    public function __invoke(ServerRequestInterface $request, $next)
52
    {
53 5
        $type = strtolower($request->getHeaderLine('Content-Type'));
54 5
        list($type) = explode(';', $type);
55
56 5
        if (!isset($this->types[$type])) {
57
            return $next($request);
58
        }
59
60
        try {
61 5
            $parser = $this->types[$type];
62
            /** @var ServerRequestInterface $request */
63 5
            $request = $parser($request);
64
        } catch (\Exception $e) {
65
            return $next($request);
66
        } catch (\Throwable $t) {
67
            return $next($request);
68
        }
69
70 5
        return $next($request);
71
    }
72
73 5
    public function addType($type, $callback)
74
    {
75 5
        $this->types[$type] = $callback;
76 5
    }
77
}
78