1 | <?php |
||
21 | class UserAgentParser |
||
22 | { |
||
23 | /** |
||
24 | * RFC 7231 - Section 5.5.3 - User-agent |
||
25 | */ |
||
26 | const RFC_README = 'https://tools.ietf.org/html/rfc7231#section-5.5.3'; |
||
27 | |||
28 | /** |
||
29 | * Origin product |
||
30 | * @var string |
||
31 | */ |
||
32 | private $originProduct; |
||
33 | |||
34 | /** |
||
35 | * Product |
||
36 | * @var string |
||
37 | */ |
||
38 | private $product; |
||
39 | |||
40 | /** |
||
41 | * Version |
||
42 | * @var float|int|string|null |
||
43 | */ |
||
44 | private $version; |
||
45 | |||
46 | /** |
||
47 | * UserAgentParser constructor |
||
48 | * |
||
49 | * @param string $product |
||
50 | * @param float|int|string|null $version |
||
51 | */ |
||
52 | public function __construct($product, $version = null) |
||
62 | |||
63 | /** |
||
64 | * Split Product and Version |
||
65 | * |
||
66 | * @return bool |
||
67 | */ |
||
68 | private function split() |
||
76 | |||
77 | /** |
||
78 | * Validate the Product format |
||
79 | * @link https://tools.ietf.org/html/rfc7231#section-5.5.3 |
||
80 | * @link https://tools.ietf.org/html/rfc7230#section-3.2.4 |
||
81 | * |
||
82 | * @return bool |
||
83 | * @throws Exceptions\ProductException |
||
84 | */ |
||
85 | private function validateProduct() |
||
97 | |||
98 | /** |
||
99 | * Check for blacklisted strings or characters |
||
100 | * |
||
101 | * @param float|int|string|null $input |
||
102 | * @return bool |
||
103 | * @throws Exceptions\FormatException |
||
104 | */ |
||
105 | private function blacklistCheck($input) |
||
119 | |||
120 | /** |
||
121 | * Strip invalid characters |
||
122 | * |
||
123 | * @param string|string[] $string |
||
124 | * @return string|string[]|null |
||
125 | */ |
||
126 | private function strip($string) |
||
130 | |||
131 | /** |
||
132 | * Validate the Version and it's format |
||
133 | * @link https://tools.ietf.org/html/rfc7231#section-5.5.3 |
||
134 | * |
||
135 | * @return bool |
||
136 | * @throws Exceptions\VersionException |
||
137 | */ |
||
138 | private function validateVersion() |
||
152 | |||
153 | /** |
||
154 | * Get User-agent |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getUserAgent() |
||
164 | |||
165 | /** |
||
166 | * Get product |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getProduct() |
||
174 | |||
175 | /** |
||
176 | * Get version |
||
177 | * |
||
178 | * @return float|int|string|null |
||
179 | */ |
||
180 | public function getVersion() |
||
184 | |||
185 | /** |
||
186 | * Find the best matching User-agent |
||
187 | * |
||
188 | * @param string[] $userAgents |
||
189 | * @return string|false |
||
190 | */ |
||
191 | public function getMostSpecific(array $userAgents) |
||
206 | |||
207 | /** |
||
208 | * Get an array of all possible User-agent combinations |
||
209 | * |
||
210 | * @return string[] |
||
211 | */ |
||
212 | public function getUserAgents() |
||
219 | |||
220 | /** |
||
221 | * Get versions |
||
222 | * |
||
223 | * @return float[]|int[]|string[] |
||
224 | */ |
||
225 | public function getVersions() |
||
245 | |||
246 | /** |
||
247 | * Explode |
||
248 | * |
||
249 | * @param string $string |
||
250 | * @param string $delimiter |
||
251 | * @return string[] |
||
252 | */ |
||
253 | private function explode($string, $delimiter) |
||
261 | |||
262 | /** |
||
263 | * Filter duplicates from an array |
||
264 | * |
||
265 | * @param string[] $array |
||
266 | * @return string[] |
||
267 | */ |
||
268 | private function filterDuplicates($array) |
||
278 | |||
279 | /** |
||
280 | * Get products |
||
281 | * |
||
282 | * @return string[] |
||
283 | */ |
||
284 | public function getProducts() |
||
294 | } |
||
295 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.