1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Konfig |
5
|
|
|
* |
6
|
|
|
* Yet another simple configuration loader library. |
7
|
|
|
* |
8
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
9
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
10
|
|
|
* @link https://xeriab.github.io/projects/konfig |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Exen\Konfig\Exception; |
14
|
|
|
|
15
|
|
|
// class ParseException extends ErrorException |
16
|
|
|
class ParseException extends RuntimeException |
17
|
|
|
{ |
18
|
|
|
private $parsedFile; |
19
|
|
|
private $parsedLine; |
20
|
|
|
private $snippet; |
21
|
|
|
private $rawMessage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
* |
26
|
|
|
* @param array $error The error array |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $error) |
29
|
|
|
{ |
30
|
|
|
$message = isset($error['message']) ? $error['message'] : 'There was an error parsing the file'; |
31
|
|
|
$code = isset($error['code']) ? $error['code'] : 0; |
|
|
|
|
32
|
|
|
$severity = isset($error['type']) ? $error['type'] : 1; |
|
|
|
|
33
|
|
|
$fileName = isset($error['file']) ? $error['file'] : __FILE__; |
34
|
|
|
$lineNo = isset($error['line']) ? $error['line'] : __LINE__; |
35
|
|
|
$exception = isset($error['exception']) ? $error['exception'] : null; |
|
|
|
|
36
|
|
|
$snippet = isset($error['snippet']) ? $error['snippet'] : null; |
37
|
|
|
$previous = isset($error['previous']) ? $error['previous'] : null; |
38
|
|
|
|
39
|
|
|
$this->parsedFile = $fileName; |
40
|
|
|
$this->parsedLine = $lineNo; |
41
|
|
|
$this->snippet = $snippet; |
42
|
|
|
$this->rawMessage = $message; |
43
|
|
|
|
44
|
|
|
$this->updateRepr(); |
45
|
|
|
|
46
|
|
|
parent::__construct( |
47
|
|
|
$this->message, |
48
|
|
|
0, |
49
|
|
|
$previous |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
// parent::__construct( |
53
|
|
|
// $message, |
54
|
|
|
// $code, |
55
|
|
|
// $severity, |
56
|
|
|
// $fileName, |
57
|
|
|
// $lineNo, |
58
|
|
|
// $exception |
59
|
|
|
// ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Gets the snippet of code near the error. |
64
|
|
|
* |
65
|
|
|
* @return string The snippet of code |
66
|
|
|
*/ |
67
|
|
|
public function getSnippet() |
68
|
|
|
{ |
69
|
|
|
return $this->snippet; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets the snippet of code near the error. |
74
|
|
|
* |
75
|
|
|
* @param string $snippet The code snippet |
76
|
|
|
*/ |
77
|
|
|
public function setSnippet($snippet) |
78
|
|
|
{ |
79
|
|
|
$this->snippet = $snippet; |
80
|
|
|
|
81
|
|
|
$this->updateRepr(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets the filename where the error occurred. |
86
|
|
|
* |
87
|
|
|
* This method returns null if a string is parsed. |
88
|
|
|
* |
89
|
|
|
* @return string The filename |
90
|
|
|
*/ |
91
|
|
|
public function getParsedFile() |
92
|
|
|
{ |
93
|
|
|
return $this->parsedFile; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Sets the filename where the error occurred. |
98
|
|
|
* |
99
|
|
|
* @param string $parsedFile The filename |
100
|
|
|
*/ |
101
|
|
|
public function setParsedFile(string $parsedFile = null) |
102
|
|
|
{ |
103
|
|
|
$this->parsedFile = $parsedFile; |
104
|
|
|
|
105
|
|
|
$this->updateRepr(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Gets the line where the error occurred. |
110
|
|
|
* |
111
|
|
|
* @return int The file line |
112
|
|
|
*/ |
113
|
|
|
public function getParsedLine() |
114
|
|
|
{ |
115
|
|
|
return $this->parsedLine; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sets the line where the error occurred. |
120
|
|
|
* |
121
|
|
|
* @param int $parsedLine The file line |
122
|
|
|
*/ |
123
|
|
|
public function setParsedLine(int $parsedLine = 0) |
124
|
|
|
{ |
125
|
|
|
$this->parsedLine = $parsedLine; |
126
|
|
|
|
127
|
|
|
$this->updateRepr(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function updateRepr() |
131
|
|
|
{ |
132
|
|
|
$this->message = $this->rawMessage; |
133
|
|
|
|
134
|
|
|
$dot = false; |
135
|
|
|
|
136
|
|
|
if ('.' === substr($this->message, -1)) { |
137
|
|
|
$this->message = substr($this->message, 0, -1); |
138
|
|
|
$dot = true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (null !== $this->parsedFile) { |
142
|
|
|
$this->message .= sprintf( |
143
|
|
|
' in %s', |
144
|
|
|
json_encode($this->parsedFile, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($this->parsedLine >= 0) { |
149
|
|
|
$this->message .= sprintf(' at line %d', $this->parsedLine); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($this->snippet) { |
153
|
|
|
$this->message .= sprintf(' (near "%s")', $this->snippet); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($dot) { |
157
|
|
|
$this->message .= '.'; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// END OF ./src/Exception/ParseException.php FILE |
163
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.