Completed
Branch master (6e5e87)
by Rougin
01:28
created

Request::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 9.2
c 1
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Zapheus\Bridge\Psr\Zapheus;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Zapheus\Http\Message\Collection;
7
use Zapheus\Http\Message\Request as ZapheusRequest;
8
9
/**
10
 * PSR-07 to Zapheus Server Request Bridge
11
 *
12
 * @package Zapheus
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class Request extends ZapheusRequest
16
{
17
    /**
18
     * Initializes the server request instance.
19
     *
20
     * @param \Psr\Http\Message\ServerRequestInterface $request
21
     */
22 27
    public function __construct(ServerRequestInterface $request)
23
    {
24 27
        $server = $request->getServerParams();
25
26 27
        $cookies = $request->getCookieParams();
27
28 27
        list($query, $files) = $this->globals($request);
29
30 27
        $data = $request->getParsedBody();
31
32 27
        $attributes = $request->getAttributes();
33
34 27
        parent::__construct($server, $cookies, $data, $files, $query, $attributes);
0 ignored issues
show
Documentation introduced by
$server is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$cookies is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 30 can also be of type array or object; however, Zapheus\Http\Message\Request::__construct() does only seem to accept null|object<Zapheus\Http\Message\UriInterface>, 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...
Documentation introduced by
$files is of type array, but the function expects a null|object<Zapheus\Http\Message\StreamInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$attributes is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
36 27
        $this->set('headers', new Collection($request->getHeaders()), true);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Zapheus\Bridge\Psr\Zapheus\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
38 27
        $this->set('uri', new Uri($request->getUri()), true);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Zapheus\Bridge\Psr\Zapheus\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
40 27
        $this->set('stream', new Stream($request->getBody()), true);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Zapheus\Bridge\Psr\Zapheus\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
42 27
        $this->set('version', $request->getProtocolVersion(), true);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Zapheus\Bridge\Psr\Zapheus\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43 27
    }
44
45
    /**
46
     * Returns a listing of globals.
47
     *
48
     * @param  \Psr\Http\Message\ServerRequestInterface $request
49
     * @return array
50
     */
51 27
    protected function globals(ServerRequestInterface $request)
52
    {
53 27
        list($items, $uploaded) = array(array(), array());
0 ignored issues
show
Unused Code introduced by
The assignment to $items is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
54
55 27
        $items = $request->getUploadedFiles();
56
57 27 View Code Duplication
        foreach ((array) $items as $key => $files) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58 27
            $uploaded[$key] = array();
59
60 27
            foreach ((array) $files as $file) {
61 27
                $item = new File($file);
62
                
63 27
                array_push($uploaded[$key], $item);
64 27
            }
65 27
        }
66
   
67 27
        return array($request->getQueryParams(), $uploaded);
68
    }
69
}
70