Completed
Push — master ( f32a89...d65b07 )
by Mehmet
03:31
created

ServerRequestFactory::fromGlobals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 22
cp 0
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 5
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami\Http;
5
6
use Zend\Diactoros\ServerRequestFactory as DiactorosServerRequestFactory;
7
8
final class ServerRequestFactory extends DiactorosServerRequestFactory
9
{
10
    private static function marshalProtocolVersion(array $server) : string
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
11
    {
12
        if (! isset($server['SERVER_PROTOCOL'])) {
13
            return '1.1';
14
        }
15
        if (! preg_match('#^(HTTP/)?(?P<version>[1-9]\d*(?:\.\d)?)$#', $server['SERVER_PROTOCOL'], $matches)) {
16
            throw new \UnexpectedValueException(
17
                sprintf(
18
                    'Unrecognized protocol version (%s)',
19
                    $server['SERVER_PROTOCOL']
20
                )
21
            );
22
        }
23
        return $matches['version'];
24
    }
25
26
    public static function fromGlobals(
27
        array $server = null,
28
        array $query = null,
29
        array $body = null,
30
        array $cookies = null,
31
        array $files = null
32
    ) : ServerRequest {
33
        $server  = parent::normalizeServer($server);
0 ignored issues
show
Bug introduced by
It seems like $server can also be of type null; however, Zend\Diactoros\ServerReq...tory::normalizeServer() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (normalizeServer() instead of fromGlobals()). Are you sure this is correct? If so, you might want to change this to $this->normalizeServer().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
34
        $files   = parent::normalizeFiles($files);
0 ignored issues
show
Bug introduced by
It seems like $files can also be of type null; however, Zend\Diactoros\ServerReq...ctory::normalizeFiles() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (normalizeFiles() instead of fromGlobals()). Are you sure this is correct? If so, you might want to change this to $this->normalizeFiles().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
35
        $headers = parent::marshalHeaders($server);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (marshalHeaders() instead of fromGlobals()). Are you sure this is correct? If so, you might want to change this to $this->marshalHeaders().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
36
        return new ServerRequest(
37
            $server,
38
            $files,
39
            parent::marshalUriFromServer($server, $headers),
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (marshalUriFromServer() instead of fromGlobals()). Are you sure this is correct? If so, you might want to change this to $this->marshalUriFromServer().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
40
            parent::get('REQUEST_METHOD', $server, 'GET'),
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (get() instead of fromGlobals()). Are you sure this is correct? If so, you might want to change this to $this->get().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
41
            'php://input',
42
            $headers,
43
            $cookies,
0 ignored issues
show
Bug introduced by
It seems like $cookies defined by parameter $cookies on line 30 can also be of type null; however, Zend\Diactoros\ServerRequest::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
44
            $query,
0 ignored issues
show
Bug introduced by
It seems like $query defined by parameter $query on line 28 can also be of type null; however, Zend\Diactoros\ServerRequest::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
45
            $body,
46
            self::marshalProtocolVersion($server)
47
        );
48
    }
49
}
50