Skip to content

Commit

Permalink
uses PhpToken
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 27, 2021
1 parent e0e3547 commit bd652f2
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions src/RobotLoader/RobotLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private function scanPhp(string $file): array
$classes = [];

try {
$tokens = token_get_all($code, TOKEN_PARSE);
$tokens = \PhpToken::tokenize($code, TOKEN_PARSE);
} catch (\ParseError $e) {
if ($this->reportParseErrors) {
$rp = new \ReflectionProperty($e, 'file');
Expand All @@ -327,50 +327,48 @@ private function scanPhp(string $file): array
}

foreach ($tokens as $token) {
if (is_array($token)) {
switch ($token[0]) {
case T_COMMENT:
case T_DOC_COMMENT:
case T_WHITESPACE:
continue 2;

case T_STRING:
case T_NAME_QUALIFIED:
if ($expected) {
$name .= $token[1];
}
continue 2;

case T_NAMESPACE:
case T_CLASS:
case T_INTERFACE:
case T_TRAIT:
case PHP_VERSION_ID < 80100
? T_CLASS
: T_ENUM:
$expected = $token[0];
$name = '';
continue 2;
case T_CURLY_OPEN:
case T_DOLLAR_OPEN_CURLY_BRACES:
$level++;
}
switch ($token->id) {
case T_COMMENT:
case T_DOC_COMMENT:
case T_WHITESPACE:
continue 2;

case T_STRING:
case T_NAME_QUALIFIED:
if ($expected) {
$name .= $token->text;
}
continue 2;

case T_NAMESPACE:
case T_CLASS:
case T_INTERFACE:
case T_TRAIT:
case PHP_VERSION_ID < 80100
? T_CLASS
: T_ENUM:
$expected = $token->id;
$name = '';
continue 2;
case T_CURLY_OPEN:
case T_DOLLAR_OPEN_CURLY_BRACES:
$level++;
}

if ($expected) {
if ($expected === T_NAMESPACE) {
$namespace = $name ? $name . '\\' : '';
$minLevel = $token === '{' ? 1 : 0;
$minLevel = $token->text === '{' ? 1 : 0;

} elseif ($name && $level === $minLevel) {
$classes[] = $namespace . $name;
}
$expected = null;
}

if ($token === '{') {
if ($token->text === '{') {
$level++;
} elseif ($token === '}') {
} elseif ($token->text === '}') {
$level--;
}
}
Expand Down

0 comments on commit bd652f2

Please sign in to comment.