Completed
Push — master ( 704539...2a5253 )
by Robbie
02:16
created

src/HttpRequestAdapter.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Robbie\Psr7;
4
5
use GuzzleHttp\Psr7\ServerRequest;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Robbie\Psr7\AbstractHttpAdapter;
8
use SilverStripe\Control\HTTPRequest;
9
10
/**
11
 * @package psr7-adapters
12
 */
13
class HttpRequestAdapter extends AbstractHttpAdapter
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $serverVars;
19
20
    /**
21
     * Set up the server vars - they can be overridden if required
22
     */
23
    public function __construct()
24
    {
25
        $this->setServerVars($_SERVER);
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function toPsr7($input)
32
    {
33
        $request = new ServerRequest(
34
            $input->httpMethod(),
0 ignored issues
show
The method httpMethod does only exist in SilverStripe\Control\HTTPRequest, but not in SilverStripe\Control\HTTPResponse.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
35
            $this->getUri($input->getURL()),
0 ignored issues
show
The method getURL does only exist in SilverStripe\Control\HTTPRequest, but not in SilverStripe\Control\HTTPResponse.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
36
            $input->getHeaders(),
37
            $input->getBody(),
38
            $this->getProtocolVersion(),
39
            $this->getServerVars()
40
        );
41
42
        if (!empty($input->getVars())) {
43
            $request = $request->withQueryParams($input->getVars());
0 ignored issues
show
The method getVars does only exist in SilverStripe\Control\HTTPRequest, but not in SilverStripe\Control\HTTPResponse.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
44
        }
45
46
        if (!empty($input->postVars())) {
47
            $request = $request->withParsedBody($input->postVars());
0 ignored issues
show
The method postVars does only exist in SilverStripe\Control\HTTPRequest, but not in SilverStripe\Control\HTTPResponse.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
        }
49
50
        return $request;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function fromPsr7($input)
57
    {
58
        $adapted = new HTTPRequest(
59
            $input->getMethod(),
60
            (string) $input->getUri()->getPath(),
61
            $input->getQueryParams(),
62
            $input->getParsedBody(),
63
            (string) $input->getBody()
64
        );
65
66
        $this->importHeaders($input, $adapted);
67
68
        return $adapted;
69
    }
70
71
    /**
72
     * Get the full request URI (can be empty, but probably won't be)
73
     *
74
     * @param  string $path
75
     * @return string
76
     */
77
    public function getUri($path)
78
    {
79
        $vars = $this->getServerVars();
80
81
        $uri = '';
82
        $protocol = (isset($vars['HTTPS']) || $vars['SERVER_PORT'] === '443') ? 'https' : 'http';
83
        $uri .= $protocol . '://';
84
85
        if (!empty($vars['PHP_AUTH_USER'])) {
86
            $uri .= $vars['PHP_AUTH_USER'];
87
88
            if (!empty($vars['PHP_AUTH_PW'])) {
89
                $uri .= ':' . $vars['PHP_AUTH_PW'];
90
            }
91
92
            $uri .= '@';
93
        }
94
95
        if (!empty($vars['HTTP_HOST'])) {
96
            $uri .= $vars['HTTP_HOST'];
97
        }
98
99
        $uri .= '/' . ltrim($path, '/');
100
101
        return $uri;
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function getServerVars()
108
    {
109
        return $this->serverVars;
110
    }
111
112
    /**
113
     * @param  array $vars
114
     * @return $this
115
     */
116
    public function setServerVars(array $vars)
117
    {
118
        $this->serverVars = $vars;
119
        return $this;
120
    }
121
}
122