4 * Reads PHP code and returns the FQCN of every class defined within it.
9 * @var string Current namespace
11 protected $namespace = '';
14 * @var array List of FQCN detected in this pass
19 * @var array Token from token_get_all() that started an expect sequence
21 protected $startToken;
24 * @var array List of tokens that are members of the current expect sequence
29 * @var string $code PHP code (including <?php) to detect class names from
30 * @return array List of FQCN detected within the tokens
32 public function getClasses( $code ) {
33 $this->namespace = '';
35 $this->startToken
= null;
38 foreach ( token_get_all( $code ) as $token ) {
39 if ( $this->startToken
=== null ) {
40 $this->tryBeginExpect( $token );
42 $this->tryEndExpect( $token );
46 return $this->classes
;
50 * Determine if $token begins the next expect sequence.
54 protected function tryBeginExpect( $token ) {
55 if ( is_string( $token ) ) {
58 switch ( $token[0] ) {
64 $this->startToken
= $token;
69 * Accepts the next token in an expect sequence
73 protected function tryEndExpect( $token ) {
74 switch ( $this->startToken
[0] ) {
76 // Skip over T_CLASS after T_DOUBLE_COLON because this is something like
77 // "self::static" which accesses the class name. It doens't define a new class.
78 $this->startToken
= null;
81 if ( $token === ';' ||
$token === '{' ) {
82 $this->namespace = $this->implodeTokens() . '\\';
84 $this->tokens
[] = $token;
91 $this->tokens
[] = $token;
92 if ( is_array( $token ) && $token[0] === T_STRING
) {
93 $this->classes
[] = $this->namespace . $this->implodeTokens();
99 * Returns the string representation of the tokens within the
100 * current expect sequence and resets the sequence.
104 protected function implodeTokens() {
106 foreach ( $this->tokens
as $token ) {
107 $content[] = is_string( $token ) ?
$token : $token[1];
111 $this->startToken
= null;
113 return trim( implode( '', $content ), " \n\t" );