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; |
|
|
|
|
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
|
|
|
|
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: