ComposerVersionNormalizer::normalizeLevelCount()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 1
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The variable $suffix does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
62
		}
63
64
		return $version;
65
	}
66
}
67