setTokenId($tokenId); $this->setText($text); } /** * The token's text * * @return string The token's text */ public function __toString (): string { return $this->getText(); } /** * Transform a code into a list of tokens * * @param string $code The code to tokenize * * @return static[] The list of tokens */ public static function tokenize (string $code): array { return array_map( function ($token): self { if (is_array($token)) { return new static($token[0], $token[1]); } else { return new static(T_LITERAL_TEXT, $token); } }, token_get_all($code) ); } /** * Check if token is of a kind * * @param int $kind The kind * @param int ...$extraKinds Extra possibles kinds * * @return bool True if token is one of the kinds, else False */ public function is (int $kind, int ...$extraKinds): bool { return $this->getTokenId() === $kind || in_array($this->getTokenId(), $extraKinds); } /** * Get extra token ids * * @return array Extra token ids: key = token ID, value = token name */ protected static function getExtraTokenIds (): array { return [ T_LITERAL_TEXT => 'T_LITERAL_TEXT', ]; } /** * The name of parser's token ID * * @return string The name of parser's token ID */ public function getTokenName (): string { $extraTokens = static::getExtraTokenIds(); if (isset($extraTokens[$this->getTokenId()])) { return $extraTokens[$this->getTokenId()]; } return token_name($this->getTokenId()); } /** * The {@see https://www.php.net/manual/fr/tokens.php token's parser ID} * * @return int The {@see https://www.php.net/manual/fr/tokens.php token's parser ID} */ public function getTokenId (): int { return $this->tokenId; } /** * Set the {@see https://www.php.net/manual/fr/tokens.php token's parser ID} * * @param int $tokenId The {@see https://www.php.net/manual/fr/tokens.php token's parser ID} * * @return $this */ public function setTokenId (int $tokenId): self { $this->tokenId = $tokenId; return $this; } /** * The token's text * * @return string The token's text */ public function getText (): string { return $this->text; } /** * Set the token's text * * @param string $text The token's text * * @return $this */ public function setText (string $text): self { $this->text = $text; return $this; } }