RequestFactory::fromGlobals()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.5069

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 2
b 0
f 0
nc 3
nop 5
dl 0
loc 24
ccs 8
cts 11
cp 0.7272
crap 5.5069
rs 9.6111
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Request;
13
14
use Bluz\Http\RequestMethod;
15
use Bluz\Proxy\Request;
16
use Laminas\Diactoros\ServerRequest;
0 ignored issues
show
Bug introduced by
The type Laminas\Diactoros\ServerRequest 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...
17
use Laminas\Diactoros\ServerRequestFactory;
0 ignored issues
show
Bug introduced by
The type Laminas\Diactoros\ServerRequestFactory 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...
18
19
/**
20
 * Request Factory
21
 *
22
 * @package  Bluz\Request
23
 * @author   Anton Shevchuk
24
 */
25
class RequestFactory extends ServerRequestFactory
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 587
    public static function fromGlobals(
31
        array $server = null,
32
        array $query = null,
33
        array $body = null,
34
        array $cookies = null,
35
        array $files = null
36
    ): ServerRequest {
37
38 587
        $request = parent::fromGlobals($server, $query, $body, $cookies, $files);
39
40 587
        $contentType = current($request->getHeader('Content-Type'));
41
42
        // support header like "application/json" and "application/json; charset=utf-8"
43 587
        if (false !== $contentType && false !== stripos($contentType, Request::TYPE_JSON)) {
44
            $input = file_get_contents('php://input');
45
            $data = (array) json_decode($input, false);
46 587
        } elseif ($request->getMethod() === RequestMethod::POST) {
47
            $data = $_POST;
48
        } else {
49 587
            $input = file_get_contents('php://input');
50 587
            parse_str($input, $data);
51
        }
52
53 587
        return $request->withParsedBody($body ?: $data);
54
    }
55
}
56