jetpack
/
vendor
/
wp-php-toolkit
/
data-liberation
/
vendor-patched
/
symfony
/
polyfill-php80
/
PhpToken.php
jetpack
/
vendor
/
wp-php-toolkit
/
data-liberation
/
vendor-patched
/
symfony
/
polyfill-php80
Last commit date
Resources
3 weeks ago
LICENSE
3 weeks ago
Php80.php
3 weeks ago
PhpToken.php
3 weeks ago
README.md
3 weeks ago
bootstrap.php
3 weeks ago
PhpToken.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Polyfill\Php80; |
| 13 | |
| 14 | use function in_array; |
| 15 | use function is_string; |
| 16 | use function ord; |
| 17 | use function strlen; |
| 18 | |
| 19 | use const T_COMMENT; |
| 20 | use const T_DOC_COMMENT; |
| 21 | use const T_OPEN_TAG; |
| 22 | use const T_WHITESPACE; |
| 23 | |
| 24 | /** |
| 25 | * @author Fedonyuk Anton <info@ensostudio.ru> |
| 26 | * |
| 27 | * @internal |
| 28 | */ |
| 29 | class PhpToken { |
| 30 | /** |
| 31 | * @var int |
| 32 | */ |
| 33 | public $id; |
| 34 | /** |
| 35 | * @var string |
| 36 | */ |
| 37 | public $text; |
| 38 | /** |
| 39 | * @var int |
| 40 | */ |
| 41 | public $line; |
| 42 | /** |
| 43 | * @var int |
| 44 | */ |
| 45 | public $pos; |
| 46 | |
| 47 | public function __construct( int $id, string $text, int $line = - 1, int $position = - 1 ) { |
| 48 | $this->id = $id; |
| 49 | $this->text = $text; |
| 50 | $this->line = $line; |
| 51 | $this->pos = $position; |
| 52 | } |
| 53 | |
| 54 | public function getTokenName(): ?string { |
| 55 | if ( 'UNKNOWN' === $name = token_name( $this->id ) ) { |
| 56 | $name = strlen( $this->text ) > 1 || ord( $this->text ) < 32 ? null : $this->text; |
| 57 | } |
| 58 | |
| 59 | return $name; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param int|string|array $kind |
| 64 | */ |
| 65 | public function is( $kind ): bool { |
| 66 | foreach ( (array) $kind as $value ) { |
| 67 | if ( in_array( $value, [ $this->id, $this->text ], true ) ) { |
| 68 | return true; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | public function isIgnorable(): bool { |
| 76 | return in_array( $this->id, [ T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG ], true ); |
| 77 | } |
| 78 | |
| 79 | public function __toString(): string { |
| 80 | return (string) $this->text; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @return static[] |
| 85 | */ |
| 86 | public static function tokenize( string $code, int $flags = 0 ): array { |
| 87 | $line = 1; |
| 88 | $position = 0; |
| 89 | $tokens = token_get_all( $code, $flags ); |
| 90 | foreach ( $tokens as $index => $token ) { |
| 91 | if ( is_string( $token ) ) { |
| 92 | $id = ord( $token ); |
| 93 | $text = $token; |
| 94 | } else { |
| 95 | [ $id, $text, $line ] = $token; |
| 96 | } |
| 97 | $tokens[ $index ] = new static( $id, $text, $line, $position ); |
| 98 | $position += strlen( $text ); |
| 99 | } |
| 100 | |
| 101 | return $tokens; |
| 102 | } |
| 103 | } |
| 104 |