[id, text, line]. */ public array $t = []; public int $n = 0; /** @var array An opening bracket's index => its closing bracket's, and the reverse. */ public array $match = []; /** * The functions, methods, closures and arrow functions, then the file's own code (kind "file"). * The body runs from `start` to `end` (exclusive); `decl` is the keyword's index. * * @var array, decl: int, start: int, end: int, line: int}> */ public array $units = []; /** @var array By lower-case name. */ public array $classes = []; /** @var array A nested function or class keyword's index => the index just after it. */ public array $skip = []; /** @var string[] */ private array $lines; private function __construct( string $file, string $code ) { $this->file = $file; $this->lines = explode( "\n", $code ); } /** * Null when the file is too large or can't be tokenised. */ public static function parse( string $code, string $file = '' ): ?self { if ( strlen( $code ) > PhpSource::MAX_BYTES ) { return null; } try { // TOKEN_PARSE reads keywords used as method names right ($x->list()); it throws on code // for a newer PHP, which the plain tokenizer still reads well enough. $raw = token_get_all( $code, TOKEN_PARSE ); } catch ( \Throwable $e ) { try { $raw = token_get_all( $code ); } catch ( \Throwable $e2 ) { return null; } } $source = new self( $file, $code ); $source->tokens( $raw ); $source->brackets(); $source->structure(); return $source; } /** * @param array $raw From token_get_all(). */ private function tokens( array $raw ): void { $drop = [ T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG ]; $names = []; foreach ( [ 'T_NAME_QUALIFIED', 'T_NAME_FULLY_QUALIFIED', 'T_NAME_RELATIVE' ] as $constant ) { if ( defined( $constant ) ) { $names[] = (int) constant( $constant ); } } $line = 1; $pending = false; foreach ( $raw as $token ) { if ( ! is_array( $token ) ) { $this->t[] = [ self::CHAR, $token, $line ]; $pending = false; continue; } list( $id, $text, $start ) = $token; $line = $start + substr_count( $text, "\n" ); if ( in_array( $id, $drop, true ) ) { continue; } if ( in_array( $id, $names, true ) ) { $this->t[] = [ T_STRING, ltrim( $text, '\\' ), $start ]; $pending = false; continue; } // PHP 7.4 splits Foo\Bar into T_STRING T_NS_SEPARATOR T_STRING: merge them. if ( T_NS_SEPARATOR === $id ) { $last = count( $this->t ) - 1; if ( $last >= 0 && T_STRING === $this->t[ $last ][0] && ! $pending ) { $this->t[ $last ][1] .= '\\'; } else { $this->t[] = [ T_STRING, '', $start ]; } $pending = true; continue; } if ( T_STRING === $id && $pending ) { $this->t[ count( $this->t ) - 1 ][1] .= $text; $pending = false; continue; } $pending = false; $this->t[] = [ $id, $text, $start ]; } $this->n = count( $this->t ); } private function brackets(): void { $stack = []; for ( $i = 0; $i < $this->n; $i++ ) { list( $id, $text ) = $this->t[ $i ]; if ( ( self::CHAR === $id && ( '(' === $text || '[' === $text || '{' === $text ) ) || T_CURLY_OPEN === $id || T_DOLLAR_OPEN_CURLY_BRACES === $id ) { $stack[] = $i; } elseif ( self::CHAR === $id && ( ')' === $text || ']' === $text || '}' === $text ) && [] !== $stack ) { $open = array_pop( $stack ); $this->match[ $open ] = $i; $this->match[ $i ] = $open; } } } private function structure(): void { $scopes = []; for ( $i = 0; $i < $this->n; $i++ ) { while ( [] !== $scopes && end( $scopes )[1] < $i ) { array_pop( $scopes ); } $id = $this->t[ $i ][0]; if ( in_array( $id, [ T_CLASS, T_INTERFACE, T_TRAIT ], true ) || ( defined( 'T_ENUM' ) && constant( 'T_ENUM' ) === $id ) ) { if ( $i > 0 && T_DOUBLE_COLON === $this->t[ $i - 1 ][0] ) { continue; } $end = $this->class_at( $i ); if ( null !== $end ) { $scopes[] = [ 'class', $end ]; $this->skip[ $i ] = $end + 1; } continue; } if ( T_FUNCTION === $id ) { $in_class = [] !== $scopes && 'class' === end( $scopes )[0]; $end = $this->function_at( $i, $in_class ); if ( null !== $end ) { $scopes[] = [ 'fn', $end ]; $this->skip[ $i ] = $end + 1; } continue; } if ( T_FN === $id && ( $this->is( $i + 1, '(' ) || $this->is( $i + 1, '&' ) ) ) { $end = $this->arrow_at( $i ); if ( null !== $end ) { $this->skip[ $i ] = $end; } } } $this->units[] = [ 'kind' => 'file', 'name' => '', 'class' => null, 'params' => [], 'decl' => -1, 'start' => 0, 'end' => $this->n, 'line' => 1, ]; } /** * Records a class, interface, trait or enum; returns its closing brace. */ private function class_at( int $i ): ?int { $name = 'class@anonymous'; $extends = null; if ( isset( $this->t[ $i + 1 ] ) && T_STRING === $this->t[ $i + 1 ][0] ) { $name = $this->t[ $i + 1 ][1]; } for ( $k = $i + 1; $k < $this->n; $k++ ) { if ( $this->is( $k, '(' ) && isset( $this->match[ $k ] ) ) { $k = $this->match[ $k ]; continue; } if ( T_EXTENDS === $this->t[ $k ][0] && isset( $this->t[ $k + 1 ] ) && T_STRING === $this->t[ $k + 1 ][0] ) { $extends = $this->t[ $k + 1 ][1]; } if ( $this->is( $k, '{' ) ) { if ( ! isset( $this->match[ $k ] ) ) { return null; } $this->classes[ strtolower( $name ) ] = [ 'name' => $name, 'extends' => $extends, 'start' => $k, 'end' => $this->match[ $k ], ]; return $this->match[ $k ]; } if ( $this->is( $k, ';' ) ) { return null; } } return null; } /** * Records a function, method or closure; returns the closing brace of its body. */ private function function_at( int $i, bool $in_class ): ?int { $j = $i + 1; if ( $this->is( $j, '&' ) ) { ++$j; } $name = ''; if ( isset( $this->t[ $j ] ) && ! $this->is( $j, '(' ) ) { $name = $this->t[ $j ][1]; ++$j; } if ( ! $this->is( $j, '(' ) || ! isset( $this->match[ $j ] ) ) { return null; } $params = $this->params( $j ); $k = $this->match[ $j ] + 1; if ( isset( $this->t[ $k ] ) && T_USE === $this->t[ $k ][0] && $this->is( $k + 1, '(' ) && isset( $this->match[ $k + 1 ] ) ) { $k = $this->match[ $k + 1 ] + 1; } while ( $k < $this->n && ! $this->is( $k, '{' ) && ! $this->is( $k, ';' ) ) { ++$k; } if ( ! $this->is( $k, '{' ) || ! isset( $this->match[ $k ] ) ) { return null; } $class = null; if ( $in_class && '' !== $name ) { $class = $this->class_name_at( $i ); } $this->units[] = [ 'kind' => '' === $name ? 'closure' : ( null === $class ? 'function' : 'method' ), 'name' => $name, 'class' => '' === $name ? $this->class_name_at( $i ) : $class, 'params' => $params, 'decl' => $i, 'start' => $k + 1, 'end' => $this->match[ $k ], 'line' => $this->t[ $i ][2], ]; return $this->match[ $k ]; } /** * Records an arrow function; returns the index just after its expression. */ private function arrow_at( int $i ): ?int { $j = $i + 1; if ( $this->is( $j, '&' ) ) { ++$j; } if ( ! $this->is( $j, '(' ) || ! isset( $this->match[ $j ] ) ) { return null; } $params = $this->params( $j ); $k = $this->match[ $j ] + 1; while ( $k < $this->n && T_DOUBLE_ARROW !== $this->t[ $k ][0] ) { if ( $this->is( $k, ';' ) || $this->is( $k, '{' ) ) { return null; } ++$k; } $start = $k + 1; $end = $this->expr_end( $start, $this->n, true ); $this->units[] = [ 'kind' => 'arrow', 'name' => '', 'class' => $this->class_name_at( $i ), 'params' => $params, 'decl' => $i, 'start' => $start, 'end' => $end, 'line' => $this->t[ $i ][2], ]; return $end; } /** * @return array */ private function params( int $open ): array { $out = []; foreach ( $this->args( $open ) as $range ) { $type = ''; for ( $k = $range[0]; $k < $range[1]; $k++ ) { $id = $this->t[ $k ][0]; if ( T_VARIABLE === $id ) { $out[] = [ 'name' => $this->t[ $k ][1], 'type' => $type, ]; break; } if ( T_STRING === $id || T_ARRAY === $id || T_CALLABLE === $id ) { $type = $this->t[ $k ][1]; } } } return $out; } /** * The name of the innermost class around a token, or null. */ public function class_name_at( int $i ): ?string { $best = null; foreach ( $this->classes as $class ) { if ( $class['start'] < $i && $i < $class['end'] && ( null === $best || $class['start'] > $best['start'] ) ) { $best = $class; } } return null === $best ? null : $best['name']; } public function is( int $i, string $char ): bool { return isset( $this->t[ $i ] ) && self::CHAR === $this->t[ $i ][0] && $char === $this->t[ $i ][1]; } public function id( int $i ): int { return $this->t[ $i ][0] ?? -1; } public function text( int $i ): string { return $this->t[ $i ][1] ?? ''; } public function line( int $i ): int { return $this->t[ $i ][2] ?? 0; } /** * The closing bracket of an opening one at $i, or $i when it has none. */ public function close( int $i ): int { return $this->match[ $i ] ?? $i; } /** * The items of a bracketed list whose opening bracket is at $open, split at top-level commas, * as [start, end) ranges. An empty list gives []. * * @return array */ public function args( int $open ): array { if ( ! isset( $this->match[ $open ] ) ) { return []; } $close = $this->match[ $open ]; $out = []; $start = $open + 1; for ( $k = $open + 1; $k < $close; $k++ ) { if ( isset( $this->match[ $k ] ) && $this->match[ $k ] > $k ) { $k = $this->match[ $k ]; continue; } if ( $this->is( $k, ',' ) ) { $out[] = [ $start, $k ]; $start = $k + 1; } } if ( $start < $close ) { $out[] = [ $start, $close ]; } return $out; } /** * Where an expression that starts at $from ends: at a top-level ';', ',', '?>' or `as`, or at a * closing bracket that isn't its own. With $arrow, also at a top-level '=>' that isn't inside it * (an arrow function inside an array). */ public function expr_end( int $from, int $limit, bool $arrow = false ): int { for ( $k = $from; $k < $limit; $k++ ) { $id = $this->t[ $k ][0]; if ( isset( $this->match[ $k ] ) ) { if ( $this->match[ $k ] > $k ) { $k = $this->match[ $k ]; continue; } return $k; } if ( self::CHAR === $id && ( ';' === $this->t[ $k ][1] || ',' === $this->t[ $k ][1] ) ) { return $k; } if ( T_CLOSE_TAG === $id || T_AS === $id || T_INLINE_HTML === $id || ( $arrow && T_DOUBLE_ARROW === $id ) ) { return $k; } } return $limit; } /** * The tokens' text joined, for comparing two expressions. */ public function norm( int $a, int $b ): string { $out = ''; for ( $k = $a; $k < $b && $k < $this->n; $k++ ) { $out .= $this->t[ $k ][1]; } return $out; } /** * A plain string literal's value ('…' or "…" with no variables), or null. */ public function literal( int $i ): ?string { if ( ! isset( $this->t[ $i ] ) || T_CONSTANT_ENCAPSED_STRING !== $this->t[ $i ][0] ) { return null; } $text = $this->t[ $i ][1]; $body = substr( $text, 1, -1 ); return "'" === $text[0] ? str_replace( [ "\\'", '\\\\' ], [ "'", '\\' ], $body ) : stripcslashes( $body ); } /** * The literal an expression [a, b) is made of alone, or null. * * @param array{0: int, 1: int} $range The expression. */ public function only_literal( array $range ): ?string { return 1 === $range[1] - $range[0] ? $this->literal( $range[0] ) : null; } /** * The items of an array literal whose bracket ('[' or the '(' after `array`) is at $open: * each with its key when the key is a string literal, and its value's range. * * @return array */ public function items( int $open ): array { $out = []; foreach ( $this->args( $open ) as $range ) { $arrow = null; for ( $k = $range[0]; $k < $range[1]; $k++ ) { if ( isset( $this->match[ $k ] ) && $this->match[ $k ] > $k ) { $k = $this->match[ $k ]; continue; } if ( T_DOUBLE_ARROW === $this->t[ $k ][0] ) { $arrow = $k; break; } } if ( null === $arrow ) { $out[] = [ 'key' => null, 'value' => $range, ]; continue; } $out[] = [ 'key' => $this->only_literal( [ $range[0], $arrow ] ), 'value' => [ $arrow + 1, $range[1] ], ]; } return $out; } /** * The bracket that opens an array literal at $i ('[' or `array(`), or null. */ public function array_open( int $i ): ?int { if ( $this->is( $i, '[' ) ) { return $i; } if ( isset( $this->t[ $i ] ) && T_ARRAY === $this->t[ $i ][0] && $this->is( $i + 1, '(' ) ) { return $i + 1; } return null; } /** * The source line, trimmed, for evidence. */ public function excerpt( int $line ): string { return trim( $this->lines[ $line - 1 ] ?? '' ); } }