Skip to content

Commit

Permalink
uses PhpToken
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 5, 2023
1 parent 92ad30c commit 925b93b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
29 changes: 13 additions & 16 deletions src/CodeCoverage/PhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class PhpParser
*/
public function parse(string $code): \stdClass
{
$tokens = token_get_all($code, TOKEN_PARSE);
$tokens = \PhpToken::tokenize($code, TOKEN_PARSE);

$level = $classLevel = $functionLevel = null;
$namespace = '';
Expand All @@ -70,11 +70,9 @@ public function parse(string $code): \stdClass

while ($token = current($tokens)) {
next($tokens);
if (is_array($token)) {
$line = $token[2];
}
$line = $token->line;

switch (is_array($token) ? $token[0] : $token) {
switch ($token->id) {
case T_NAMESPACE:
$namespace = self::fetch($tokens, [T_STRING, T_NAME_QUALIFIED]);
$namespace = ltrim($namespace . '\\', '\\');
Expand All @@ -84,9 +82,9 @@ public function parse(string $code): \stdClass
case T_INTERFACE:
case T_TRAIT:
if ($name = self::fetch($tokens, T_STRING)) {
if ($token[0] === T_CLASS) {
if ($token->id === T_CLASS) {
$class = &$result->classes[$namespace . $name];
} elseif ($token[0] === T_INTERFACE) {
} elseif ($token->id === T_INTERFACE) {
$class = &$result->interfaces[$namespace . $name];
} else {
$class = &$result->traits[$namespace . $name];
Expand All @@ -105,7 +103,7 @@ public function parse(string $code): \stdClass
case T_PUBLIC:
case T_PROTECTED:
case T_PRIVATE:
$visibility = $token[1];
$visibility = $token->text;
break;

case T_ABSTRACT:
Expand Down Expand Up @@ -138,11 +136,11 @@ public function parse(string $code): \stdClass

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

case '}':
case ord('}'):
if (isset($function) && $level === $functionLevel) {
$function->end = $line;
unset($function);
Expand All @@ -157,12 +155,12 @@ public function parse(string $code): \stdClass

case T_COMMENT:
case T_DOC_COMMENT:
$result->linesOfComments += substr_count(trim($token[1]), "\n") + 1;
$result->linesOfComments += substr_count(trim($token->text), "\n") + 1;
// break omitted

case T_WHITESPACE:
case T_CONSTANT_ENCAPSED_STRING:
$line += substr_count($token[1], "\n");
$line += substr_count($token->text, "\n");
break;
}
}
Expand All @@ -175,10 +173,9 @@ private static function fetch(array &$tokens, array|int $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;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Framework/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,10 @@ public static function bypassFinals(): void
{
FileMutator::addMutator(function (string $code): string {
if (str_contains($code, 'final')) {
$tokens = token_get_all($code, TOKEN_PARSE);
$tokens = \PhpToken::tokenize($code, TOKEN_PARSE);
$code = '';
foreach ($tokens as $token) {
$code .= is_array($token)
? ($token[0] === T_FINAL ? '' : $token[1])
: $token;
$code .= $token->is(T_FINAL) ? '' : $token->text;
}
}

Expand Down

0 comments on commit 925b93b

Please sign in to comment.