These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file is part of the teamneusta/hosts project. |
||
4 | * Copyright (c) 2017 neusta GmbH | Ein team neusta Unternehmen |
||
5 | * For the full copyright and license information, please view the LICENSE file that was distributed with this source code. |
||
6 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
7 | * |
||
8 | */ |
||
9 | |||
10 | /** |
||
11 | * originally authored by Kevin Herrera<[email protected]> |
||
12 | */ |
||
13 | namespace |
||
14 | |||
15 | { |
||
16 | use Herrera\Version\Comparator; |
||
17 | use Herrera\Version\Dumper; |
||
18 | use Herrera\Version\Parser; |
||
19 | |||
20 | $n = PHP_EOL; |
||
21 | |||
22 | set_error_handler( |
||
23 | function($code, $message) use ($n) { |
||
24 | if ($code & error_reporting()) { |
||
25 | echo "$n{$n}Error: $message$n$n"; |
||
26 | exit(1); |
||
27 | } |
||
28 | } |
||
29 | ); |
||
30 | |||
31 | echo "Host Installer$n"; |
||
32 | echo "=============$n$n"; |
||
33 | |||
34 | echo "Environment Check$n"; |
||
35 | echo "-----------------$n$n"; |
||
36 | |||
37 | echo "\"-\" indicates success.$n"; |
||
38 | echo "\"*\" indicates error.$n$n"; |
||
39 | |||
40 | // check version |
||
41 | check( |
||
42 | 'You have a supported version of PHP (>= 7.0.0).', |
||
43 | 'You need PHP 7.0.0 or greater.', |
||
44 | function() { |
||
45 | return version_compare(PHP_VERSION, '7.0.0', '>='); |
||
46 | } |
||
47 | ); |
||
48 | |||
49 | // check phar extension |
||
50 | check( |
||
51 | 'You have the "phar" extension installed.', |
||
52 | 'You need to have the "phar" extension installed.', |
||
53 | function() { |
||
54 | return extension_loaded('phar'); |
||
55 | } |
||
56 | ); |
||
57 | |||
58 | // check phar extension version |
||
59 | check( |
||
60 | 'You have a supported version of the "phar" extension.', |
||
61 | 'You need a newer version of the "phar" extension (>=2.0).', |
||
62 | function() { |
||
63 | $phar = new ReflectionExtension('phar'); |
||
64 | |||
65 | return version_compare($phar->getVersion(), '2.0', '>='); |
||
66 | } |
||
67 | ); |
||
68 | |||
69 | // check openssl extension |
||
70 | check( |
||
71 | 'You have the "openssl" extension installed.', |
||
72 | 'Notice: The "openssl" extension will be needed to sign with private keys.', |
||
73 | function() { |
||
74 | return extension_loaded('openssl'); |
||
75 | }, |
||
76 | false |
||
77 | ); |
||
78 | |||
79 | // check detect unicode setting |
||
80 | check( |
||
81 | 'The "detect_unicode" setting is off.', |
||
82 | 'The "detect_unicode" setting needs to be off.', |
||
83 | function() { |
||
84 | return (ini_get_bool('detect_unicode') === false); |
||
85 | } |
||
86 | ); |
||
87 | |||
88 | // check suhosin setting |
||
89 | if (extension_loaded('suhosin')) { |
||
90 | check( |
||
91 | 'The "phar" stream wrapper is allowed by suhosin.', |
||
92 | 'The "phar" stream wrapper is blocked by suhosin.', |
||
93 | function() { |
||
94 | $white = ini_get('suhosin.executor.include.whitelist'); |
||
95 | $black = ini_get('suhosin.executor.include.blacklist'); |
||
96 | |||
97 | if ((false === stripos($white, 'phar')) |
||
98 | || (false !== stripos($black, 'phar'))) { |
||
99 | return false; |
||
100 | } |
||
101 | |||
102 | return true; |
||
103 | } |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | // check allow url open setting |
||
108 | check( |
||
109 | 'The "allow_url_fopen" setting is on.', |
||
110 | 'The "allow_url_fopen" setting needs to be on.', |
||
111 | function() { |
||
112 | return ini_get_bool('allow_url_fopen'); |
||
113 | } |
||
114 | ); |
||
115 | |||
116 | echo "{$n}Everything seems good!$n$n"; |
||
117 | |||
118 | // Retrieve manifest |
||
119 | echo " - Downloading manifest...$n"; |
||
120 | |||
121 | $manifest = file_get_contents('http://teamneusta.github.io/php-cli-hosts/manifest.json'); |
||
122 | |||
123 | echo " - Reading manifest...$n"; |
||
124 | |||
125 | $manifest = json_decode($manifest); |
||
126 | $current = null; |
||
127 | |||
128 | foreach ($manifest as $item) { |
||
129 | $item->version = Parser::toVersion($item->version); |
||
130 | |||
131 | if ($current |
||
132 | && (Comparator::isGreaterThan($item->version, $current->version))) { |
||
133 | $current = $item; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | if (!$item) { |
||
138 | echo " x No application download was found.$n"; |
||
139 | } |
||
140 | |||
141 | echo " - Downloading Host v", Dumper::toString($item->version), "...$n"; |
||
142 | |||
143 | file_put_contents($item->name, file_get_contents($item->url)); |
||
144 | |||
145 | echo " - Checking file checksum...$n"; |
||
146 | |||
147 | if ($item->sha1 !== sha1_file($item->name)) { |
||
148 | unlink($item->name); |
||
149 | |||
150 | echo " x The download was corrupted.$n"; |
||
151 | } |
||
152 | |||
153 | echo " - Checking if valid Phar...$n"; |
||
154 | |||
155 | try { |
||
156 | new Phar($item->name); |
||
157 | } catch (Exception $e) { |
||
158 | echo " x The Phar is not valid.\n\n"; |
||
159 | |||
160 | throw $e; |
||
161 | } |
||
162 | |||
163 | echo " - Making Host executable...$n"; |
||
164 | |||
165 | if (@chmod($item->name, 0755) === false) { |
||
166 | throw new \RuntimeException('Permissions on ' . $item->name . ' could not be changed.'); |
||
167 | } |
||
168 | |||
169 | echo "{$n}Host installed!$n"; |
||
170 | |||
171 | /** |
||
172 | * Checks a condition, outputs a message, and exits if failed. |
||
173 | * |
||
174 | * @param string $success The success message. |
||
175 | * @param string $failure The failure message. |
||
176 | * @param callable $condition The condition to check. |
||
177 | * @param boolean $exit Exit on failure? |
||
178 | */ |
||
179 | function check($success, $failure, $condition, $exit = true) |
||
180 | { |
||
181 | global $n; |
||
0 ignored issues
–
show
|
|||
182 | |||
183 | if ($condition()) { |
||
184 | echo ' - ', $success, $n; |
||
185 | } else { |
||
186 | echo ' * ', $failure, $n; |
||
187 | |||
188 | if ($exit) { |
||
189 | exit(1); |
||
0 ignored issues
–
show
The function check() contains an exit expression.
An exit expression should only be used in rare cases. For example, if you write a short command line script. In most cases however, using an ![]() |
|||
190 | } |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param string $varname |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | function ini_get_bool($varname) { |
||
200 | return \filter_var(\ini_get($varname), FILTER_VALIDATE_BOOLEAN); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | namespace Herrera\Version\Exception |
||
205 | |||
206 | { |
||
207 | use Exception; |
||
0 ignored issues
–
show
|
|||
208 | |||
209 | /** |
||
210 | * Throw if an invalid version string representation is used. |
||
211 | * |
||
212 | * @author Kevin Herrera <[email protected]> |
||
213 | */ |
||
214 | class InvalidStringRepresentationException extends VersionException |
||
215 | { |
||
216 | /** |
||
217 | * The invalid string representation. |
||
218 | * |
||
219 | * @var string |
||
220 | */ |
||
221 | private $version; |
||
222 | |||
223 | /** |
||
224 | * Sets the invalid string representation. |
||
225 | * |
||
226 | * @param string $version The string representation. |
||
227 | */ |
||
228 | public function __construct($version) |
||
229 | { |
||
230 | parent::__construct( |
||
231 | sprintf( |
||
232 | 'The version string representation "%s" is invalid.', |
||
233 | $version |
||
234 | ) |
||
235 | ); |
||
236 | |||
237 | $this->version = $version; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Returns the invalid string representation. |
||
242 | * |
||
243 | * @return string The invalid string representation. |
||
244 | */ |
||
245 | public function getVersion() |
||
246 | { |
||
247 | return $this->version; |
||
248 | } |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * The base library exception class. |
||
253 | * |
||
254 | * @author Kevin Herrera <[email protected]> |
||
255 | */ |
||
256 | class VersionException extends Exception |
||
0 ignored issues
–
show
|
|||
257 | { |
||
258 | } |
||
259 | } |
||
260 | |||
261 | namespace Herrera\Version |
||
262 | |||
263 | { |
||
264 | use Herrera\Version\Exception\InvalidStringRepresentationException; |
||
0 ignored issues
–
show
|
|||
265 | |||
266 | /** |
||
267 | * Compares two Version instances. |
||
268 | * |
||
269 | * @author Kevin Herrera <[email protected]> |
||
270 | */ |
||
271 | class Comparator |
||
0 ignored issues
–
show
|
|||
272 | { |
||
273 | /** |
||
274 | * The version is equal to another. |
||
275 | */ |
||
276 | const EQUAL_TO = 0; |
||
277 | |||
278 | /** |
||
279 | * The version is greater than another. |
||
280 | */ |
||
281 | const GREATER_THAN = 1; |
||
282 | |||
283 | /** |
||
284 | * The version is less than another. |
||
285 | */ |
||
286 | const LESS_THAN = -1; |
||
287 | |||
288 | /** |
||
289 | * Compares one version with another. |
||
290 | * |
||
291 | * @param Version $left The left version to compare. |
||
292 | * @param Version $right The right version to compare. |
||
293 | * |
||
294 | * @return integer Returns Comparator::EQUAL_TO if the two versions are |
||
295 | * equal. If the left version is less than the right |
||
296 | * version, Comparator::LESS_THAN is returned. If the left |
||
297 | * version is greater than the right version, |
||
298 | * Comparator::GREATER_THAN is returned. |
||
299 | */ |
||
300 | public static function compareTo(Version $left, Version $right) |
||
301 | { |
||
302 | switch (true) { |
||
303 | case ($left->getMajor() < $right->getMajor()): |
||
304 | return self::LESS_THAN; |
||
305 | case ($left->getMajor() > $right->getMajor()): |
||
306 | return self::GREATER_THAN; |
||
307 | case ($left->getMinor() > $right->getMinor()): |
||
308 | return self::GREATER_THAN; |
||
309 | case ($left->getMinor() < $right->getMinor()): |
||
310 | return self::LESS_THAN; |
||
311 | case ($left->getPatch() > $right->getPatch()): |
||
312 | return self::GREATER_THAN; |
||
313 | case ($left->getPatch() < $right->getPatch()): |
||
314 | return self::LESS_THAN; |
||
315 | // @codeCoverageIgnoreStart |
||
316 | } |
||
317 | // @codeCoverageIgnoreEnd |
||
318 | |||
319 | return self::compareIdentifiers( |
||
320 | $left->getPreRelease(), |
||
321 | $right->getPreRelease() |
||
322 | ); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Checks if the left version is equal to the right. |
||
327 | * |
||
328 | * @param Version $left The left version to compare. |
||
329 | * @param Version $right The right version to compare. |
||
330 | * |
||
331 | * @return boolean TRUE if the left version is equal to the right, FALSE |
||
332 | * if not. |
||
333 | */ |
||
334 | public static function isEqualTo(Version $left, Version $right) |
||
335 | { |
||
336 | return (self::EQUAL_TO === self::compareTo($left, $right)); |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Checks if the left version is greater than the right. |
||
341 | * |
||
342 | * @param Version $left The left version to compare. |
||
343 | * @param Version $right The right version to compare. |
||
344 | * |
||
345 | * @return boolean TRUE if the left version is greater than the right, |
||
346 | * FALSE if not. |
||
347 | */ |
||
348 | public static function isGreaterThan(Version $left, Version $right) |
||
349 | { |
||
350 | return (self::GREATER_THAN === self::compareTo($left, $right)); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Checks if the left version is less than the right. |
||
355 | * |
||
356 | * @param Version $left The left version to compare. |
||
357 | * @param Version $right The right version to compare. |
||
358 | * |
||
359 | * @return boolean TRUE if the left version is less than the right, |
||
360 | * FALSE if not. |
||
361 | */ |
||
362 | public static function isLessThan(Version $left, Version $right) |
||
363 | { |
||
364 | return (self::LESS_THAN === self::compareTo($left, $right)); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Compares the identifier components of the left and right versions. |
||
369 | * |
||
370 | * @param array $left The left identifiers. |
||
371 | * @param array $right The right identifiers. |
||
372 | * |
||
373 | * @return integer Returns Comparator::EQUAL_TO if the two identifiers are |
||
374 | * equal. If the left identifiers is less than the right |
||
375 | * identifiers, Comparator::LESS_THAN is returned. If the |
||
376 | * left identifiers is greater than the right identifiers, |
||
377 | * Comparator::GREATER_THAN is returned. |
||
378 | */ |
||
379 | public static function compareIdentifiers(array $left, array $right) |
||
380 | { |
||
381 | if (!empty($left) && empty($right)) { |
||
382 | return self::LESS_THAN; |
||
383 | } elseif (empty($left) && !empty($right)) { |
||
384 | return self::GREATER_THAN; |
||
385 | } |
||
386 | |||
387 | $l = $left; |
||
388 | $r = $right; |
||
389 | $x = self::GREATER_THAN; |
||
390 | $y = self::LESS_THAN; |
||
391 | |||
392 | if (count($l) < count($r)) { |
||
393 | $l = $right; |
||
394 | $r = $left; |
||
395 | $x = self::LESS_THAN; |
||
396 | $y = self::GREATER_THAN; |
||
397 | } |
||
398 | |||
399 | foreach (array_keys($l) as $i) { |
||
400 | if (!isset($r[$i])) { |
||
401 | return $x; |
||
402 | } |
||
403 | |||
404 | if ($l[$i] === $r[$i]) { |
||
405 | continue; |
||
406 | } |
||
407 | |||
408 | View Code Duplication | if (true === ($li = (false != preg_match('/^\d+$/', $l[$i])))) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
409 | $l[$i] = intval($l[$i]); |
||
410 | } |
||
411 | |||
412 | View Code Duplication | if (true === ($ri = (false != preg_match('/^\d+$/', $r[$i])))) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
413 | $r[$i] = intval($r[$i]); |
||
414 | } |
||
415 | |||
416 | if ($li && $ri) { |
||
417 | return ($l[$i] > $r[$i]) ? $x : $y; |
||
418 | } elseif (!$li && $ri) { |
||
419 | return $x; |
||
420 | } elseif ($li && !$ri) { |
||
421 | return $y; |
||
422 | } |
||
423 | |||
424 | return strcmp($l[$i], $r[$i]); |
||
425 | } |
||
426 | |||
427 | return self::EQUAL_TO; |
||
428 | } |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Dumps the Version instance to a variety of formats. |
||
433 | * |
||
434 | * @author Kevin Herrera <[email protected]> |
||
435 | */ |
||
436 | class Dumper |
||
0 ignored issues
–
show
|
|||
437 | { |
||
438 | /** |
||
439 | * Returns the components of a Version instance. |
||
440 | * |
||
441 | * @param Version $version A version. |
||
442 | * |
||
443 | * @return array The components. |
||
444 | */ |
||
445 | public static function toComponents(Version $version) |
||
446 | { |
||
447 | return array( |
||
448 | Parser::MAJOR => $version->getMajor(), |
||
449 | Parser::MINOR => $version->getMinor(), |
||
450 | Parser::PATCH => $version->getPatch(), |
||
451 | Parser::PRE_RELEASE => $version->getPreRelease(), |
||
452 | Parser::BUILD => $version->getBuild() |
||
453 | ); |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Returns the string representation of a Version instance. |
||
458 | * |
||
459 | * @param Version $version A version. |
||
460 | * |
||
461 | * @return string The string representation. |
||
462 | */ |
||
463 | public static function toString(Version $version) |
||
464 | { |
||
465 | return sprintf( |
||
466 | '%d.%d.%d%s%s', |
||
467 | $version->getMajor(), |
||
468 | $version->getMinor(), |
||
469 | $version->getPatch(), |
||
470 | $version->getPreRelease() |
||
471 | ? '-' . join('.', $version->getPreRelease()) |
||
472 | : '', |
||
473 | $version->getBuild() |
||
474 | ? '+' . join('.', $version->getBuild()) |
||
475 | : '' |
||
476 | ); |
||
477 | } |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Parses the string representation of a version number. |
||
482 | * |
||
483 | * @author Kevin Herrera <[email protected]> |
||
484 | */ |
||
485 | class Parser |
||
0 ignored issues
–
show
|
|||
486 | { |
||
487 | /** |
||
488 | * The build metadata component. |
||
489 | */ |
||
490 | const BUILD = 'build'; |
||
491 | |||
492 | /** |
||
493 | * The major version number component. |
||
494 | */ |
||
495 | const MAJOR = 'major'; |
||
496 | |||
497 | /** |
||
498 | * The minor version number component. |
||
499 | */ |
||
500 | const MINOR = 'minor'; |
||
501 | |||
502 | /** |
||
503 | * The patch version number component. |
||
504 | */ |
||
505 | const PATCH = 'patch'; |
||
506 | |||
507 | /** |
||
508 | * The pre-release version number component. |
||
509 | */ |
||
510 | const PRE_RELEASE = 'pre'; |
||
511 | |||
512 | /** |
||
513 | * Returns a Version builder for the string representation. |
||
514 | * |
||
515 | * @param string $version The string representation. |
||
516 | * |
||
517 | * @return Builder A Version builder. |
||
518 | */ |
||
519 | public static function toBuilder($version) |
||
520 | { |
||
521 | return Builder::create()->importComponents( |
||
522 | self::toComponents($version) |
||
523 | ); |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * Returns the components of the string representation. |
||
528 | * |
||
529 | * @param string $version The string representation. |
||
530 | * |
||
531 | * @return array The components of the version. |
||
532 | * |
||
533 | * @throws InvalidStringRepresentationException If the string representation |
||
534 | * is invalid. |
||
535 | */ |
||
536 | public static function toComponents($version) |
||
537 | { |
||
538 | if (!Validator::isVersion($version)) { |
||
539 | throw new InvalidStringRepresentationException($version); |
||
540 | } |
||
541 | |||
542 | View Code Duplication | if (false !== strpos($version, '+')) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
543 | list($version, $build) = explode('+', $version); |
||
544 | |||
545 | $build = explode('.', $build); |
||
546 | } |
||
547 | |||
548 | View Code Duplication | if (false !== strpos($version, '-')) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
549 | list($version, $pre) = explode('-', $version); |
||
550 | |||
551 | $pre = explode('.', $pre); |
||
552 | } |
||
553 | |||
554 | list( |
||
555 | $major, |
||
556 | $minor, |
||
557 | $patch |
||
558 | ) = explode('.', $version); |
||
559 | |||
560 | return array( |
||
561 | self::MAJOR => intval($major), |
||
562 | self::MINOR => intval($minor), |
||
563 | self::PATCH => intval($patch), |
||
564 | self::PRE_RELEASE => isset($pre) ? $pre : array(), |
||
565 | self::BUILD => isset($build) ? $build : array(), |
||
566 | ); |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * Returns a Version instance for the string representation. |
||
571 | * |
||
572 | * @param string $version The string representation. |
||
573 | * |
||
574 | * @return Version A Version instance. |
||
575 | */ |
||
576 | public static function toVersion($version) |
||
577 | { |
||
578 | $components = self::toComponents($version); |
||
579 | |||
580 | return new Version( |
||
581 | $components['major'], |
||
0 ignored issues
–
show
It seems like
$components['major'] can also be of type array ; however, Herrera\Version\Version::__construct() does only seem to accept integer , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
582 | $components['minor'], |
||
0 ignored issues
–
show
It seems like
$components['minor'] can also be of type array ; however, Herrera\Version\Version::__construct() does only seem to accept integer , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
583 | $components['patch'], |
||
0 ignored issues
–
show
It seems like
$components['patch'] can also be of type array ; however, Herrera\Version\Version::__construct() does only seem to accept integer , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
584 | $components['pre'], |
||
0 ignored issues
–
show
It seems like
$components['pre'] can also be of type integer ; however, Herrera\Version\Version::__construct() does only seem to accept array , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
585 | $components['build'] |
||
0 ignored issues
–
show
It seems like
$components['build'] can also be of type integer ; however, Herrera\Version\Version::__construct() does only seem to accept array , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
586 | ); |
||
587 | } |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * Validates version information. |
||
592 | * |
||
593 | * @author Kevin Herrera <[email protected]> |
||
594 | */ |
||
595 | class Validator |
||
0 ignored issues
–
show
|
|||
596 | { |
||
597 | /** |
||
598 | * The regular expression for a valid identifier. |
||
599 | */ |
||
600 | const IDENTIFIER_REGEX = '/^[0-9A-Za-z\-]+$/'; |
||
601 | |||
602 | /** |
||
603 | * The regular expression for a valid semantic version number. |
||
604 | */ |
||
605 | const VERSION_REGEX = '/^\d+\.\d+\.\d+(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/'; |
||
606 | |||
607 | /** |
||
608 | * Checks if a identifier is valid. |
||
609 | * |
||
610 | * @param string $identifier A identifier. |
||
611 | * |
||
612 | * @return boolean TRUE if the identifier is valid, FALSE If not. |
||
613 | */ |
||
614 | public static function isIdentifier($identifier) |
||
615 | { |
||
616 | return (true == preg_match(self::IDENTIFIER_REGEX, $identifier)); |
||
0 ignored issues
–
show
|
|||
617 | } |
||
618 | |||
619 | /** |
||
620 | * Checks if a number is a valid version number. |
||
621 | * |
||
622 | * @param integer $number A number. |
||
623 | * |
||
624 | * @return boolean TRUE if the number is valid, FALSE If not. |
||
625 | */ |
||
626 | public static function isNumber($number) |
||
627 | { |
||
628 | return (true == preg_match('/^\d+$/', $number)); |
||
0 ignored issues
–
show
|
|||
629 | } |
||
630 | |||
631 | /** |
||
632 | * Checks if the string representation of a version number is valid. |
||
633 | * |
||
634 | * @param string $version The string representation. |
||
635 | * |
||
636 | * @return boolean TRUE if the string representation is valid, FALSE if not. |
||
637 | */ |
||
638 | public static function isVersion($version) |
||
639 | { |
||
640 | return (true == preg_match(self::VERSION_REGEX, $version)); |
||
0 ignored issues
–
show
|
|||
641 | } |
||
642 | } |
||
643 | |||
644 | /** |
||
645 | * Stores and returns the version information. |
||
646 | * |
||
647 | * @author Kevin Herrera <[email protected]> |
||
648 | */ |
||
649 | class Version |
||
0 ignored issues
–
show
|
|||
650 | { |
||
651 | /** |
||
652 | * The build metadata identifiers. |
||
653 | * |
||
654 | * @var array |
||
655 | */ |
||
656 | protected $build; |
||
657 | |||
658 | /** |
||
659 | * The major version number. |
||
660 | * |
||
661 | * @var integer |
||
662 | */ |
||
663 | protected $major; |
||
664 | |||
665 | /** |
||
666 | * The minor version number. |
||
667 | * |
||
668 | * @var integer |
||
669 | */ |
||
670 | protected $minor; |
||
671 | |||
672 | /** |
||
673 | * The patch version number. |
||
674 | * |
||
675 | * @var integer |
||
676 | */ |
||
677 | protected $patch; |
||
678 | |||
679 | /** |
||
680 | * The pre-release version identifiers. |
||
681 | * |
||
682 | * @var array |
||
683 | */ |
||
684 | protected $preRelease; |
||
685 | |||
686 | /** |
||
687 | * Sets the version information. |
||
688 | * |
||
689 | * @param integer $major The major version number. |
||
690 | * @param integer $minor The minor version number. |
||
691 | * @param integer $patch The patch version number. |
||
692 | * @param array $pre The pre-release version identifiers. |
||
693 | * @param array $build The build metadata identifiers. |
||
694 | */ |
||
695 | public function __construct( |
||
696 | $major = 0, |
||
697 | $minor = 0, |
||
698 | $patch = 0, |
||
699 | array $pre = array(), |
||
700 | array $build = array() |
||
701 | ) { |
||
702 | $this->build = $build; |
||
703 | $this->major = $major; |
||
704 | $this->minor = $minor; |
||
705 | $this->patch = $patch; |
||
706 | $this->preRelease = $pre; |
||
707 | } |
||
708 | |||
709 | /** |
||
710 | * Returns the build metadata identifiers. |
||
711 | * |
||
712 | * @return array The build metadata identifiers. |
||
713 | */ |
||
714 | public function getBuild() |
||
715 | { |
||
716 | return $this->build; |
||
717 | } |
||
718 | |||
719 | /** |
||
720 | * Returns the major version number. |
||
721 | * |
||
722 | * @return integer The major version number. |
||
723 | */ |
||
724 | public function getMajor() |
||
725 | { |
||
726 | return $this->major; |
||
727 | } |
||
728 | |||
729 | /** |
||
730 | * Returns the minor version number. |
||
731 | * |
||
732 | * @return integer The minor version number. |
||
733 | */ |
||
734 | public function getMinor() |
||
735 | { |
||
736 | return $this->minor; |
||
737 | } |
||
738 | |||
739 | /** |
||
740 | * Returns the patch version number. |
||
741 | * |
||
742 | * @return integer The patch version number. |
||
743 | */ |
||
744 | public function getPatch() |
||
745 | { |
||
746 | return $this->patch; |
||
747 | } |
||
748 | |||
749 | /** |
||
750 | * Returns the pre-release version identifiers. |
||
751 | * |
||
752 | * @return array The pre-release version identifiers. |
||
753 | */ |
||
754 | public function getPreRelease() |
||
755 | { |
||
756 | return $this->preRelease; |
||
757 | } |
||
758 | } |
||
759 | } |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state