Internal::create()   C
last analyzed

Complexity

Conditions 12
Paths 256

Size

Total Lines 59
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 12
eloc 30
c 3
b 0
f 0
nc 256
nop 5
dl 0
loc 59
rs 5.4333

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
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 3.0.0
13
 */
14
15
namespace Quantum\Http\Traits\Request;
16
17
use Quantum\Config\Exceptions\ConfigException;
18
use Quantum\App\Exceptions\BaseException;
19
use Quantum\Di\Exceptions\DiException;
20
use Quantum\Http\Enums\ContentType;
21
use Quantum\Environment\Server;
22
use ReflectionException;
23
24
/**
25
 * Trait Internal
26
 * @package Quantum\Http\Request
27
 */
28
trait Internal
29
{
30
    /**
31
     * Creates an internal request for testing purposes
32
     * @param string $method
33
     * @param string $url
34
     * @param array $params
35
     * @param array $headers
36
     * @param array $files
37
     * @throws BaseException
38
     * @throws ReflectionException
39
     * @throws ConfigException
40
     * @throws DiException
41
     */
42
    public static function create(
43
        string $method,
44
        string $url,
45
        array  $params = [],
46
        array  $headers = [],
47
        array  $files = []
48
    ) {
49
        $parsed = parse_url($url);
50
51
        $server = Server::getInstance();
52
53
        $server->flush();
54
55
        $server->set('REQUEST_METHOD', strtoupper($method));
56
57
        $path = isset($parsed['path']) ? ltrim($parsed['path'], '/') : '/';
58
59
        $server->set('REQUEST_URI', $path);
60
61
        if (isset($parsed['scheme'])) {
62
            $server->set('REQUEST_SCHEME', $parsed['scheme']);
63
            $server->set('HTTPS', $parsed['scheme'] === 'https' ? 'on' : 'off');
64
        }
65
66
        if (isset($parsed['host'])) {
67
            $server->set('HTTP_HOST', $parsed['host']);
68
            $server->set('SERVER_NAME', $parsed['host']);
69
        }
70
71
        if (isset($parsed['port'])) {
72
            $server->set('SERVER_PORT', $parsed['port']);
73
        } else {
74
            $server->set('SERVER_PORT', ($parsed['scheme'] ?? '') === 'https' ? 443 : 80);
75
        }
76
77
        if (isset($parsed['query'])) {
78
            $server->set('QUERY_STRING', $parsed['query']);
79
        } else {
80
            $server->set('QUERY_STRING', '');
81
        }
82
83
        self::detectAndSetContentType($server, $params, $files);
84
85
        if ($headers !== []) {
86
            foreach ($headers as $name => $value) {
87
                $server->set('HTTP_' . strtoupper(str_replace('-', '_', $name)), $value);
88
            }
89
        }
90
91
        self::flush();
92
93
        self::init($server);
94
95
        if ($params !== []) {
96
            self::setRequestParams($params);
97
        }
98
99
        if ($files !== []) {
100
            self::setUploadedFiles(self::handleFiles($files));
101
        }
102
    }
103
104
    /**
105
     * Detects the content type
106
     * @param $server
107
     * @param array|null $data
108
     * @param array|null $files
109
     * @return void
110
     */
111
    protected static function detectAndSetContentType($server, ?array $data = null, ?array $files = null): void
112
    {
113
        if ($files && count($files) > 0) {
114
            $server->set('CONTENT_TYPE', ContentType::FORM_DATA);
115
        } elseif ($data) {
116
            $server->set('CONTENT_TYPE', ContentType::URL_ENCODED);
117
        } else {
118
            $server->set('CONTENT_TYPE', ContentType::HTML);
119
        }
120
    }
121
}
122