Passed
Branch master (72025d)
by
unknown
02:41
created

src/URL.php (1 issue)

assigning scalar types to arrays.

Bug Documentation Major

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
 *
4
 * @filesource   URL.php
5
 * @created      18.02.2016
6
 * @package      chillerlan\TinyCurl
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2016 Smiley
9
 * @license      MIT
10
 */
11
12
namespace chillerlan\TinyCurl;
13
14
/**
15
 * Class URL
16
 *
17
 * @property string url
18
 * @property string method
19
 * @property string scheme
20
 * @property string host
21
 * @property string port
22
 * @property string path
23
 * @property string query
24
 * @property string fragment
25
 * @property array  params
26
 * @property array  parsedquery
27
 * @property array  body
28
 */
29
class URL{
30
31
	/**
32
	 * @var string
33
	 */
34
	protected $url;
35
36
	/**
37
	 * @var string
38
	 */
39
	protected $method;
40
41
	/**
42
	 * @var string
43
	 */
44
	protected $scheme;
45
46
	/**
47
	 * @var string
48
	 */
49
	protected $host;
50
51
	/**
52
	 * @var int
53
	 */
54
	protected $port;
55
56
	/**
57
	 * @var string
58
	 */
59
	protected $path;
60
61
	/**
62
	 * @var string
63
	 */
64
	protected $query;
65
66
	/**
67
	 * @var string
68
	 */
69
	protected $fragment;
70
71
	/**
72
	 * @var array
73
	 */
74
	protected $parsedquery = [];
75
76
	/**
77
	 * @var array
78
	 */
79
	protected $params = [];
80
81
	/**
82
	 * @var array
83
	 */
84
	protected $body = [];
85
86
	/**
87
	 * URL constructor.
88
	 *
89
	 * @param string $url
90
	 * @param array  $params
91
	 * @param string $method
92
	 * @param array  $body
93
	 */
94
	public function __construct($url, array $params = [], $method = 'GET', $body = null){
95
		$this->url    = $url;
96
		$this->params = $params;
97
		$this->body   = $body;
0 ignored issues
show
Documentation Bug introduced by
It seems like $body can be null. However, the property $body is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
98
99
		$method = strtoupper($method);
100
		if(in_array($method, ['GET', 'POST'], true)){ // @todo
101
			$this->method = $method;
102
		}
103
104
		$this->parseUrl();
105
	}
106
107
	/**
108
	 * @param $name
109
	 *
110
	 * @return mixed
111
	 */
112
	public function __get($name){
113
		return $this->{$name};
114
	}
115
116
	/**
117
	 * @return string URL with merged params
118
	 */
119
	public function __toString(){
120
		return $this->mergeParams();
121
	}
122
123
	/**
124
	 * @return string
125
	 */
126
	public function originalParams(){
127
		return $this->getURL().'?'.http_build_query($this->parsedquery);
128
	}
129
130
	/**
131
	 * @return string
132
	 */
133
	public function overrideParams(){
134
		return $this->getURL().'?'.http_build_query($this->params);
135
	}
136
137
	/**
138
	 * @return string
139
	 */
140
	public function mergeParams(){
141
		return $this->getURL().'?'.http_build_query(array_merge($this->parsedquery, $this->params));
142
	}
143
144
	/**
145
	 * @return void
146
	 */
147
	protected function parseUrl(){
148
		$url = parse_url($this->url);
149
150
		$this->host      = !isset($url['host'])      ? null : $url['host'];
151
		$this->port      = !isset($url['port'])      ? null : $url['port'];
152
		$this->path      = !isset($url['path'])      ? null : $url['path'];
153
		$this->scheme    = !isset($url['scheme'])    ? null : $url['scheme'];
154
		$this->query     = !isset($url['query'])     ? null : $url['query'];
155
		$this->fragment  = !isset($url['fragment'])  ? null : $url['fragment'];
156
157
		if($this->query){
158
			parse_str($this->query, $this->parsedquery);
159
		}
160
161
	}
162
163
	/**
164
	 * @return string
165
	 */
166
	protected function getURL(){
167
		$url = '';
168
169
		if($this->scheme){
170
			$url .= $this->scheme.':';
171
		}
172
173
		if($this->host){
174
			$url .= '//'.$this->host;
175
176
			if($this->port){
177
				$url .= ':'.$this->port;
178
			}
179
180
		}
181
182
		if($this->path){
183
			$url .= $this->path;
184
		}
185
186
		return $url;
187
	}
188
189
}
190