note 126646 deleted from class.phptoken by cmb

From: Date: Fri, 25 Feb 2022 17:03:52 +0000
Subject: note 126646 deleted from class.phptoken by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-217657@lists.php.net to get a copy of this message
Note Submitter: info at ensostudio dot ru ---- <?php /** * PhpToken polyfill. */ class PhpToken { /** * @var int Identifier */ public $id; /** * @var string The textual content of the token */ public $text; /** * @var int The starting line number (1-based) of the token */ public $line; /** * @var int The starting position (0-based) in the tokenized string */ public $pos; /** * Creates a new instance. * * @param int|string $id T_* constant or an ASCII codepoint representing a single-char token * @param string $text The textual content of the token * @param int $line The starting line number (1-based) of the token * @param int $position The starting position (0-based) in the tokenized string * @return void */ public function __construct($id, $text, $line = -1, $position = -1) { $this->id = is_int($id) ? $id : ord($id); $this->text = $text; $this->line = $line; $this->pos = $position; } /** * Returns the name of the token. * * @return string|null */ public function getTokenName() { $name = token_name($this->id); return $name === 'UNKNOWN' ? null : $name; } /** * Tells whether the token is of given kind. * * @param int|string|array $kind Either a single value to match the token's id or textual content, or an array * thereof * @return bool */ public function is($kind) { $token = (is_int($kind) || (is_array($kind) && is_int(current($kind)))) ? $this->id : $this->getTokenName(); return in_array($token, (array) $kind, true); } /** * Tells whether the token would be ignored by the PHP parser. * * @return bool */ public function isIgnorable() { return $this->is([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_INLINE_HTML]); } /** * Returns the textual content of the token. * * @return string */ public function __toString() { return (string) $this->text; } /** * Splits given source into PHP tokens, represented by self objects. * * @param string $code The PHP source to parse * @param int $flags TOKEN_PARSE - recognises the ability to usereserved words in specific contexts * @return static[] */ public static function tokenize($code, $flags = 0): array { $tokens = []; $line = 1; $position = 0; foreach (token_get_all($code, $flags) as $token) { if (is_string($token)) { $id = $token; $text = $token; } else { if ($token[2] !== $line) { $position = 0; } list($id, $text, $line) = $token; } $tokens[] = new static($id, $text, $line, $position); $position += strlen($text); } return $tokens; } } ?>

« previous php.notes (#217657) next »