Skip to content

Commit

Permalink
Reflection: uses PhpToken
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 15, 2021
1 parent 4877b18 commit 29e8256
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Utils/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static function getUseStatements(\ReflectionClass $class): array
private static function parseUseStatements(string $code, string $forClass = null): array
{
try {
$tokens = token_get_all($code, TOKEN_PARSE);
$tokens = \PhpToken::tokenize($code, TOKEN_PARSE);
} catch (\ParseError $e) {
trigger_error($e->getMessage(), E_USER_NOTICE);
$tokens = [];
Expand All @@ -289,7 +289,7 @@ private static function parseUseStatements(string $code, string $forClass = null

while ($token = current($tokens)) {
next($tokens);
switch (is_array($token) ? $token[0] : $token) {
switch ($token->id) {
case T_NAMESPACE:
$namespace = ltrim(self::fetch($tokens, $nameTokens) . '\\', '\\');
$uses = [];
Expand Down Expand Up @@ -342,11 +342,11 @@ private static function parseUseStatements(string $code, string $forClass = null

case T_CURLY_OPEN:
case T_DOLLAR_OPEN_CURLY_BRACES:
case '{':
case ord('{'):
$level++;
break;

case '}':
case ord('}'):
if ($level === $classLevel) {
$class = $classLevel = null;
}
Expand All @@ -362,10 +362,9 @@ private static function fetch(array &$tokens, string|int|array $take): ?string
{
$res = null;
while ($token = current($tokens)) {
[$token, $s] = is_array($token) ? $token : [$token, $token];
if (in_array($token, (array) $take, true)) {
$res .= $s;
} elseif (!in_array($token, [T_DOC_COMMENT, T_WHITESPACE, T_COMMENT], true)) {
if ($token->is($take)) {
$res .= $token->text;
} elseif (!$token->is([T_DOC_COMMENT, T_WHITESPACE, T_COMMENT])) {
break;
}
next($tokens);
Expand Down

0 comments on commit 29e8256

Please sign in to comment.