Request::getParameters()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/**
4
 * Manage Request
5
 *
6
 * @category  	lib
7
 * @author    	Judicaël Paquet <[email protected]>
8
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
9
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
10
 * @version   	Release: 1.0.0
11
 * @filesource	https://github.com/las93/venus2
12
 * @link      	https://github.com/las93
13
 * @since     	1.0
14
 */
15
namespace Venus\lib;
16
17
use \Venus\core\Mother as Mother;
18
use \Venus\lib\Request\Cookies as Cookies;
19
use \Venus\lib\Request\Files as Files;
20
use \Venus\lib\Request\Headers as Headers;
21
use \Venus\lib\Request\Query as Query;
22
use \Venus\lib\Request\Request as RequestPost;
23
use \Venus\lib\Request\Server as Server;
24
25
/**
26
 * This class manage the request
27
 *
28
 * @property 	\Venus\lib\Request\Cookies cookies
29
 * @property 	\Venus\lib\Request\Files files
30
 * @property 	\Venus\lib\Request\Headers headers
31
 * @property 	\Venus\lib\Request\Query query
32
 * @property 	\Venus\lib\Request\Request request
33
 * @property 	\Venus\lib\Request\Server server
34
 * @category  	lib
35
 * @author    	Judicaël Paquet <[email protected]>
36
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
37
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
38
 * @version   	Release: 1.0.0
39
 * @filesource	https://github.com/las93/venus2
40
 * @link      	https://github.com/las93
41
 * @since     	1.0
42
 */
43
class Request extends Mother
44
{
45
	/**
46
	 * Query constructor.
47
	 */
48
	public function __construct()
49
	{
50
        $this->cookies = function() { return new Cookies(); };
51
        $this->files = function() { return new Files(); };
52
        $this->headers = function() { return new Headers(); };
53
        $this->query = function() { return new Query(); };
54
        $this->request = function() { return new RequestPost(); };
55
		$this->server = function() { return new Server(); };
56
	}
57
58
	/**
59
	 * if the request is ajax
60
	 *
61
	 * @access public
62
	 * @param  string $sName name of the template
0 ignored issues
show
Bug introduced by
There is no parameter named $sName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
63
	 * @return bool
64
	 */
65
	public static function isXmlHttpRequest()
66
	{
67
		if (!self::isCliRequest()) {
68
69
			if (array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
70
71
				return true;
72
			}
73
			else {
74
75
				return false;
76
			}
77
		}
78
	}
79
80
	/**
81
	 * if the request is http (web site or web service)
82
	 *
83
	 * @access public
84
	 * @return bool
85
	 */
86
	public static function isHttpRequest()
87
	{
88
		if (isset($_SERVER) && isset($_SERVER['HTTP_HOST'])) { return true; }
89
		else { return false; }
90
	}
91
92
	/**
93
	 * if the request is https (web site or web service)
94
	 *
95
	 * @access public
96
	 * @return bool
97
	 */
98
	public static function isHttpsRequest()
99
	{
100
		if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') { return true; }
101
		else { return false; }
102
	}
103
104
	/**
105
	 * if the request is http (web site or web service)
106
	 *
107
	 * @access public
108
	 * @return bool
109
	 */
110
	public static function isCliRequest()
111
	{
112
		$sSapiType = php_sapi_name();
113
114
		if (substr($sSapiType, 0, 3) == 'cgi' || defined('STDIN')) { return true; }
115
		else { return false; }
116
	}
117
118
	/**
119
	 * if the request is http (web site or web service)
120
	 *
121
	 * @access public
122
	 * @return bool
123
     * @deprecated  don't use this method because they return a false result
124
     *              delete in the version 5
125
     * @throws \Exception
126
	 */
127
	public static function getPreferredLanguage()
128
	{
129
        throw new \Exception("Use getLanguages() method now!");
130
	}
131
132
	/**
133
	 * if the request is http (web site or web service)
134
	 *
135
	 * @access public
136
	 * @return bool
137
	 */
138
	public static function getParameters()
139
	{
140
		if (isset($_GET)) { return $_GET; }
141
		else { return array(); }
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type documented by Venus\lib\Request::getParameters of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
142
	}
143
144
	/**
145
	 * if the request is http (web site or web service)
146
	 *
147
	 * @access public
148
	 * @return bool
149
	 */
150
	public static function getPostParameters()
151
	{
152
		if (isset($_POST)) { return $_POST; }
153
		else { return array(); }
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type documented by Venus\lib\Request::getPostParameters of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
154
	}
155
156
	/**
157
	 * if there are POST parameters
158
	 *
159
	 * @access public
160
	 * @return bool
161
	 */
162
	public function isPost()
163
	{
164
		if (isset($_POST) && count($_POST) > 0) { return true; }
165
		else { return false; }
166
	}
167
168
	/**
169
	 * get the POST for $sName
170
	 *
171
	 * @access public
172
	 * @param  string $name
173
	 * @return mixed
174
     * @deprecated  please use $this->request->get()
175
     *              delete in the version 5
176
	 */
177
	public function getPost(string $name)
178
	{
179
		return $this->request->get($name);
0 ignored issues
show
Bug introduced by
The method get does only exist in Venus\lib\Request\Request, but not in Closure.

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...
180
	}
181
182
	/**
183
	 * get the put method
184
	 *
185
	 * @access public
186
	 * @return array
187
	 */
188
	public static function getPut() 
189
	{	    
190
	    $aPut = array();
191
	    
192
	    $rPutResource = fopen("php://input", "r");
193
	    
194
	    while ($sData = fread($rPutResource, 1024)) {
195
196
	        $aSeparatePut = explode('&', $sData);
197
	        
198
	        foreach($aSeparatePut as $sOne) {
199
	            
200
	            $aOnePut = explode('=', $sOne);
201
	            $aPut[$aOnePut[0]] = $aOnePut[1];
202
	        }
203
	    }
204
	    
205
	    return $aPut;
206
	}
207
208
	/**
209
	 * Set the HTTP status
210
	 *
211
	 * @access public
212
	 * @param  int $iCode
213
	 * @return void
214
	 */
215
	public static function setStatus($iCode)
216
	{
217
		if ($iCode === 200) { header('HTTP/1.1 200 Ok');  }
218
		else if ($iCode === 201) { header('HTTP/1.1 201 Created');  }
219
		else if ($iCode === 204) { header("HTTP/1.0 204 No Content");  }
220
		else if ($iCode === 403) { header('HTTP/1.1 403 Forbidden'); }
221
		else if ($iCode === 404) { header('HTTP/1.1 404 Not Found'); }
222
	}
223
224
    /**
225
     * get http method
226
     * @return string
227
     */
228
    public function getMethod() : string
229
    {
230
        return $this->server->get('REQUEST_METHOD');
0 ignored issues
show
Bug introduced by
The method get does only exist in Venus\lib\Request\Server, but not in Closure.

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...
231
    }
232
233
    /**
234
     * return languages accepted by the customer
235
     * @return array
236
     */
237
    public function getLanguages() : array
238
    {
239
        if (!self::isCliRequest()) { return explode(',', preg_replace('/^([^;]);?.*$/', '$1', $_SERVER['HTTP_ACCEPT_LANGUAGE'])); }
240
        else { return array(); }
241
    }
242
243
	/**
244
	 * get path info
245
	 * @return string
246
     */
247
	public function getPathInfo() : string
248
	{
249
		return $this->server->get('REQUEST_URI');
0 ignored issues
show
Bug introduced by
The method get does only exist in Venus\lib\Request\Server, but not in Closure.

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...
250
	}
251
}
252