Issues (3)

src/ServerRequestFactory.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RazonYang\Psr7\Swoole;
6
7
use Nyholm\Psr7\ServerRequest;
8
use Nyholm\Psr7\UploadedFile;
9
use Nyholm\Psr7\Uri;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Message\UriInterface;
12
use Swoole\Http\Request;
13
14
final class ServerRequestFactory implements ServerRequestFactoryInterface
15
{
16 4
    public function create(Request $request): ServerRequestInterface
17
    {
18 4
        $server = array_change_key_case($request->server, CASE_UPPER);
19 4
        $server['SCRIPT_NAME'] = $this->getScriptName();
20 4
        $uri = $this->createUri($server);
21
22 4
        $headers = $request->header ?? [];
23
24 4
        $cookies = $request->cookie ?? [];
25 4
        if ($cookies) {
26 1
            $tmp = [];
27 1
            foreach ($cookies as $name => $value) {
28 1
                $tmp[] = sprintf('%s=%s', $name, $value);
29
            }
30 1
            $headers['cookie'] = implode('; ', $tmp);
31
        }
32
33 4
        $psrRequest = new ServerRequest(
34 4
            $request->getMethod(),
0 ignored issues
show
It seems like $request->getMethod() can also be of type false; however, parameter $method of Nyholm\Psr7\ServerRequest::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
            /** @scrutinizer ignore-type */ $request->getMethod(),
Loading history...
35
            $uri,
36
            $headers,
37 4
            $request->getContent(),
38 4
            $this->parseProtocolVersion($server),
39
            $server,
40
        );
41
42 4
        \parse_str($uri->getQuery(), $queryParams);
43
44 4
        $psrRequest = $psrRequest
45 4
            ->withQueryParams($queryParams)
46 4
            ->withCookieParams($cookies)
47 4
            ->withUploadedFiles($this->parseUploadedFiles($request->files ?? []));
48
49 4
        return $psrRequest;
50
    }
51
52 4
    private function getScriptName(): string
53
    {
54
        global $argv;
55
56 4
        return $argv[0] ?? '';
57
    }
58
59 25
    private function createUri(array $server): UriInterface
60
    {
61 25
        $uri = new Uri();
62
63 25
        if (!empty($server['HTTPS']) && $server['HTTPS'] !== 'off') {
64 3
            $uri = $uri->withScheme('https');
65
        } else {
66 22
            $uri = $uri->withScheme('http');
67
        }
68
69 25
        if (isset($server['SERVER_PORT'])) {
70 2
            $uri = $uri->withPort((int) $server['SERVER_PORT']);
71
        } else {
72 23
            $uri = $uri->withPort($uri->getScheme() === 'https' ? 443 : 80);
73
        }
74
75 25
        if (isset($server['HTTP_HOST'])) {
76 4
            $parts = explode(':', $server['HTTP_HOST']);
77 4
            $uri = count($parts) == 2
78 1
                ? $uri->withHost($parts[0])
79 1
                    ->withPort((int) $parts[1])
80 3
                : $uri->withHost($server['HTTP_HOST']);
81 21
        } elseif (isset($server['SERVER_NAME'])) {
82 1
            $uri = $uri->withHost($server['SERVER_NAME']);
83
        }
84
85 25
        if (isset($server['REQUEST_URI'])) {
86 8
            $uri = $uri->withPath($server['REQUEST_URI']);
87
        }
88
89 25
        if (isset($server['QUERY_STRING'])) {
90 4
            $uri = $uri->withQuery($server['QUERY_STRING']);
91
        }
92
93 25
        return $uri;
94
    }
95
96 7
    private function parseUploadedFiles(array $files): array
97
    {
98 7
        $uploadedFiles = [];
99
100 7
        foreach ($files as $file) {
101 3
            $uploadedFiles[] = new UploadedFile(
102 3
                $file['tmp_name'],
103 3
                $file['size'],
104 3
                $file['error'],
105 3
                $file['name'],
106 3
                $file['type'],
107
            );
108
        }
109
110 7
        return $uploadedFiles;
111
    }
112
113 8
    private function parseProtocolVersion(array $server): string
114
    {
115 8
        if (isset($server['SERVER_PROTOCOL']) && \strpos($server['SERVER_PROTOCOL'], 'HTTP/') === 0) {
116 7
            return \substr($server['SERVER_PROTOCOL'], 5);
117
        }
118
119 1
        return '1.1';
120
    }
121
}
122