Skip to content

Commit 26f97bc

Browse files
committed
Share error handling in one base class
Signed-off-by: Michal Čihař <michal@cihar.com>
1 parent b084e76 commit 26f97bc

3 files changed

Lines changed: 56 additions & 54 deletions

File tree

src/Core.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* Defines the core helper infrastructure of the library.
5+
*
6+
* @package SqlParser
7+
*/
8+
namespace SqlParser;
9+
10+
11+
class Core
12+
{
13+
14+
/**
15+
* Whether errors should throw exceptions or just be stored.
16+
*
17+
* @var bool
18+
*
19+
* @see static::$errors
20+
*/
21+
public $strict = false;
22+
23+
/**
24+
* List of errors that occurred during lexing.
25+
*
26+
* Usually, the lexing does not stop once an error occurred because that
27+
* error might be false positive or a partial result (even a bad one)
28+
* might be needed.
29+
*
30+
* @var Exception[]
31+
*
32+
* @see Core::error()
33+
*/
34+
public $errors = array();
35+
36+
/**
37+
* Creates a new error log.
38+
*
39+
* @param Exception $error The error exception.
40+
*
41+
* @throws Exception Throws the exception, if strict mode is enabled.
42+
*
43+
* @return void
44+
*/
45+
public function error($error)
46+
{
47+
if ($this->strict) {
48+
throw $error;
49+
}
50+
$this->errors[] = $error;
51+
}
52+
}

src/Lexer.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
4242
* @see Context
4343
*/
44-
class Lexer
44+
class Lexer extends Core
4545
{
4646

4747
/**
@@ -79,15 +79,6 @@ class Lexer
7979
'parseSymbol', 'parseKeyword', 'parseLabel', 'parseUnknown'
8080
);
8181

82-
/**
83-
* Whether errors should throw exceptions or just be stored.
84-
*
85-
* @var bool
86-
*
87-
* @see static::$errors
88-
*/
89-
public $strict = false;
90-
9182
/**
9283
* The string to be parsed.
9384
*
@@ -145,19 +136,6 @@ class Lexer
145136
*/
146137
public $delimiterLen;
147138

148-
/**
149-
* List of errors that occurred during lexing.
150-
*
151-
* Usually, the lexing does not stop once an error occurred because that
152-
* error might be false positive or a partial result (even a bad one)
153-
* might be needed.
154-
*
155-
* @var LexerException[]
156-
*
157-
* @see Lexer::error()
158-
*/
159-
public $errors = array();
160-
161139
/**
162140
* Gets the tokens list parsed by a new instance of a lexer.
163141
*
@@ -375,10 +353,7 @@ public function lex()
375353
public function error($msg = '', $str = '', $pos = 0, $code = 0)
376354
{
377355
$error = new LexerException($msg, $str, $pos, $code);
378-
if ($this->strict) {
379-
throw $error;
380-
}
381-
$this->errors[] = $error;
356+
parent::error($error);
382357
}
383358

384359
/**

src/Parser.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @package SqlParser
2424
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
2525
*/
26-
class Parser
26+
class Parser extends Core
2727
{
2828

2929
/**
@@ -318,28 +318,6 @@ class Parser
318318
*/
319319
public $list;
320320

321-
/**
322-
* Whether errors should throw exceptions or just be stored.
323-
*
324-
* @var bool
325-
*
326-
* @see static::$errors
327-
*/
328-
public $strict = false;
329-
330-
/**
331-
* List of errors that occurred during parsing.
332-
*
333-
* Usually, the parsing does not stop once an error occurred because that
334-
* error might be a false positive or a partial result (even a bad one)
335-
* might be needed.
336-
*
337-
* @var ParserException[]
338-
*
339-
* @see Parser::error()
340-
*/
341-
public $errors = array();
342-
343321
/**
344322
* List of statements parsed.
345323
*
@@ -599,9 +577,6 @@ public function parse()
599577
public function error($msg = '', Token $token = null, $code = 0)
600578
{
601579
$error = new ParserException($msg, $token, $code);
602-
if ($this->strict) {
603-
throw $error;
604-
}
605-
$this->errors[] = $error;
580+
parent::error($error);
606581
}
607582
}

0 commit comments

Comments
 (0)