From aa22c2f6888d1b293e79ee954823adc96f8af4e1 Mon Sep 17 00:00:00 2001 From: Jan Tvrdik Date: Mon, 30 Apr 2018 12:37:48 +0200 Subject: [PATCH] fix issues found by phpstan --- src/Lexer/Lexer.php | 7 ++++--- src/Parser/ParserException.php | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Lexer/Lexer.php b/src/Lexer/Lexer.php index 09e0ef1..f31dbb0 100644 --- a/src/Lexer/Lexer.php +++ b/src/Lexer/Lexer.php @@ -83,15 +83,16 @@ class Lexer public function tokenize(string $s): array { - if ($this->regexp === null) { + if ($this->regexp === null || $this->types === null) { $this->initialize(); } + assert($this->regexp !== null); + assert($this->types !== null); + preg_match_all($this->regexp, $s, $tokens, PREG_SET_ORDER); - assert($this->types !== null); $count = count($this->types); - foreach ($tokens as &$match) { for ($i = 1; $i <= $count; $i++) { if ($match[$i] !== null && $match[$i] !== '') { diff --git a/src/Parser/ParserException.php b/src/Parser/ParserException.php index e5683de..d6fd6fe 100644 --- a/src/Parser/ParserException.php +++ b/src/Parser/ParserException.php @@ -31,9 +31,12 @@ class ParserException extends \Exception $this->currentOffset = $currentOffset; $this->expectedTokenType = $expectedTokenType; + $json = json_encode($currentTokenValue, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + assert($json !== false); + parent::__construct(sprintf( 'Unexpected token %s, expected %s at offset %d', - json_encode($currentTokenValue, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), + $json, Lexer::TOKEN_LABELS[$expectedTokenType], $currentOffset ));