GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PDOMySqlExtensionCheck   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 79
ccs 22
cts 25
cp 0.88
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 17 4
A getSuccessMessage() 0 6 1
B getFailureMessage() 0 12 5
A isOptional() 0 4 1
1
<?php
2
/**
3
 * This file is part of the Gerrie package.
4
 *
5
 * (c) Andreas Grunwald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Gerrie\Check;
12
13
/**
14
 * Check if PHP extensions "PDO" and "pdo_mysql" are installed.
15
 *
16
 * @author Andreas Grunwald <[email protected]>
17
 */
18
class PDOMySqlExtensionCheck implements CheckInterface
19
{
20
    /**
21
     * Version of PDO extension
22
     *
23
     * @var string
24
     */
25
    private $pdoVersion = '';
26
27
    /**
28
     * Version of pdo_mysql extension
29
     *
30
     * @var string
31
     */
32
    private $pdoMySqlVersion = '';
33
34
    /**
35
     * Executes the check itselfs
36
     *
37
     * @return boolean
38
     */
39 1
    public function check()
40
    {
41 1
        $pdoResult = extension_loaded('PDO');
42 1
        $pdoMySqlResult = extension_loaded('pdo_mysql');
43
44 1
        $result = ($pdoResult === true && $pdoMySqlResult === true);
45
46 1
        if ($pdoResult === true) {
47 1
            $this->pdoVersion = phpversion('PDO');
48 1
        }
49
50 1
        if ($pdoMySqlResult === true) {
51 1
            $this->pdoMySqlVersion = phpversion('pdo_mysql');
52 1
        }
53
54 1
        return $result;
55
    }
56
57
    /**
58
     * Returns the message, if the check succeeded
59
     *
60
     * @return string
61
     */
62 1
    public function getSuccessMessage()
63
    {
64 1
        $message = 'PHP-Extensions "PDO" (v%s) and "pdo_mysql" (v%s) are installed and usable.';
65 1
        $message = sprintf($message, $this->pdoVersion, $this->pdoMySqlVersion);
66 1
        return $message;
67
    }
68
69
    /**
70
     * Returns the message, if the check fails
71
     *
72
     * @return string
73
     */
74 1
    public function getFailureMessage()
75
    {
76 1
        if (!$this->pdoVersion && !$this->pdoMySqlVersion) {
77 1
            $message = 'PHP-Extensions "PDO" and "pdo_mysql" are not installed. Please install both.';
78
79 1
        } elseif ($this->pdoVersion && !$this->pdoMySqlVersion) {
80
            $message = 'PHP-Extension "PDO" (v%s) is installed, but "pdo_mysql" not. Please install it.';
81
            $message = sprintf($message, $this->pdoVersion);
82
        }
83
84 1
        return $message;
0 ignored issues
show
Bug introduced by
The variable $message 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...
85
    }
86
87
    /**
88
     * Returns if this check is optional or required.
89
     *
90
     * @return bool
91
     */
92 1
    public function isOptional()
93
    {
94 1
        return false;
95
    }
96
}
97