{"record":{"id":"e78b5cc54e977b38","repo":"twigphp/Twig","slug":"token-of-type-s-does-not-exist","errorCode":null,"errorMessage":"Token of type \"%s\" does not exist.","messagePattern":"Token of type \"(.+?)\" does not exist\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Token.php","lineNumber":219,"sourceCode":"                $name = 'OPERATOR_TYPE';\n                break;\n            case self::PUNCTUATION_TYPE:\n                $name = 'PUNCTUATION_TYPE';\n                break;\n            case self::INTERPOLATION_START_TYPE:\n                $name = 'INTERPOLATION_START_TYPE';\n                break;\n            case self::INTERPOLATION_END_TYPE:\n                $name = 'INTERPOLATION_END_TYPE';\n                break;\n            case self::ARROW_TYPE:\n                $name = 'ARROW_TYPE';\n                break;\n            case self::SPREAD_TYPE:\n                $name = 'SPREAD_TYPE';\n                break;\n            default:\n                throw new \\LogicException(\\sprintf('Token of type \"%s\" does not exist.', $type));\n        }\n\n        return $short ? $name : 'Twig\\Token::'.$name;\n    }\n\n    public static function typeToEnglish(int $type): string\n    {\n        switch ($type) {\n            case self::EOF_TYPE:\n                return 'end of template';\n            case self::TEXT_TYPE:\n                return 'text';\n            case self::BLOCK_START_TYPE:\n                return 'begin of statement block';\n            case self::VAR_START_TYPE:\n                return 'begin of print statement';\n            case self::BLOCK_END_TYPE:\n                return 'end of statement block';","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Token.php#L201-L237","documentation":"Token::typeToString() converts a numeric token type constant (e.g. Token::NAME_TYPE) into its human-readable name. If the integer does not correspond to any defined Twig token type, the default branch throws this LogicException. It is called from __toString() and error message formatting, so it usually surfaces while rendering diagnostics for a corrupt token.","triggerScenarios":"Passing an integer to Token::typeToString() (or to a Token whose ->type is such an int) that is not one of the Token::*_TYPE constants — e.g. hand-constructed Token objects with bogus types, lexer bugs, or int-cast garbage from custom lexers.","commonSituations":"Custom lexer/TokenParser producing tokens with out-of-range types; passing a raw value (e.g. from untrusted data) to Token::typeToString(); debugging a syntax error where the token stream was built incorrectly.","solutions":["Only pass constants from Twig\\Token (NAME_TYPE, NUMBER_TYPE, etc.) to typeToString()/new Token().","Add an is_int + range/constant check before constructing tokens from external input.","If writing a custom lexer, verify every type value against Token's defined constants.","Check the call site in __toString()/error formatting for a corrupted token that originates earlier in the lexer."],"exampleFix":"// before\n$token = new Token($userData['type'], $value, 1); // type may be arbitrary\n\n// after\n$known = [\\Twig\\Token::NAME_TYPE, \\Twig\\Token::NUMBER_TYPE, \\Twig\\Token::STRING_TYPE, /* ... */];\nif (!in_array($userData['type'], $known, true)) {\n    throw new \\InvalidArgumentException('Unknown token type');\n}\n$token = new Token((int) $userData['type'], $value, 1);","handlingStrategy":"type-guard","validationCode":"if (!in_array($type, [\\Twig\\Token::BLOCK_END_TYPE, \\Twig\\Token::NAME_TYPE, /* all used constants */], true)) {\n    throw new \\InvalidArgumentException('Unknown token type: '.$type);\n}","typeGuard":"function isValidTokenType(int $type): bool\n{\n    return in_array($type, (new ReflectionClass(\\Twig\\Token::class))->getConstants(), true);\n}","tryCatchPattern":"try { $label = \\Twig\\Token::typeToString($type); } catch (\\LogicException $e) { if (str_contains($e->getMessage(), 'does not exist')) { $label = 'UNKNOWN_TYPE('.$type.')'; } else { throw $e; } }","preventionTips":["Only construct tokens using Twig\\Token::*_TYPE constants","Validate integer types coming from external data before building tokens","In custom lexers, define a unit test converting every emitted type with typeToString()"],"tags":["twig","php","lexer","invalid-value","internal"],"backgroundTag":"invalid-enum-value","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}