From fadccead52e3c82fb0bb21c053fa81e72d94aa9a Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sun, 14 Jul 2024 20:51:46 +0200 Subject: [PATCH] Avoid compatibilty token list in parser Create the mapping using the entries in symbolToName, so we don't need to repeat the compatibility tokens in another place. --- lib/PhpParser/ParserAbstract.php | 48 ++++++-------------- phpstan-baseline.neon | 75 -------------------------------- 2 files changed, 14 insertions(+), 109 deletions(-) diff --git a/lib/PhpParser/ParserAbstract.php b/lib/PhpParser/ParserAbstract.php index b1bdf8a980..c3a1845731 100644 --- a/lib/PhpParser/ParserAbstract.php +++ b/lib/PhpParser/ParserAbstract.php @@ -1190,43 +1190,23 @@ protected function checkUseUse(UseItem $node, int $namePos): void { protected function createTokenMap(): array { $tokenMap = []; - for ($i = 0; $i < 1000; ++$i) { - if ($i < 256) { - // Single-char tokens use an identity mapping. - $tokenMap[$i] = $i; - } elseif (\T_DOUBLE_COLON === $i) { - // T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM - $tokenMap[$i] = static::T_PAAMAYIM_NEKUDOTAYIM; - } elseif (\T_OPEN_TAG_WITH_ECHO === $i) { - // T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO - $tokenMap[$i] = static::T_ECHO; - } elseif (\T_CLOSE_TAG === $i) { - // T_CLOSE_TAG is equivalent to ';' - $tokenMap[$i] = ord(';'); - } elseif ('UNKNOWN' !== $name = token_name($i)) { - if (defined($name = static::class . '::' . $name)) { - // Other tokens can be mapped directly - $tokenMap[$i] = constant($name); - } + // Single-char tokens use an identity mapping. + for ($i = 0; $i < 256; ++$i) { + $tokenMap[$i] = $i; + } + + foreach ($this->symbolToName as $name) { + if ($name[0] === 'T') { + $tokenMap[\constant($name)] = constant(static::class . '::' . $name); } } - // Assign tokens for which we define compatibility constants, as token_name() does not know them. - $tokenMap[\T_FN] = static::T_FN; - $tokenMap[\T_COALESCE_EQUAL] = static::T_COALESCE_EQUAL; - $tokenMap[\T_NAME_QUALIFIED] = static::T_NAME_QUALIFIED; - $tokenMap[\T_NAME_FULLY_QUALIFIED] = static::T_NAME_FULLY_QUALIFIED; - $tokenMap[\T_NAME_RELATIVE] = static::T_NAME_RELATIVE; - $tokenMap[\T_MATCH] = static::T_MATCH; - $tokenMap[\T_NULLSAFE_OBJECT_OPERATOR] = static::T_NULLSAFE_OBJECT_OPERATOR; - $tokenMap[\T_ATTRIBUTE] = static::T_ATTRIBUTE; - $tokenMap[\T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG] = static::T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG; - $tokenMap[\T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG] = static::T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG; - $tokenMap[\T_ENUM] = static::T_ENUM; - $tokenMap[\T_READONLY] = static::T_READONLY; - $tokenMap[\T_PROPERTY_C] = static::T_PROPERTY_C; - - // We have create a map from PHP token IDs to external symbol IDs. + // T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO + $tokenMap[\T_OPEN_TAG_WITH_ECHO] = static::T_ECHO; + // T_CLOSE_TAG is equivalent to ';' + $tokenMap[\T_CLOSE_TAG] = ord(';'); + + // We have created a map from PHP token IDs to external symbol IDs. // Now map them to the internal symbol ID. $fullTokenMap = []; foreach ($tokenMap as $phpToken => $extSymbol) { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index aeef7efa9d..f8a9570baa 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -160,86 +160,11 @@ parameters: count: 1 path: lib/PhpParser/NodeVisitor/ParentConnectingVisitor.php - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_ATTRIBUTE\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_COALESCE_EQUAL\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_ECHO\\.$#" count: 1 path: lib/PhpParser/ParserAbstract.php - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_ENUM\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_FN\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_MATCH\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_NAME_FULLY_QUALIFIED\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_NAME_QUALIFIED\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_NAME_RELATIVE\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_NULLSAFE_OBJECT_OPERATOR\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_PAAMAYIM_NEKUDOTAYIM\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_PROPERTY_C\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Access to undefined constant static\\(PhpParser\\\\ParserAbstract\\)\\:\\:T_READONLY\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - - - message: "#^Constant T_PROPERTY_C not found\\.$#" - count: 1 - path: lib/PhpParser/ParserAbstract.php - - message: "#^Unary operation \"\\+\" on string results in an error\\.$#" count: 1