|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Error related utilities. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
declare(strict_types=1); |
|
7
|
|
|
|
|
8
|
|
|
namespace PhpMyAdmin\SqlParser\Utils; |
|
9
|
|
|
|
|
10
|
|
|
use PhpMyAdmin\SqlParser\Exceptions\LexerException; |
|
11
|
|
|
use PhpMyAdmin\SqlParser\Exceptions\ParserException; |
|
12
|
|
|
use PhpMyAdmin\SqlParser\Lexer; |
|
13
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
|
14
|
|
|
use function htmlspecialchars; |
|
15
|
|
|
use function sprintf; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Error related utilities. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Error |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Gets the errors of a lexer and a parser. |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $objs objects from where the errors will be extracted |
|
26
|
|
|
* |
|
27
|
|
|
* @return array Each element of the array represents an error. |
|
28
|
|
|
* `$err[0]` holds the error message. |
|
29
|
|
|
* `$err[1]` holds the error code. |
|
30
|
|
|
* `$err[2]` holds the string that caused the issue. |
|
31
|
|
|
* `$err[3]` holds the position of the string. |
|
32
|
|
|
* (i.e. `[$msg, $code, $str, $pos]`) |
|
33
|
|
|
*/ |
|
34
|
32 |
|
public static function get($objs) |
|
35
|
|
|
{ |
|
36
|
32 |
|
$ret = []; |
|
37
|
|
|
|
|
38
|
32 |
|
foreach ($objs as $obj) { |
|
39
|
32 |
|
if ($obj instanceof Lexer) { |
|
40
|
|
|
/** @var LexerException $err */ |
|
41
|
32 |
|
foreach ($obj->errors as $err) { |
|
42
|
4 |
|
$ret[] = [ |
|
43
|
4 |
|
$err->getMessage(), |
|
44
|
4 |
|
$err->getCode(), |
|
45
|
4 |
|
$err->ch, |
|
46
|
4 |
|
$err->pos, |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
32 |
|
} elseif ($obj instanceof Parser) { |
|
50
|
|
|
/** @var ParserException $err */ |
|
51
|
32 |
|
foreach ($obj->errors as $err) { |
|
52
|
20 |
|
$ret[] = [ |
|
53
|
20 |
|
$err->getMessage(), |
|
54
|
20 |
|
$err->getCode(), |
|
55
|
20 |
|
$err->token->token, |
|
56
|
20 |
|
$err->token->position, |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
32 |
|
return $ret; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Formats the specified errors. |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $errors the errors to be formatted |
|
69
|
|
|
* @param string $format The format of an error. |
|
70
|
|
|
* '$1$d' is replaced by the position of this error. |
|
71
|
|
|
* '$2$s' is replaced by the error message. |
|
72
|
|
|
* '$3$d' is replaced by the error code. |
|
73
|
|
|
* '$4$s' is replaced by the string that caused the |
|
74
|
|
|
* issue. |
|
75
|
|
|
* '$5$d' is replaced by the position of the string. |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
20 |
|
public static function format( |
|
80
|
|
|
$errors, |
|
81
|
|
|
$format = '#%1$d: %2$s (near "%4$s" at position %5$d)' |
|
82
|
|
|
) { |
|
83
|
20 |
|
$ret = []; |
|
84
|
|
|
|
|
85
|
20 |
|
$i = 0; |
|
86
|
20 |
|
foreach ($errors as $key => $err) { |
|
87
|
20 |
|
$ret[$key] = sprintf( |
|
88
|
20 |
|
$format, |
|
89
|
20 |
|
++$i, |
|
90
|
20 |
|
$err[0], |
|
91
|
20 |
|
$err[1], |
|
92
|
20 |
|
htmlspecialchars((string) $err[2]), |
|
93
|
20 |
|
$err[3] |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
20 |
|
return $ret; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|