Completed
Push — master ( 9bf2b4...5507a2 )
by Westin
03:14
created

Psr7RequestBuilder::buildServerParams()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 39
cts 39
cp 1
rs 8.9411
c 0
b 0
f 0
cc 2
eloc 39
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\SwooleExpressive\Bridge;
5
6
use Swoole\Http\Request as SwooleRequest;
7
use Zend\Diactoros\ServerRequest;
8
9
class Psr7RequestBuilder
10
{
11 3
    public function build(SwooleRequest $swooleRequest)
12
    {
13 3
        $body = (string) $swooleRequest->rawcontent();
14
15 3
        if (empty($body)) {
16 3
            $body = 'php://input';
17
        }
18
19 3
        return new ServerRequest(
20 3
            $this->buildServerParams($swooleRequest),
21 3
            $swooleRequest->files ?? [],
22 3
            $swooleRequest->server['request_uri'] ?? null,
23 3
            $swooleRequest->server['request_method'] ?? null,
24 3
            $body,
25 3
            $swooleRequest->header ?? [],
26 3
            $swooleRequest->cookie ?? [],
27 3
            $swooleRequest->get ?? [],
28 3
            $swooleRequest->post ?? null,
29 3
            $swooleRequest->server['server_protocol'] ?? '1.1'
30
        );
31
    }
32
33 3
    public function buildServerParams(SwooleRequest $swooleRequest)
34
    {
35 3
        $server = $swooleRequest->server ?? [];
36 3
        $header = $swooleRequest->header ?? [];
37
38 3
        $return['USER'] = get_current_user();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
39
40 3
        if (function_exists('posix_getpwuid')) {
41 3
            $return['USER'] = posix_getpwuid(posix_geteuid())['name'];
42
        }
43
44 3
        $return['HTTP_CACHE_CONTROL'] = $header['cache-control'] ?? '';
45 3
        $return['HTTP_UPGRADE_INSECURE_REQUESTS'] = $header['upgrade-insecure-requests-control'] ?? '';
46 3
        $return['HTTP_CONNECTION'] = $header['connection'] ?? '';
47 3
        $return['HTTP_DNT'] = $header['dnt'] ?? '';
48 3
        $return['HTTP_ACCEPT_ENCODING'] = $header['accept-encoding'] ?? '';
49 3
        $return['HTTP_ACCEPT_LANGUAGE'] = $header['accept-accept-language'] ?? '';
50 3
        $return['HTTP_ACCEPT'] = $header['accept'] ?? '';
51 3
        $return['HTTP_USER_AGENT'] = $header['user-agent'] ?? '';
52 3
        $return['HTTP_HOST'] = $header['user-host'] ?? '';
53 3
        $return['SERVER_NAME'] = '_';
54 3
        $return['SERVER_PORT'] = $server['server_port'] ?? null;
55 3
        $return['SERVER_ADDR'] = $server['server_addr'] ?? '';
56 3
        $return['REMOTE_PORT'] = $server['remote_port'] ?? null;
57 3
        $return['REMOTE_ADDR'] = $server['remote_addr'] ?? '';
58 3
        $return['SERVER_SOFTWARE'] = $server['server_software'] ?? '';
59 3
        $return['GATEWAY_INTERFACE'] = $server['server_software'] ?? '';
60 3
        $return['REQUEST_SCHEME'] = 'http';
61 3
        $return['SERVER_PROTOCOL'] = $server['server_protocol'] ?? null;
62 3
        $return['DOCUMENT_ROOT'] = realpath(__DIR__.'/../../bin');
63 3
        $return['DOCUMENT_URI'] = '/';
64 3
        $return['REQUEST_URI'] = $server['request_uri'] ?? '';
65 3
        $return['SCRIPT_NAME'] = '/swoole-expressive';
66 3
        $return['CONTENT_LENGTH'] = $header['content-length'] ?? null;
67 3
        $return['CONTENT_TYPE'] = $header['content-type'] ?? null;
68 3
        $return['REQUEST_METHOD'] = $server['request_method'] ?? 'GET';
69 3
        $return['QUERY_STRING'] = $server['query_string'] ?? '';
70 3
        $return['SCRIPT_FILENAME'] = rtrim($return['DOCUMENT_ROOT'], '/').'/'.ltrim($return['SCRIPT_NAME']);
71 3
        $return['PATH_INFO'] = $server['path_info'] ?? '';
72 3
        $return['FCGI_ROLE'] = 'RESPONDER';
73 3
        $return['PHP_SELF'] = $return['PATH_INFO'];
74 3
        $return['REQUEST_TIME_FLOAT'] = $server['request_time_float'] ?? '';
75 3
        $return['REQUEST_TIME'] = $server['request_time'] ?? '';
76
77 3
        return $return;
78
    }
79
}
80