Create BetterPhpToken class
parent
5250c63ba6
commit
a544a473ff
@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace jrosset\BetterPhpToken;
|
||||
|
||||
defined('T_LITERAL_TEXT') || define('T_LITERAL_TEXT', 10001);
|
||||
|
||||
/**
|
||||
* A PHP token
|
||||
*/
|
||||
class BetterPhpToken {
|
||||
/**
|
||||
* @var int The {@see https://www.php.net/manual/fr/tokens.php token's parser ID}
|
||||
*/
|
||||
protected int $tokenId;
|
||||
/**
|
||||
* @var string The token's text
|
||||
*/
|
||||
protected string $text;
|
||||
|
||||
/**
|
||||
* Create a new token
|
||||
*
|
||||
* @param int $tokenId The {@see https://www.php.net/manual/fr/tokens.php token's parser ID}
|
||||
* @param string $text The token's text
|
||||
* @param int $line The starting line number (1-based), -1 if not available
|
||||
*/
|
||||
public function __construct (int $tokenId, string $text) {
|
||||
$this->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<int, string> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue