1 | <?php |
||
2 | |||
3 | namespace NMeta; |
||
4 | |||
5 | /** |
||
6 | * Class NMeta |
||
7 | * |
||
8 | * @package NMeta |
||
9 | * @author Casper Rasmussen <[email protected]> |
||
10 | */ |
||
11 | class NMeta |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $platform; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $environment; |
||
22 | |||
23 | /** |
||
24 | * Version number. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $version = 1; |
||
29 | |||
30 | /** |
||
31 | * Major version number. |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $majorVersion = 0; |
||
36 | |||
37 | /** |
||
38 | * Minor version number. |
||
39 | * |
||
40 | * @var int |
||
41 | */ |
||
42 | protected $minorVersion = 0; |
||
43 | |||
44 | /** |
||
45 | * Patch version number. |
||
46 | * |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $patchVersion = 0; |
||
50 | |||
51 | /** |
||
52 | * @var string|null |
||
53 | */ |
||
54 | protected $deviceOsVersion; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $device; |
||
60 | |||
61 | /** |
||
62 | * platforms. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $platforms; |
||
67 | |||
68 | /** |
||
69 | * environments. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $environments; |
||
74 | |||
75 | /** |
||
76 | * NMeta constructor. |
||
77 | * |
||
78 | * @param string|null $header |
||
79 | * @param \NMeta\Config|null $config |
||
80 | * @throws \NMeta\BadRequestException |
||
81 | * @author Casper Rasmussen <[email protected]> |
||
82 | */ |
||
83 | public function __construct(?string $header = null, Config $config = null) |
||
84 | { |
||
85 | if (empty($header)) { |
||
86 | throw new BadRequestException($config->getHeader() . ' header is missing'); |
||
87 | } |
||
88 | |||
89 | if (!$config) { |
||
90 | $config = Config::createDefault(); |
||
91 | } |
||
92 | |||
93 | $this->platforms = $config->getPlatforms(); |
||
94 | $this->environments = $config->getEnvironments(); |
||
95 | |||
96 | $format = 'platform;environment;version;os-version;device'; // ios;local;1.0.0;10.1;iphone-x |
||
97 | |||
98 | $headerArr = explode(';', $header); |
||
99 | |||
100 | // Parse platform |
||
101 | if (!isset($headerArr[0]) || !in_array($headerArr[0], $this->platforms)) { |
||
102 | throw new BadRequestException($config->getHeader() . ' header: Platform is not supported, should be: ' . |
||
103 | implode(',', $this->platforms) . |
||
104 | ' - format:' . $format); |
||
105 | } |
||
106 | |||
107 | $this->platform = $headerArr[0]; |
||
108 | |||
109 | // Parse env |
||
110 | if (!isset($headerArr[1]) || !in_array($headerArr[1], $this->environments)) { |
||
111 | throw new BadRequestException($config->getHeader() . ' header: Environment is not supported, should be: ' . |
||
112 | implode(',', $this->environments) . |
||
113 | ' - format:' . $format); |
||
114 | } |
||
115 | |||
116 | $this->environment = $headerArr[1]; |
||
117 | |||
118 | // Web does not have further requirements, since they have a normal User-Agent header |
||
119 | if ($this->platform == 'web') { |
||
120 | return; |
||
121 | } |
||
122 | |||
123 | // Parse Build number |
||
124 | if (!isset($headerArr[2])) { |
||
125 | throw new BadRequestException('Meta header: Missing version' . ' - format:' . $format); |
||
126 | } |
||
127 | |||
128 | $this->version = $headerArr[2]; |
||
129 | $versionArr = explode('.', $this->version); |
||
130 | $this->majorVersion = $versionArr[0] ?? 0; |
||
0 ignored issues
–
show
|
|||
131 | $this->minorVersion = $versionArr[1] ?? 0; |
||
0 ignored issues
–
show
It seems like
$versionArr[1] ?? 0 can also be of type string . However, the property $minorVersion is declared as type integer . Maybe add an additional type check?
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 Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
![]() |
|||
132 | $this->patchVersion = $versionArr[2] ?? 0; |
||
0 ignored issues
–
show
It seems like
$versionArr[2] ?? 0 can also be of type string . However, the property $patchVersion is declared as type integer . Maybe add an additional type check?
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 Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
![]() |
|||
133 | |||
134 | // Parse device os version |
||
135 | if (!isset($headerArr[3])) { |
||
136 | throw new BadRequestException('Meta header: Missing device os version' . |
||
137 | ' - format:' . $format); |
||
138 | } |
||
139 | |||
140 | $this->deviceOsVersion = $headerArr[3]; |
||
141 | |||
142 | // Parse device |
||
143 | if (!isset($headerArr[4])) { |
||
144 | throw new BadRequestException('Meta header: Missing device' . ' - format:' . $format); |
||
145 | } |
||
146 | |||
147 | $this->device = $headerArr[4]; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Retrieve platform. |
||
152 | * |
||
153 | * @return string |
||
154 | * @author Casper Rasmussen <[email protected]> |
||
155 | */ |
||
156 | public function getPlatform() |
||
157 | { |
||
158 | return $this->platform; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Retrieve environment. |
||
163 | * |
||
164 | * @return string |
||
165 | * @author Casper Rasmussen <[email protected]> |
||
166 | */ |
||
167 | public function getEnvironment() |
||
168 | { |
||
169 | return $this->environment; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Retrieve version. |
||
174 | * |
||
175 | * @return string |
||
176 | * @author Casper Rasmussen <[email protected]> |
||
177 | */ |
||
178 | public function getVersion() |
||
179 | { |
||
180 | return $this->version; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Retrieve majorVersion. |
||
185 | * |
||
186 | * @return int |
||
187 | * @author Casper Rasmussen <[email protected]> |
||
188 | */ |
||
189 | public function getMajorVersion() |
||
190 | { |
||
191 | return $this->majorVersion; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Retrieve minorVersion. |
||
196 | * |
||
197 | * @return int |
||
198 | * @author Casper Rasmussen <[email protected]> |
||
199 | */ |
||
200 | public function getMinorVersion() |
||
201 | { |
||
202 | return $this->minorVersion; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Retrieve patchVersion. |
||
207 | * |
||
208 | * @return int |
||
209 | * @author Casper Rasmussen <[email protected]> |
||
210 | */ |
||
211 | public function getPatchVersion() |
||
212 | { |
||
213 | return $this->patchVersion; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Retrieve deviceOsVersion. |
||
218 | * |
||
219 | * @return null|string |
||
220 | * @author Casper Rasmussen <[email protected]> |
||
221 | */ |
||
222 | public function getDeviceOsVersion() |
||
223 | { |
||
224 | return $this->deviceOsVersion; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Retrieve device. |
||
229 | * |
||
230 | * @return string |
||
231 | * @author Casper Rasmussen <[email protected]> |
||
232 | */ |
||
233 | public function getDevice() |
||
234 | { |
||
235 | return $this->device; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * toArray. |
||
240 | * |
||
241 | * @return array |
||
242 | * @author Casper Rasmussen <[email protected]> |
||
243 | */ |
||
244 | public function toArray() |
||
245 | { |
||
246 | return [ |
||
247 | 'platform' => $this->platform, |
||
248 | 'environment' => $this->environment, |
||
249 | 'version' => $this->version, |
||
250 | 'majorVersion' => $this->majorVersion, |
||
251 | 'minorVersion' => $this->minorVersion, |
||
252 | 'patchVersion' => $this->patchVersion, |
||
253 | 'deviceOsVersion' => $this->deviceOsVersion, |
||
254 | 'device' => $this->device, |
||
255 | ]; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Pass object back to header format platform:environment;version;os-version;device |
||
260 | * example: ios;local;1.0.0;10.1;iphone-x |
||
261 | * |
||
262 | * @return string |
||
263 | * @author Casper Rasmussen <[email protected]> |
||
264 | * @access public |
||
265 | */ |
||
266 | public function toHeaderString(): string |
||
267 | { |
||
268 | switch ($this->platform) { |
||
269 | case 'web': |
||
270 | return sprintf('%s;%s;', $this->platform, $this->environment); |
||
271 | default: |
||
272 | return sprintf('%s;%s;%s;%s;%s', $this->platform, |
||
273 | $this->environment, $this->version, |
||
274 | $this->deviceOsVersion, $this->device); |
||
275 | } |
||
276 | } |
||
277 | } |
||
278 |
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.