Completed
Push — develop ( 82d5df...652db5 )
by
unknown
09:07
created

Wordlift_Install_Service::get_instance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Installs: Install Service.
4
 *
5
 * The Installation Service.
6
 *
7
 * @since      3.18.0
8
 * @package    Wordlift
9
 * @subpackage Wordlift/install
10
 */
11
12
/**
13
 * Define the {@link Wordlift_Install_Service} interface.
14
 *
15
 * @since      3.18.0
16
 * @package    Wordlift
17
 * @subpackage Wordlift/install
18
 */
19
class Wordlift_Install_Service {
20
21
	/**
22
	 * The singleton instance.
23
	 *
24
	 * @since  3.18.0
25
	 * @access private
26
	 * @var \Wordlift_Install_Service $instance A {@link Wordlift_Install_Service} instance.
27
	 */
28
	private static $instance;
29
30
31
	/**
32
	 * Wordlift_Install_Service constructor.
33
	 *
34
	 * @since 3.18.0
35
	 */
36
	public function __construct() {
37
38
		self::$instance = $this;
39
	}
40
41
	/**
42
	 * Get the singleton instance.
43
	 *
44
	 * @since 3.18.0
45
	 */
46
	public static function get_instance() {
47
48
		return self::$instance;
49
	}
50
51
	/**
52
	 * Loop thought all versions and install the updates.
53
	 *
54
	 * @since 3.18.0
55
	 *
56
	 * @return void
57
	 */
58
	public function install() {
59
60
		// Get the install services.
61
		$installs = array(
62
			new Wordlift_Install_1_0_0(),
63
			new Wordlift_Install_3_10_0(),
64
			new Wordlift_Install_3_12_0(),
65
			new Wordlift_Install_3_14_0(),
66
			new Wordlift_Install_3_15_0(),
67
		);
68
69
		/** @var Wordlift_Install $install */
70
		foreach ( $installs as $install ) {
71
			// Get the install version.
72
			$version = $install->get_version();
73
74
			if ( version_compare( $version, $this->get_current_version(), '>=' ) ) {
75
				// Install version.
76
				$install->install();
77
78
			}
79
		}
80
81
		// Bump the `wl_db_version`.
82
		update_option( 'wl_db_version', $version );
0 ignored issues
show
Bug introduced by
The variable $version 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...
83
	}
84
85
	/**
86
	 * Retrieve the current db version.
87
	 *
88
	 * @return type
89
	 */
90
	private function get_current_version() {
91
		return get_option( 'wl_db_version', '0.0.0' );
92
	}
93
94
}
95