From 0fed6671d7ac7b8b10f40fb3a987aa57eb6d73aa Mon Sep 17 00:00:00 2001 From: Paul Crovella Date: Sat, 20 Nov 2021 22:34:16 -0800 Subject: [PATCH] Yay, const visibility --- src/InputStream/Psr7Stream.php | 2 +- src/InputStream/Stream.php | 2 +- src/Parser/JsonParser.php | 65 ++++++++++++++++------------------ src/Parser/Tokenizer.php | 24 ++++++------- 4 files changed, 45 insertions(+), 48 deletions(-) diff --git a/src/InputStream/Psr7Stream.php b/src/InputStream/Psr7Stream.php index 63abc04..4ce8615 100644 --- a/src/InputStream/Psr7Stream.php +++ b/src/InputStream/Psr7Stream.php @@ -6,7 +6,7 @@ final class Psr7Stream implements InputStream { - const CHUNK_SIZE = 8192; + public const CHUNK_SIZE = 8192; private $stream; diff --git a/src/InputStream/Stream.php b/src/InputStream/Stream.php index 770eaeb..927e99d 100644 --- a/src/InputStream/Stream.php +++ b/src/InputStream/Stream.php @@ -6,7 +6,7 @@ final class Stream implements InputStream { - const CHUNK_SIZE = 8192; + public const CHUNK_SIZE = 8192; private $handle; diff --git a/src/Parser/JsonParser.php b/src/Parser/JsonParser.php index ad28774..69f6423 100644 --- a/src/Parser/JsonParser.php +++ b/src/Parser/JsonParser.php @@ -6,10 +6,7 @@ final class JsonParser implements Parser { - /** - * @var array Map of token to node types. - */ - private static $tokenTypeMap = [ + private const TOKEN_TYPE_MAP = [ Tokenizer::T_STRING => JsonReader::STRING, Tokenizer::T_NUMBER => JsonReader::NUMBER, Tokenizer::T_TRUE => JsonReader::BOOL, @@ -21,15 +18,15 @@ final class JsonParser implements Parser Tokenizer::T_END_OBJECT => JsonReader::END_OBJECT ]; - private static $stateDocumentEnd = 0; - private static $stateDocumentStart = 1; - private static $stateAfterArrayStart = 2; - private static $stateAfterArrayMember = 3; - private static $stateAfterObjectStart = 4; - private static $stateAfterObjectMember = 5; + private const STATE_DOCUMENT_END = 0; + private const STATE_DOCUMENT_START = 1; + private const STATE_AFTER_ARRAY_START = 2; + private const STATE_AFTER_ARRAY_MEMBER = 3; + private const STATE_AFTER_OBJECT_START = 4; + private const STATE_AFTER_OBJECT_MEMBER = 5; - private static $inArray = 1; - private static $inObject = 2; + private const IN_ARRAY = 1; + private const IN_OBJECT = 2; /** * @var Tokenizer @@ -44,7 +41,7 @@ final class JsonParser implements Parser public function __construct(Tokenizer $tokenizer) { $this->tokenizer = $tokenizer; - $this->state = self::$stateDocumentStart; + $this->state = self::STATE_DOCUMENT_START; } /** @@ -61,16 +58,16 @@ public function read() $token = $tokenizer->read(); switch ($state) { - case self::$stateAfterArrayStart: + case self::STATE_AFTER_ARRAY_START: if ($token[0] === Tokenizer::T_END_ARRAY) { goto end_of_array_or_object; } $names[$depth] = null; - $state = self::$stateAfterArrayMember; + $state = self::STATE_AFTER_ARRAY_MEMBER; goto value; - case self::$stateAfterArrayMember: + case self::STATE_AFTER_ARRAY_MEMBER: if ($token[0] === Tokenizer::T_END_ARRAY) { goto end_of_array_or_object; } @@ -82,15 +79,15 @@ public function read() $token = $tokenizer->read(); goto value; - case self::$stateAfterObjectStart: + case self::STATE_AFTER_OBJECT_START: if ($token[0] === Tokenizer::T_END_OBJECT) { goto end_of_array_or_object; } - $state = self::$stateAfterObjectMember; + $state = self::STATE_AFTER_OBJECT_MEMBER; goto object_member; - case self::$stateAfterObjectMember: + case self::STATE_AFTER_OBJECT_MEMBER: if ($token[0] === Tokenizer::T_END_OBJECT) { goto end_of_array_or_object; } @@ -102,11 +99,11 @@ public function read() $token = $tokenizer->read(); goto object_member; - case self::$stateDocumentStart: - $state = self::$stateDocumentEnd; + case self::STATE_DOCUMENT_START: + $state = self::STATE_DOCUMENT_END; goto value; - case self::$stateDocumentEnd: + case self::STATE_DOCUMENT_END: $names = [null]; if ($token[0] !== Tokenizer::T_EOF) { throw new ParseException($this->getExceptionMessage($token)); @@ -133,16 +130,16 @@ public function read() \array_pop($stack); $depth--; switch (\end($stack)) { - case self::$inArray: - $state = self::$stateAfterArrayMember; + case self::IN_ARRAY: + $state = self::STATE_AFTER_ARRAY_MEMBER; break; - case self::$inObject: - $state = self::$stateAfterObjectMember; + case self::IN_OBJECT: + $state = self::STATE_AFTER_OBJECT_MEMBER; break; default: - $state = self::$stateDocumentEnd; + $state = self::STATE_DOCUMENT_END; } - return [self::$tokenTypeMap[$token[0]], $names[$depth], $token[1], $depth]; + return [self::TOKEN_TYPE_MAP[$token[0]], $names[$depth], $token[1], $depth]; } value: { @@ -155,25 +152,25 @@ public function read() case Tokenizer::T_NULL: break; case Tokenizer::T_BEGIN_ARRAY: - $state = self::$stateAfterArrayStart; - $stack[] = self::$inArray; + $state = self::STATE_AFTER_ARRAY_START; + $stack[] = self::IN_ARRAY; $depth++; break; case Tokenizer::T_BEGIN_OBJECT: - $state = self::$stateAfterObjectStart; - $stack[] = self::$inObject; + $state = self::STATE_AFTER_OBJECT_START; + $stack[] = self::IN_OBJECT; $depth++; break; default: throw new ParseException($this->getExceptionMessage($token)); } - return [self::$tokenTypeMap[$token[0]], $names[$currentDepth], $token[1], $currentDepth]; + return [self::TOKEN_TYPE_MAP[$token[0]], $names[$currentDepth], $token[1], $currentDepth]; } } private function getExceptionMessage(array $token): string { - list ($tokenType, , $tokenLine) = $token; + [$tokenType, , $tokenLine] = $token; if ($tokenType === Tokenizer::T_EOF) { return \sprintf( diff --git a/src/Parser/Tokenizer.php b/src/Parser/Tokenizer.php index 1dd7ae1..dded2aa 100644 --- a/src/Parser/Tokenizer.php +++ b/src/Parser/Tokenizer.php @@ -4,18 +4,18 @@ interface Tokenizer { - const T_STRING = "T_STRING"; - const T_NUMBER = "T_NUMBER"; - const T_TRUE = "T_TRUE"; - const T_FALSE = "T_FALSE"; - const T_NULL = "T_NULL"; - const T_COLON = "T_COLON"; - const T_COMMA = "T_COMMA"; - const T_BEGIN_ARRAY = "T_BEGIN_ARRAY"; - const T_END_ARRAY = "T_END_ARRAY"; - const T_BEGIN_OBJECT = "T_BEGIN_OBJECT"; - const T_END_OBJECT = "T_END_OBJECT"; - const T_EOF = "T_EOF"; + public const T_STRING = "T_STRING"; + public const T_NUMBER = "T_NUMBER"; + public const T_TRUE = "T_TRUE"; + public const T_FALSE = "T_FALSE"; + public const T_NULL = "T_NULL"; + public const T_COLON = "T_COLON"; + public const T_COMMA = "T_COMMA"; + public const T_BEGIN_ARRAY = "T_BEGIN_ARRAY"; + public const T_END_ARRAY = "T_END_ARRAY"; + public const T_BEGIN_OBJECT = "T_BEGIN_OBJECT"; + public const T_END_OBJECT = "T_END_OBJECT"; + public const T_EOF = "T_EOF"; /** * @return array Tuples in the form of [string type, mixed value, int line]