Completed
Pull Request — master (#2278)
by ྅༻ Ǭɀħ
14:42
created

includes/class-mysql.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Pick the right DB class and return an instance
5
 *
6
 * @since 1.7
7
 * @param string $extension Optional: user defined choice
0 ignored issues
show
There is no parameter named $extension. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
8
 * @return class $ydb DB class instance
9
 */
10
function yourls_set_DB_driver( ) {
11
12
	// Auto-pick the driver. Priority: user defined, then PDO, then mysqli, then mysql
13
	if ( defined( 'YOURLS_DB_DRIVER' ) ) {
14
		$driver = strtolower( YOURLS_DB_DRIVER ); // accept 'MySQL', 'mySQL', etc
15
	} elseif ( extension_loaded( 'pdo_mysql' ) ) {
16
		$driver = 'pdo';
17
	} elseif ( extension_loaded( 'mysqli' ) ) {
18
		$driver = 'mysqli';
19
	} elseif ( extension_loaded( 'mysql' ) ) {
20
		$driver = 'mysql';
21
	} else {
22
		$driver = '';
23
	}
24
	
25
	// Set the new driver
26
	if ( in_array( $driver, array( 'mysql', 'mysqli', 'pdo' ) ) ) {
27
        $class = yourls_require_db_files( $driver );
28
	}
29
30
	global $ydb;
31
32
	if ( !class_exists( $class, false ) ) {
0 ignored issues
show
The variable $class 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...
33
		$ydb = new stdClass();
34
		yourls_die(
35
			yourls__( 'YOURLS requires the mysql, mysqli or pdo_mysql PHP extension. No extension found. Check your server config, or contact your host.' ),
36
			yourls__( 'Fatal error' ),
37
			503
38
		);
39
	}
40
	
41
	yourls_do_action( 'set_DB_driver', $driver );
42
		
43
	$ydb = new $class( YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST );
44
    $ydb->DB_driver = $driver;
45
46
	yourls_debug_log( "DB driver: $driver" );
47
}
48
49
/**
50
 * Load required DB class files
51
 *
52
 * This goes in its own function to allow easier unit tests
53
 *
54
 * @since 1.7.1
55
 * @param string $driver DB driver
56
 * @return string name of the DB class to instantiate
57
 */
58
function yourls_require_db_files( $driver ) {
59
    require_once( YOURLS_INC . '/ezSQL/ez_sql_core.php' );
60
    require_once( YOURLS_INC . '/ezSQL/ez_sql_core_yourls.php' );
61
    require_once( YOURLS_INC . '/ezSQL/ez_sql_' . $driver . '.php' );
62
    require_once( YOURLS_INC . '/ezSQL/ez_sql_' . $driver . '_yourls.php' );
63
    return 'ezSQL_' . $driver . '_yourls';
64
} 
65
66
/**
67
 * Connect to DB
68
 *
69
 * @since 1.0
70
 */
71
function yourls_db_connect() {
72
	global $ydb;
73
74
	if (   !defined( 'YOURLS_DB_USER' )
75
		or !defined( 'YOURLS_DB_PASS' )
76
		or !defined( 'YOURLS_DB_NAME' )
77
		or !defined( 'YOURLS_DB_HOST' )
78
	) yourls_die ( yourls__( 'Incorrect DB config, or could not connect to DB' ), yourls__( 'Fatal error' ), 503 );	
79
80
	// Are we standalone or in the WordPress environment?
81
	if ( class_exists( 'wpdb', false ) ) {
82
		/* TODO: should we deprecate this? Follow WP dev in that area */
83
		$ydb =  new wpdb( YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST );
84
	} else {
85
		yourls_set_DB_driver();
86
	}
87
	
88
	return $ydb;
89
}
90
91
/**
92
 * Return true if DB server is responding
93
 *
94
 * This function is supposed to be called right after yourls_get_all_options() has fired. It is not designed (yet) to
95
 * check for a responding server after several successful operation to check if the server has gone MIA
96
 *
97
 * @since 1.7.1
98
 */
99
function yourls_is_db_alive() {
100
    global $ydb;
101
    
102
    $alive = false;
0 ignored issues
show
$alive is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103
    switch( $ydb->DB_driver ) {
104
        case 'pdo' :
105
            $alive = isset( $ydb->dbh );
106
            break;
107
    
108
        case 'mysql' :
109
            $alive = ( isset( $ydb->dbh ) && false !== $ydb->dbh );
110
            break;
111
    
112
        case 'mysqli' :
113
            $alive = ( null == mysqli_connect_error() );
114
            break;
115
        
116
        // Custom DB driver & class : delegate check
117
        default:
118
            $alive = yourls_apply_filter( 'is_db_alive_custom', false );
119
    }
120
    
121
    return $alive;
122
}
123
124
/**
125
 * Die with a DB error message
126
 *
127
 * @TODO in version 1.8 : use a new localized string, specific to the problem (ie: "DB is dead")
128
 *
129
 * @since 1.7.1
130
 */
131
function yourls_db_dead() {
132
    // Use any /user/db_error.php file
133
    if( file_exists( YOURLS_USERDIR . '/db_error.php' ) ) {
134
        include_once( YOURLS_USERDIR . '/db_error.php' );
135
        die();
136
    }
137
138
    yourls_die( yourls__( 'Incorrect DB config, or could not connect to DB' ), yourls__( 'Fatal error' ), 503 );
139
}