note 126881 deleted from class.phptoken by cmb
| From: | cmb@php.net | Date: | Fri, 25 Feb 2022 17:03:58 +0000 |
| Subject: | note 126881 deleted from class.phptoken by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217658@lists.php.net to get a copy of this message | ||
Note Submitter: info at ensostudio dot ru
----
Fixed version:
<?php
/**
* Tokenizer: PhpToken polyfill.
*
* @author Fedonyuk Anton <info@ensostudio.ru>
* @link https://www.php.net/manual/en/class.phptoken.php
*/
class PhpToken implements \Stringable
{
/**
* @var int
*/
public $id;
/**
* @var string
*/
public $text;
/**
* @var int
*/
public $line;
/**
* @var int
*/
public $pos;
public function __construct(int $id, string $text, int $line = -1, int $position = -1)
{
$this->id = $id;
$this->text = $text;
$this->line = $line;
$this->pos = $position;
}
public function getTokenName(): ?string
{
$name = \token_name($this->id);
if ($name === 'UNKNOWN') {
$name = \strlen($this->text) > 1 || \ord($this->text) < 32 ? null :
$this->text;
}
return $name;
}
/**
* @param int|string|array $kind
*/
public function is($kind): bool
{
$kind = (array) $kind;
if (\is_int(\current($kind))) {
return \in_array($this->id, $kind, true);
}
return \in_array($this->text, $kind, true) || \in_array($this->getTokenName(), $kind,
true);
}
public function isIgnorable(): bool
{
return $this->is([\T_WHITESPACE, \T_COMMENT, \T_DOC_COMMENT, \T_INLINE_HTML]);
}
public function __toString(): string
{
return $this->text;
}
/**
* @return static[]
*/
public static function tokenize(string $code, int $flags = 0): array
{
$tokens = [];
$line = 1;
$position = 0;
foreach (\token_get_all($code, $flags) as $token) {
if (\is_string($token)) {
$id = $text = $token;
} else {
[$id, $text, $line] = $token;
}
if (\is_string($id)) {
$id = \ord($id);
}
$tokens[] = new static($id, $text, $line, $position);
$position += \strlen($text);
}
return $tokens;
}
}
?>