Completed
Push — master ( a5c9dc...9f0cc8 )
by Joao
05:07
created

src/HttpRequest.php (2 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 ByJG\RestServer;
4
5
class HttpRequest
6
{
7
8
    protected $get;
9
    protected $post;
10
    protected $server;
11
    protected $session;
12
    protected $cookie;
13
    protected $request;
14
15
    /**
16
     *
17
     * @param array $get
18
     * @param array $post
19
     * @param array $server
20
     * @param array $session
21
     * @param array $cookie
22
     */
23
    public function __construct($get, $post, $server, $session, $cookie)
24
    {
25
        $this->get = $get;
26
        $this->post = $post;
27
        $this->server = $server;
28
        $this->session = $session;
29
        $this->cookie = $cookie;
30
31
        $this->request = array_merge($get, $post, $server, $session, $cookie);
32
    }
33
34
    /**
35
     * Get a parameter passed by GET (the same as $_GET). If not found return false.
36
     *
37
     * @param string $value
38
     * @return string|boolean
39
     */
40 View Code Duplication
    public function get($value)
41
    {
42
        if (!isset($this->get[$value])) {
43
            return false;
44
        } else {
45
            return $this->get[$value];
46
        }
47
    }
48
49
    /**
50
     * Get a parameter passed by POST (the same as $_POST). If not found return false.
51
     *
52
     * @param string $value
53
     * @return string|boolean
54
     */
55 View Code Duplication
    public function post($value)
56
    {
57
        if (!isset($this->post[$value])) {
58
            return false;
59
        } else {
60
            return $this->post[$value];
61
        }
62
    }
63
64
    /**
65
     * Get the parameters sent by server (the same as $_SERVER). If not found return false.
66
     *
67
     * @param string $value
68
     * @return string|boolean
69
     */
70 View Code Duplication
    public function server($value)
71
    {
72
        if (!isset($this->server[$value])) {
73
            return false;
74
        } else {
75
            return $this->server[$value];
76
        }
77
    }
78
79
    /**
80
     * Get a server session value(the same as $_SESSION). If not found return false.
81
     *
82
     * @param string $value
83
     * @return string|boolean
84
     */
85 View Code Duplication
    public function session($value)
86
    {
87
        if (!isset($this->session[$value])) {
88
            return false;
89
        } else {
90
            return $this->session[$value];
91
        }
92
    }
93
94
    /**
95
     * Get the cookie sent by the client (the same as $_COOKIE). If not found return false.
96
     *
97
     * @param string $value
98
     * @return string|boolean
99
     */
100 View Code Duplication
    public function cookie($value)
101
    {
102
        if (!isset($this->cookie[$value])) {
103
            return false;
104
        } else {
105
            return $this->cookie[$value];
106
        }
107
    }
108
109
    /**
110
     * Get a value from any of get, post, server, cookie or session. If not found return false.
111
     *
112
     * @param string $value
113
     * @return string|boolean
114
     */
115 View Code Duplication
    public function request($value)
116
    {
117
        if (!isset($this->request[$value])) {
118
            return false;
119
        } else {
120
            return $this->request[$value];
121
        }
122
    }
123
124
    private $payload;
125
126
    /**
127
     * Get the payload passed during the request(the same as php://input). If not found return empty.
128
     *
129
     * @return string
130
     */
131
    public function payload()
132
    {
133
        if (is_null($this->payload)) {
134
            $this->payload = file_get_contents("php://input");
135
        }
136
137
        return $this->payload;
138
    }
139
140
    /**
141
     * Use this method to get the CLIENT REQUEST IP.
142
     * Note that if you behing a Proxy, the variable REMOTE_ADDR will always have the same IP
143
     * @return string
144
     */
145
    public function getRequestIp()
146
    {
147
        $ipaddress = '';
0 ignored issues
show
$ipaddress is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
148
        if ($this->server('HTTP_CLIENT_IP') !== false) {
149
            $ipaddress = $this->server('HTTP_CLIENT_IP');
150
        } else if ($this->server('HTTP_X_FORWARDED_FOR') !== false) {
151
            $ipaddress = $this->server('HTTP_X_FORWARDED_FOR');
152
        } else if ($this->server('HTTP_X_FORWARDED') !== false) {
153
            $ipaddress = $this->server('HTTP_X_FORWARDED');
154
        } else if ($this->server('HTTP_FORWARDED_FOR') !== false) {
155
            $ipaddress = $this->server('HTTP_FORWARDED_FOR');
156
        } else if ($this->server('HTTP_FORWARDED') !== false) {
157
            $ipaddress = $this->server('HTTP_FORWARDED');
158
        } else if ($this->server('REMOTE_ADDR') !== false) {
159
            $ipaddress = $this->server('REMOTE_ADDR');
160
        } else {
161
            $ipaddress = 'UNKNOWN';
162
        }
163
164
        return $ipaddress;
165
    }
166
167
    /**
168
     * Use this method to get the SERVER NAME.
169
     * @return string
170
     */
171
    public function getRequestServer($port = false, $protocol = false)
172
    {
173
        $servername = '';
0 ignored issues
show
$servername is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
174
        if ($this->server('SERVER_NAME') !== false) {
175
            $servername = $this->server('SERVER_NAME');
176
        } else if ($this->server('HTTP_HOST' !== false)) {
177
            $servername = $this->server('HTTP_HOST');
178
        } else {
179
            $servername = $this->server('SERVER_ADDR');
180
        }
181
182
        if ($port && $this->server('SERVER_PORT' !== false)) {
183
            $servername .= ':' . $this->server('SERVER_PORT');
184
        }
185
186
        if ($protocol) {
187
            $servername = (($this->server('HTTPS') !== 'off' || $this->server('SERVER_PORT') == 443) ? "https://" : "http://") . $servername;
188
        }
189
190
        return $servername;
191
    }
192
}
193