1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @licence GNU GPL v2+ |
5
|
|
|
* @author Jeroen De Dauw < [email protected] > |
6
|
|
|
*/ |
7
|
|
|
class ComposerVersionNormalizer { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Ensures there is a dash in between the version and the stability suffix. |
11
|
|
|
* |
12
|
|
|
* Examples: |
13
|
|
|
* - 1.23RC => 1.23-RC |
14
|
|
|
* - 1.23alpha => 1.23-alpha |
15
|
|
|
* - 1.23alpha3 => 1.23-alpha3 |
16
|
|
|
* - 1.23-beta => 1.23-beta |
17
|
|
|
* |
18
|
|
|
* @param string $version |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
* @throws InvalidArgumentException |
22
|
|
|
*/ |
23
|
|
|
public function normalizeSuffix( $version ) { |
24
|
|
|
if ( !is_string( $version ) ) { |
25
|
|
|
throw new InvalidArgumentException( '$version must be a string' ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return preg_replace( '/^(\d[\d\.]*)([a-zA-Z]+)(\d*)$/', '$1-$2$3', $version, 1 ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Ensures the version has four levels. |
33
|
|
|
* Version suffixes are supported, as long as they start with a dash. |
34
|
|
|
* |
35
|
|
|
* Examples: |
36
|
|
|
* - 1.19 => 1.19.0.0 |
37
|
|
|
* - 1.19.2.3 => 1.19.2.3 |
38
|
|
|
* - 1.19-alpha => 1.19.0.0-alpha |
39
|
|
|
* - 1337 => 1337.0.0.0 |
40
|
|
|
* |
41
|
|
|
* @param string $version |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
* @throws InvalidArgumentException |
45
|
|
|
*/ |
46
|
|
|
public function normalizeLevelCount( $version ) { |
47
|
|
|
if ( !is_string( $version ) ) { |
48
|
|
|
throw new InvalidArgumentException( '$version must be a string' ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$dashPosition = strpos( $version, '-' ); |
52
|
|
|
|
53
|
|
|
if ( $dashPosition !== false ) { |
54
|
|
|
$suffix = substr( $version, $dashPosition ); |
55
|
|
|
$version = substr( $version, 0, $dashPosition ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$version = implode( '.', array_pad( explode( '.', $version ), 4, '0' ) ); |
59
|
|
|
|
60
|
|
|
if ( $dashPosition !== false ) { |
61
|
|
|
$version .= $suffix; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $version; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: