These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Deployer\Component\PharUpdate\Version; |
||
4 | |||
5 | use Deployer\Component\PharUpdate\Version\Exception\InvalidStringRepresentationException; |
||
6 | |||
7 | /** |
||
8 | * Parses the string representation of a version number. |
||
9 | * |
||
10 | * @author Kevin Herrera <[email protected]> |
||
11 | */ |
||
12 | class Parser |
||
13 | { |
||
14 | /** |
||
15 | * The build metadata component. |
||
16 | */ |
||
17 | const BUILD = 'build'; |
||
18 | |||
19 | /** |
||
20 | * The major version number component. |
||
21 | */ |
||
22 | const MAJOR = 'major'; |
||
23 | |||
24 | /** |
||
25 | * The minor version number component. |
||
26 | */ |
||
27 | const MINOR = 'minor'; |
||
28 | |||
29 | /** |
||
30 | * The patch version number component. |
||
31 | */ |
||
32 | const PATCH = 'patch'; |
||
33 | |||
34 | /** |
||
35 | * The pre-release version number component. |
||
36 | */ |
||
37 | const PRE_RELEASE = 'pre'; |
||
38 | |||
39 | /** |
||
40 | * Returns a Version builder for the string representation. |
||
41 | * |
||
42 | * @param string $version The string representation. |
||
43 | * |
||
44 | * @return Builder A Version builder. |
||
45 | */ |
||
46 | public static function toBuilder($version) |
||
47 | { |
||
48 | return Builder::create()->importComponents( |
||
49 | self::toComponents($version) |
||
50 | ); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Returns the components of the string representation. |
||
55 | * |
||
56 | * @param string $version The string representation. |
||
57 | * |
||
58 | * @return array The components of the version. |
||
59 | * |
||
60 | * @throws InvalidStringRepresentationException If the string representation |
||
61 | * is invalid. |
||
62 | */ |
||
63 | public static function toComponents($version) |
||
64 | { |
||
65 | if (!Validator::isVersion($version)) { |
||
66 | throw new InvalidStringRepresentationException($version); |
||
67 | } |
||
68 | |||
69 | View Code Duplication | if (false !== strpos($version, '+')) { |
|
70 | list($version, $build) = explode('+', $version); |
||
71 | |||
72 | $build = explode('.', $build); |
||
73 | } |
||
74 | |||
75 | View Code Duplication | if (false !== strpos($version, '-')) { |
|
76 | list($version, $pre) = explode('-', $version); |
||
77 | |||
78 | $pre = explode('.', $pre); |
||
79 | } |
||
80 | |||
81 | list( |
||
82 | $major, |
||
83 | $minor, |
||
84 | $patch |
||
85 | ) = explode('.', $version); |
||
86 | |||
87 | return array( |
||
88 | self::MAJOR => intval($major), |
||
89 | self::MINOR => intval($minor), |
||
90 | self::PATCH => intval($patch), |
||
91 | self::PRE_RELEASE => isset($pre) ? $pre : array(), |
||
92 | self::BUILD => isset($build) ? $build : array(), |
||
93 | ); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Returns a Version instance for the string representation. |
||
98 | * |
||
99 | * @param string $version The string representation. |
||
100 | * |
||
101 | * @return Version A Version instance. |
||
102 | */ |
||
103 | public static function toVersion($version) |
||
104 | { |
||
105 | $components = self::toComponents($version); |
||
106 | |||
107 | return new Version( |
||
108 | $components['major'], |
||
0 ignored issues
–
show
|
|||
109 | $components['minor'], |
||
0 ignored issues
–
show
It seems like
$components['minor'] can also be of type array ; however, Deployer\Component\PharU...\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.
Loading history...
|
|||
110 | $components['patch'], |
||
0 ignored issues
–
show
It seems like
$components['patch'] can also be of type array ; however, Deployer\Component\PharU...\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.
Loading history...
|
|||
111 | $components['pre'], |
||
0 ignored issues
–
show
It seems like
$components['pre'] can also be of type integer ; however, Deployer\Component\PharU...\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.
Loading history...
|
|||
112 | $components['build'] |
||
0 ignored issues
–
show
It seems like
$components['build'] can also be of type integer ; however, Deployer\Component\PharU...\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.
Loading history...
|
|||
113 | ); |
||
114 | } |
||
115 | } |
||
116 |
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.