From a1c8543652eb23c3818f5c31029d7fdcfbcca2ae Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Sat, 28 Dec 2024 02:25:06 +0100 Subject: [PATCH] cut token::family logic extraction in one of hotpaths --- src/Tokenizer/Token.php | 21 +++++++++++++++++++++ src/Tokenizer/Tokens.php | 11 ++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Tokenizer/Token.php b/src/Tokenizer/Token.php index 57ad192d6fa..1d2ba49747f 100644 --- a/src/Tokenizer/Token.php +++ b/src/Tokenizer/Token.php @@ -41,6 +41,13 @@ final class Token */ private bool $isArray; + /** + * @var int|string + * + * @todo Replace the name with outcome of https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/8333 + */ + private $family; + /** * @param array{int, string}|string $token token prototype */ @@ -66,10 +73,12 @@ public function __construct($token) } $this->isArray = true; + $this->family = $token[0]; $this->id = $token[0]; $this->content = $token[1]; } elseif (\is_string($token)) { $this->isArray = false; + $this->family = $token; $this->id = null; $this->content = $token; } else { @@ -267,6 +276,18 @@ public function getId(): ?int return $this->id; } + /** + * @return int|string + * + * @internal + * + * @private + */ + public function getFamily() + { + return $this->family; + } + /** * Get token's name. * diff --git a/src/Tokenizer/Tokens.php b/src/Tokenizer/Tokens.php index 2b640bf9c3a..31b0065b8ef 100644 --- a/src/Tokenizer/Tokens.php +++ b/src/Tokenizer/Tokens.php @@ -150,8 +150,7 @@ public static function detectBlockType(Token $token): ?array } } - // inlined extractTokenKind() call on the hot path - $tokenKind = $token->isArray() ? $token->getId() : $token->getContent(); + $tokenKind = $token->getFamily(); return $blockEdgeKinds[$tokenKind] ?? null; } @@ -1423,8 +1422,7 @@ private function changeCodeHash(string $codeHash): void */ private function registerFoundToken(Token $token): void { - // inlined extractTokenKind() call on the hot path - $tokenKind = $token->isArray() ? $token->getId() : $token->getContent(); + $tokenKind = $token->getFamily(); $this->foundTokenKinds[$tokenKind] ??= 0; ++$this->foundTokenKinds[$tokenKind]; @@ -1435,8 +1433,7 @@ private function registerFoundToken(Token $token): void */ private function unregisterFoundToken(Token $token): void { - // inlined extractTokenKind() call on the hot path - $tokenKind = $token->isArray() ? $token->getId() : $token->getContent(); + $tokenKind = $token->getFamily(); if (1 === $this->foundTokenKinds[$tokenKind]) { unset($this->foundTokenKinds[$tokenKind]); @@ -1453,7 +1450,7 @@ private function unregisterFoundToken(Token $token): void private function extractTokenKind($token) { return $token instanceof Token - ? ($token->isArray() ? $token->getId() : $token->getContent()) + ? $token->getFamily() : (\is_array($token) ? $token[0] : $token); }