1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* janitor (http://juliangut.com/janitor). |
5
|
|
|
* Effortless maintenance management. |
6
|
|
|
* |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @link https://github.com/juliangut/janitor |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Janitor\Watcher; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Environment variable maintenance status watcher. |
16
|
|
|
*/ |
17
|
|
|
class Environment implements WatcherInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Environment variable. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $vars; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Environment constructor. |
28
|
|
|
* |
29
|
|
|
* @param array|string $vars |
30
|
|
|
* @param mixed $value |
31
|
|
|
*/ |
32
|
|
|
public function __construct($vars, $value = null) |
33
|
|
|
{ |
34
|
|
|
if ($vars !== null && !is_array($vars)) { |
35
|
|
|
$vars = [$vars => $value]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (is_array($vars)) { |
39
|
|
|
foreach ($vars as $varName => $varValue) { |
40
|
|
|
$this->addVariable($varName, $varValue); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set environment variable. |
47
|
|
|
* |
48
|
|
|
* @param string $var |
49
|
|
|
* @param mixed $value |
50
|
|
|
* |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
|
public function addVariable($var, $value = null) |
54
|
|
|
{ |
55
|
|
|
if (trim($var) !== '') { |
56
|
|
|
$this->vars[trim($var)] = $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function isActive() |
66
|
|
|
{ |
67
|
|
|
if (!count($this->vars)) { |
68
|
|
|
throw new \RuntimeException('At least one environment variable must be defined'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($this->vars as $variable => $value) { |
72
|
|
|
if ($value === null && $this->isEnvVariableDefined($variable)) { |
73
|
|
|
return true; |
74
|
|
|
} elseif ($value !== null && $this->getEnvVariableValue($variable) === $value) { |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test for environment variable. |
84
|
|
|
* |
85
|
|
|
* @param string $var |
|
|
|
|
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
protected function isEnvVariableDefined($variable) |
90
|
|
|
{ |
91
|
|
|
return getenv($variable) !== false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get environment variable value. |
96
|
|
|
* |
97
|
|
|
* @param string $variable |
98
|
|
|
* |
99
|
|
|
* @return mixed |
|
|
|
|
100
|
|
|
*/ |
101
|
|
|
protected function getEnvVariableValue($variable) |
102
|
|
|
{ |
103
|
|
|
$value = getenv($variable); |
104
|
|
|
|
105
|
|
|
if ($value === false) { |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
switch (strtolower($value)) { |
110
|
|
|
case 'true': |
111
|
|
|
return true; |
112
|
|
|
|
113
|
|
|
case 'false': |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (preg_match('/^[\'"](.*)[\'"]$/', $value, $matches)) { |
118
|
|
|
return $matches[1]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $value; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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 methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.