From 370c5e4e2e10eb4d3e406d3a90526f821de98190 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 25 Jan 2022 22:32:34 +0100 Subject: [PATCH] added Token::is() --- src/Tokenizer/Stream.php | 16 ++++------------ src/Tokenizer/Token.php | 8 ++++++++ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Tokenizer/Stream.php b/src/Tokenizer/Stream.php index a2ee4c0..c55680a 100644 --- a/src/Tokenizer/Stream.php +++ b/src/Tokenizer/Stream.php @@ -161,13 +161,8 @@ public function joinUntil(...$args): string */ public function isCurrent(...$args): bool { - if (!isset($this->tokens[$this->position])) { - return false; - } - - $token = $this->tokens[$this->position]; - return in_array($token->value, $args, true) - || in_array($token->type, $args, true); + $token = $this->tokens[$this->position] ?? null; + return $token && $token->is(...$args); } @@ -236,10 +231,7 @@ protected function scan( $token = $this->tokens[$pos]; if ( !$wanted - || ( - in_array($token->value, $wanted, true) - || in_array($token->type, $wanted, true) - ) ^ $until + || $token->is(...$wanted) ^ $until ) { while ($advance && !$prev && $pos > $this->position) { $this->next(); @@ -252,7 +244,7 @@ protected function scan( } else { $res[] = $token; } - } elseif ($until || !in_array($token->type, $this->ignored, true)) { + } elseif ($until || !$token->is(...$this->ignored)) { return $res; } diff --git a/src/Tokenizer/Token.php b/src/Tokenizer/Token.php index cc566df..a498c32 100644 --- a/src/Tokenizer/Token.php +++ b/src/Tokenizer/Token.php @@ -31,4 +31,12 @@ public function __construct(string $value, $type, int $offset) $this->type = $type; $this->offset = $offset; } + + + /** @param int|string ...$args */ + public function is(...$args): bool + { + return in_array($this->value, $args, true) + || in_array($this->type, $args, true); + } }