Request::toIlluminateRequest()   F
last analyzed

Complexity

Conditions 20
Paths 15360

Size

Total Lines 65
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 20
eloc 43
c 2
b 1
f 0
nc 15360
nop 2
dl 0
loc 65
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Swoole;
4
5
use Illuminate\Http\Request as IlluminateRequest;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request 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...
6
use Swoole\Http\Request as SwooleRequest;
7
use Symfony\Component\HttpFoundation\ParameterBag;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\ParameterBag 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...
8
9
class Request
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Request
Loading history...
10
{
11
    protected $swooleRequest;
12
13
    public function __construct(SwooleRequest $request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
14
    {
15
        $this->swooleRequest = $request;
16
    }
17
18
    /**
19
     * Convert SwooleRequest to IlluminateRequest
20
     * @param array $rawServer
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
21
     * @param array $rawEnv
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
22
     * @return IlluminateRequest
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
23
     */
24
    public function toIlluminateRequest(array $rawServer = [], array $rawEnv = [])
25
    {
26
        $__GET = isset($this->swooleRequest->get) ? $this->swooleRequest->get : [];
27
        $__POST = isset($this->swooleRequest->post) ? $this->swooleRequest->post : [];
28
        $__COOKIE = isset($this->swooleRequest->cookie) ? $this->swooleRequest->cookie : [];
29
        $server = isset($this->swooleRequest->server) ? $this->swooleRequest->server : [];
30
        $headers = isset($this->swooleRequest->header) ? $this->swooleRequest->header : [];
31
        $__FILES = isset($this->swooleRequest->files) ? $this->swooleRequest->files : [];
32
        $__CONTENT = empty($__FILES) ? $this->swooleRequest->rawContent() : ''; // Cannot call rawContent() to avoid double the file memory when uploading a file.
33
        $_REQUEST = [];
34
        $_SESSION = [];
35
36
        static $headerServerMapping = [
37
            'x-real-ip'       => 'REMOTE_ADDR',
38
            'x-real-port'     => 'REMOTE_PORT',
39
            'server-protocol' => 'SERVER_PROTOCOL',
40
            'server-name'     => 'SERVER_NAME',
41
            'server-addr'     => 'SERVER_ADDR',
42
            'server-port'     => 'SERVER_PORT',
43
            'scheme'          => 'REQUEST_SCHEME',
44
        ];
45
46
        $_ENV = $rawEnv;
47
        $_SERVER = $rawServer;
48
        foreach ($headers as $key => $value) {
49
            // Fix client && server's info
50
            if (isset($headerServerMapping[$key])) {
51
                $server[$headerServerMapping[$key]] = $value;
52
            } else {
53
                $key = str_replace('-', '_', $key);
54
                $server['http_' . $key] = $value;
55
            }
56
        }
57
        $server = array_change_key_case($server, CASE_UPPER);
58
        $_SERVER = array_merge($_SERVER, $server);
59
        if (isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] === 'https') {
60
            $_SERVER['HTTPS'] = 'on';
61
        }
62
63
        // Fix REQUEST_URI with QUERY_STRING
64
        if (strpos($_SERVER['REQUEST_URI'], '?') === false
65
            && isset($_SERVER['QUERY_STRING'])
66
            && $_SERVER['QUERY_STRING'] !== ''
67
        ) {
68
            $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
69
        }
70
71
        // Fix argv & argc
72
        if (!isset($_SERVER['argv'])) {
73
            $_SERVER['argv'] = isset($GLOBALS['argv']) ? $GLOBALS['argv'] : [];
74
            $_SERVER['argc'] = isset($GLOBALS['argc']) ? $GLOBALS['argc'] : 0;
75
        }
76
77
        // Initialize laravel request
78
        IlluminateRequest::enableHttpMethodParameterOverride();
79
        $request = IlluminateRequest::createFromBase(new \Symfony\Component\HttpFoundation\Request($__GET, $__POST, [], $__COOKIE, $__FILES, $_SERVER, $__CONTENT));
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request 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...
80
81
        if (0 === strpos($request->headers->get('CONTENT_TYPE', ''), 'application/x-www-form-urlencoded')
82
            && in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'])
83
        ) {
84
            parse_str($request->getContent(), $data);
85
            $request->request = new ParameterBag($data);
86
        }
87
88
        return $request;
89
    }
90
}
91