From 61ecbce1468230f68f7853d826a4755d34d51334 Mon Sep 17 00:00:00 2001 From: SerafimArts Date: Thu, 1 Aug 2024 17:44:54 +0000 Subject: [PATCH] Add missing multistate lexer constructor type hints --- src/Exception/EndlessRecursionException.php | 2 +- src/Exception/UnexpectedStateException.php | 5 +--- src/Lexer.php | 2 +- src/Multistate.php | 19 ++++++--------- src/Token/Composite.php | 26 +++++++-------------- src/Token/Token.php | 4 ++-- 6 files changed, 20 insertions(+), 38 deletions(-) diff --git a/src/Exception/EndlessRecursionException.php b/src/Exception/EndlessRecursionException.php index 9dde85e..653aca8 100644 --- a/src/Exception/EndlessRecursionException.php +++ b/src/Exception/EndlessRecursionException.php @@ -9,7 +9,7 @@ class EndlessRecursionException extends UnexpectedStateException { - public static function fromState($state, ReadableInterface $src, ?TokenInterface $tok, ?\Throwable $e = null): self + public static function fromState(mixed $state, ReadableInterface $src, ?TokenInterface $tok, ?\Throwable $e = null): self { $message = \vsprintf('An unsolvable infinite lexer state transitions was found at %s', [ $state, diff --git a/src/Exception/UnexpectedStateException.php b/src/Exception/UnexpectedStateException.php index 1bb3a8b..f0a7664 100644 --- a/src/Exception/UnexpectedStateException.php +++ b/src/Exception/UnexpectedStateException.php @@ -16,10 +16,7 @@ public static function fromEmptyStates(ReadableInterface $src, ?\Throwable $e = return new static($message, $src, null, $e); } - /** - * @param string|int $state - */ - public static function fromState($state, ReadableInterface $src, ?TokenInterface $tok, ?\Throwable $e = null): self + public static function fromState(string|int $state, ReadableInterface $src, ?TokenInterface $tok, ?\Throwable $e = null): self { $message = \sprintf('Unrecognized token state #%s', $state); diff --git a/src/Lexer.php b/src/Lexer.php index 22b5f04..fee6218 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -319,7 +319,7 @@ public function remove(string ...$tokens): self * starting the lexical analysis and indicates problems in the * analyzed source */ - public function lex($source, int $offset = 0): iterable + public function lex(mixed $source, int $offset = 0): iterable { try { $source = $this->sources->create($source); diff --git a/src/Multistate.php b/src/Multistate.php index 7eba65b..b53f916 100644 --- a/src/Multistate.php +++ b/src/Multistate.php @@ -26,7 +26,7 @@ class Multistate implements PositionalLexerInterface /** * @var array-key|null */ - private $state; + private string|int|null $state; /** * @var array, array>> @@ -52,7 +52,7 @@ class Multistate implements PositionalLexerInterface public function __construct( array $states, array $transitions = [], - $state = null, + string|int|null $state = null, ?HandlerInterface $onEndOfInput = null, ?SourceFactoryInterface $sources = null ) { @@ -75,10 +75,8 @@ public function __construct( * * @param array-key|null $state */ - public function startsWith($state): self + public function startsWith(int|string|null $state): self { - assert(\is_string($state) || \is_int($state) || $state === null); - $this->state = $state; return $this; @@ -90,11 +88,8 @@ public function startsWith($state): self * @param array-key $name * @param array|PositionalLexerInterface $data */ - public function setState($name, $data): self + public function setState(string|int $name, array|PositionalLexerInterface $data): self { - assert(\is_string($name) || \is_int($name)); - assert(\is_array($data) || $data instanceof PositionalLexerInterface); - if (\is_array($data)) { $data = new Lexer($data); } @@ -109,7 +104,7 @@ public function setState($name, $data): self * * @param array-key $name */ - public function removeState($name): self + public function removeState(string|int $name): self { unset($this->states[$name]); @@ -123,7 +118,7 @@ public function removeState($name): self * @param array-key $in * @param array-key $then */ - public function when(string $token, $in, $then): self + public function when(string $token, string|int $in, string|int $then): self { $this->transitions[$in][$token] = $then; @@ -151,7 +146,7 @@ public function when(string $token, $in, $then): self * * @psalm-suppress TypeDoesNotContainType */ - public function lex($source, int $offset = 0): iterable + public function lex(mixed $source, int $offset = 0): iterable { try { $source = $this->sources->create($source); diff --git a/src/Token/Composite.php b/src/Token/Composite.php index 597185f..3144507 100644 --- a/src/Token/Composite.php +++ b/src/Token/Composite.php @@ -18,7 +18,7 @@ class Composite extends Token implements CompositeTokenInterface * @param int<0, max> $offset * @param array $children */ - public function __construct($name, string $value, int $offset, array $children) + public function __construct(int|string $name, string $value, int $offset, array $children) { $this->children = $children; @@ -49,31 +49,21 @@ public function getIterator(): \Traversable return new \ArrayIterator($this->children); } - /** - * @param int $offset - */ - public function offsetExists($offset): bool + public function offsetExists(mixed $offset): bool { \assert(\is_int($offset)); return isset($this->children[$offset]); } - /** - * @param int $offset - */ - public function offsetGet($offset): ?TokenInterface + public function offsetGet(mixed $offset): ?TokenInterface { \assert(\is_int($offset)); return $this->children[$offset] ?? null; } - /** - * @param int $offset - * @param TokenInterface $value - */ - public function offsetSet($offset, $value): void + public function offsetSet(mixed $offset, mixed $value): void { \assert(\is_int($offset)); \assert($value instanceof TokenInterface); @@ -81,16 +71,16 @@ public function offsetSet($offset, $value): void $this->children[$offset] = $value; } - /** - * @param int $offset - */ - public function offsetUnset($offset): void + public function offsetUnset(mixed $offset): void { \assert(\is_int($offset)); unset($this->children[$offset]); } + /** + * @return int<0, max> + */ public function count(): int { return \count($this->children); diff --git a/src/Token/Token.php b/src/Token/Token.php index b797d82..bdcb46d 100644 --- a/src/Token/Token.php +++ b/src/Token/Token.php @@ -14,7 +14,7 @@ class Token extends BaseToken /** * @var array-key */ - private $name; + private string|int $name; private string $value; @@ -27,7 +27,7 @@ class Token extends BaseToken * @param array-key $name * @param int<0, max> $offset */ - public function __construct($name, string $value, int $offset = 0) + public function __construct(string|int $name, string $value, int $offset = 0) { if ($name === '') { $name = self::$anonymousId++;