From f260cfb0b1b6e84542a250a8f2f10533afdc021e Mon Sep 17 00:00:00 2001 From: Romain Canon Date: Fri, 18 Aug 2023 12:27:21 +0200 Subject: [PATCH] misc: simplify symbol parsing algorithm --- src/Type/Parser/ParserSymbols.php | 47 ++++++++++++------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/src/Type/Parser/ParserSymbols.php b/src/Type/Parser/ParserSymbols.php index 22b0d5f6..15053aec 100644 --- a/src/Type/Parser/ParserSymbols.php +++ b/src/Type/Parser/ParserSymbols.php @@ -7,38 +7,35 @@ final class ParserSymbols { private const OPERATORS = [' ', '|', '&', '<', '>', '[', ']', '{', '}', ':', '?', ',', "'", '"']; - private ?string $current = null; - /** @var list */ private array $symbols = []; public function __construct(string $string) { + $current = null; $quote = null; foreach (str_split($string) as $char) { - if ($quote !== null) { - if ($char === $quote) { - $this->addCurrent(); - $this->symbols[] = $char; - - $quote = null; - } else { - $this->current .= $char; - } - } elseif (in_array($char, self::OPERATORS, true)) { - $this->addCurrent(); - $this->symbols[] = $char; - - if ($char === '"' || $char === "'") { - $quote = $char; - } - } else { - $this->current .= $char; + if ($char === $quote) { + $quote = null; + } elseif ($char === '"' || $char === "'") { + $quote = $char; + } elseif ($quote !== null || ! in_array($char, self::OPERATORS, true)) { + $current .= $char; + continue; } + + if ($current !== null) { + $this->symbols[] = $current; + $current = null; + } + + $this->symbols[] = $char; } - $this->addCurrent(); + if ($current !== null) { + $this->symbols[] = $current; + } $this->symbols = array_map('trim', $this->symbols); $this->symbols = array_filter($this->symbols, static fn ($value) => $value !== ''); @@ -55,14 +52,6 @@ public function all(): array return $this->symbols; } - private function addCurrent(): void - { - if ($this->current !== null) { - $this->symbols[] = $this->current; - $this->current = null; - } - } - private function mergeDoubleColons(): void { foreach ($this->symbols as $key => $symbol) {