1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of WebHelper Parser. |
5
|
|
|
* |
6
|
|
|
* (c) James <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WebHelper\Parser\Directive; |
13
|
|
|
|
14
|
|
|
use WebHelper\Parser\Server\ServerInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Describes a block directive instance or a context. |
18
|
|
|
* |
19
|
|
|
* a BlockDirective is an ordered list of other directives set in a context. |
20
|
|
|
* |
21
|
|
|
* @author James <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class BlockDirective extends Directive implements DirectiveInterface |
24
|
|
|
{ |
25
|
|
|
/** @var array orderd list of sub directives */ |
26
|
|
|
private $directives = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Adds a Directive at the end of the list. |
30
|
|
|
* |
31
|
|
|
* @param DirectiveInterface $directive a directive to add |
32
|
|
|
*/ |
33
|
9 |
|
public function add(DirectiveInterface $directive) |
34
|
|
|
{ |
35
|
9 |
|
$this->directives[] = $directive; |
36
|
|
|
|
37
|
9 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Confirms if the directive contains a specified directive. |
42
|
|
|
* |
43
|
|
|
* @param string $name the directive for which to check existence |
44
|
|
|
* |
45
|
|
|
* @return bool true if the sub-directive exists, false otherwise |
46
|
|
|
*/ |
47
|
1 |
|
public function hasDirective($name) |
48
|
|
|
{ |
49
|
1 |
|
$inSubDirective = false; |
50
|
|
|
|
51
|
1 |
|
foreach ($this->directives as $directive) { |
52
|
1 |
|
if ($directive->getName() == $name) { |
53
|
1 |
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$inSubDirective = $this->hasInnerDirective($name, $inSubDirective, $directive); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
1 |
|
return $inSubDirective; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Looks into sub directives to confirm if the actual contains a specified directive. |
64
|
|
|
* |
65
|
|
|
* @param string $name the directive for which to check existence |
66
|
|
|
* @param bool $inSubDirective the actual state |
67
|
|
|
* @param DirectiveInterface $directive the sub directive to look into |
68
|
|
|
* |
69
|
|
|
* @return bool true if the sub-directive exists, false otherwise |
70
|
|
|
*/ |
71
|
1 |
|
private function hasInnerDirective($name, $inSubDirective, DirectiveInterface $directive) |
72
|
|
|
{ |
73
|
1 |
|
if (!$directive->isSimple()) { |
74
|
1 |
|
$inSubDirective = $inSubDirective || $directive->hasDirective($name); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
1 |
|
return $inSubDirective; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Confirms if the directive is simple. |
82
|
|
|
* |
83
|
|
|
* Block directive can have sub directives |
84
|
|
|
* |
85
|
|
|
* @return bool true if the directive is simple, false otherwise |
86
|
|
|
*/ |
87
|
1 |
|
public function isSimple() |
88
|
|
|
{ |
89
|
1 |
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Dumps the directive respecting a server syntax. |
94
|
|
|
* |
95
|
|
|
* @param ServerInterface $server a server instance |
96
|
|
|
* @param int $spaces the indentation spaces |
97
|
|
|
* |
98
|
|
|
* @return string the dumped directive |
99
|
|
|
*/ |
100
|
1 |
|
public function dump(ServerInterface $server, $spaces = 0) |
101
|
|
|
{ |
102
|
1 |
|
$config = ''; |
103
|
|
|
|
104
|
1 |
|
if (!$this->isMainContext()) { |
105
|
1 |
|
$value = $this->getValue() ? ' '.$this->getValue() : ''; |
106
|
1 |
|
$config .= str_repeat(' ', $spaces).sprintf( |
107
|
1 |
|
$server->getDumperStartDirective(), |
108
|
1 |
|
$this->getName(), |
109
|
|
|
$value |
110
|
1 |
|
).PHP_EOL; |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
|
foreach ($this->directives as $directive) { |
114
|
1 |
|
$config .= $directive->dump($server, $spaces + ($this->isMainContext() ? 0 : 4)); |
115
|
1 |
|
} |
116
|
|
|
|
117
|
1 |
|
if (!$this->isMainContext()) { |
118
|
1 |
|
$config .= str_repeat(' ', $spaces).sprintf( |
119
|
1 |
|
$server->getDumperEndDirective(), |
120
|
1 |
|
$this->getName(), |
121
|
|
|
$value |
|
|
|
|
122
|
1 |
|
).PHP_EOL; |
123
|
1 |
|
} |
124
|
|
|
|
125
|
1 |
|
return $config; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Confirms if a Block directive is a 'main' context. |
130
|
|
|
* |
131
|
|
|
* @return bool true if the name of the block directive is 'main', false otherwise |
132
|
|
|
*/ |
133
|
1 |
|
private function isMainContext() |
134
|
|
|
{ |
135
|
1 |
|
return 'main' == $this->getName(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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: