Issues (26)

src/Components/Target.php (6 issues)

1
<?php
2
3
namespace Raptor\Request\Components;
4
5
class Target
6
{
7
    /**
8
     * Format of the request-target (see https://tools.ietf.org/html/rfc7230#section-5.3)
9
     * Currently Raptor Request supports `origin-form` format only.
10
     *
11
     * @var string
12
     */
13
    protected $fromat = 'origin-form';
14
15
    /**
16
     * absolute-path (see https://tools.ietf.org/html/rfc7230#section-5.3.1)
17
     *
18
     * @var string
19
     */
20
    protected $path;
21
22
    /**
23
     * Query string (see https://tools.ietf.org/html/rfc7230#section-5.3.1)
24
     *
25
     * @var array
26
     */
27
    protected $query;
28
29
    /**
30
     * Get format of the request-target.
31
     *
32
     * @return string
33
     */
34
    public function format()
35
    {
36
        if ($this->format !== null) {
37
        	return $this->format;
38
        }
39
40
        if ($this->method() !== 'CONNECT' && $this->method !== 'OPTIONS') {
0 ignored issues
show
The method method() does not exist on Raptor\Request\Components\Target. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        if ($this->/** @scrutinizer ignore-call */ method() !== 'CONNECT' && $this->method !== 'OPTIONS') {

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...
Bug Best Practice introduced by
The property method does not exist on Raptor\Request\Components\Target. Did you maybe forget to declare it?
Loading history...
41
            if (strpos($this->path(), '/') === 0) {
42
                return $this->format = 'origin-form';
0 ignored issues
show
Bug Best Practice introduced by
The property format does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
            }
44
            return $this->format = 'absolute-form';
45
        }
46
47
        if ($this->method() === 'CONNECT') {
48
            return $this->format = 'authority-form';
49
        }
50
51
        if ($this->method() === 'OPTIONS') {
52
            return $this->format = 'asterisk-form';
53
        }
54
55
        return $this->format = false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->format = false returns the type false which is incompatible with the documented return type string.
Loading history...
56
    }
57
58
    /**
59
     * Get the absolute-path.
60
     *
61
     * @return string
62
     */
63
    public function path()
64
    {
65
        if ($this->path !== null) {
66
        	return $this->path;
67
        }
68
        if (!empty($_SERVER['HTTP_X_ORIGINAL_URL'])) {
69
            // IIS with Microsoft Rewrite Module
70
            unset($_SERVER['HTTP_X_ORIGINAL_URL']);
71
            unset($_SERVER['UNENCODED_URL']);
72
            unset($_SERVER['IIS_WasUrlRewritten']);
73
            return $this->path = explode('?', $_SERVER['HTTP_X_ORIGINAL_URL'])[0];
74
        }
75
        if (!empty($_SERVER['HTTP_X_REWRITE_URL'])) {
76
            // IIS with ISAPI_Rewrite
77
            unset($_SERVER['HTTP_X_REWRITE_URL']);
78
            return $this->path = explode('?', $_SERVER['HTTP_X_REWRITE_URL'])[0];
79
        }
80
        if (!empty($_SERVER['IIS_WasUrlRewritten']) && $_SERVER['IIS_WasUrlRewritten'] == '1' && $_SERVER['UNENCODED_URL'] != '') {
81
            // IIS7 with URL Rewrite: make sure we get the unencoded URL (double slash problem)
82
            unset($_SERVER['UNENCODED_URL']);
83
            unset($_SERVER['IIS_WasUrlRewritten']);
84
            return $this->path = explode('?', $_SERVER['UNENCODED_URL'])[0];
85
        }
86
        if (!empty($_SERVER['REQUEST_URI'])) {
87
            return $this->path = explode('?', $_SERVER['REQUEST_URI'])[0];
88
            // HTTP proxy reqs setup request URI with scheme and host [and port] + the URL uri, only use URL uri
89
            // $schemeAndHttpHost = $this->scheme().'://'.$this->host();
90
            // if (strpos($requestUri, $schemeAndHttpHost) === 0) {
91
            //     $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
92
            // }
93
        }
94
        if (!empty($_SERVER['ORIG_PATH_INFO'])) {
95
            // IIS 5.0, PHP as CGI
96
            // if ('' != $_SERVER['QUERY_STRING']) {
97
            //     $this->path .= '?'.$_SERVER['QUERY_STRING'];
98
            // }
99
            unset($_SERVER['ORIG_PATH_INFO']);
100
            return $this->path = $_SERVER['ORIG_PATH_INFO'];
101
        }
102
        
103
        return $this->path = false;
0 ignored issues
show
Documentation Bug introduced by
The property $path was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
Bug Best Practice introduced by
The expression return $this->path = false returns the type false which is incompatible with the documented return type string.
Loading history...
104
    }
105
106
    /**
107
     * Get the query string.
108
     *
109
     * @param  string $key (optional)
110
     * @param  mixed $default (optional)
111
     * @return  mixed
112
     */
113
    public function query($key = null, $default = null)
114
    {
115
        if ($this->query === null) {
116
        	$this->query = $_GET;
117
        }
118
        if ($key === null) {
119
        	return $this->query;
120
        }
121
        return array_key_exists($key, $this->query) ? $this->query[$key] : $default;
122
    }
123
}