thelolcat
thelolcat

Reputation: 11605

How to get the file's DocComment?

The file looks like this:

<?php
/**
 *  Title
 *
 *  Description
 *  bla bla..
 *
 *  @since 1.0
 *  @author lolcat
 */





/**
 *  Some class..
 *
 *  @since 1.0
 */
class BlaBla{

}
..

I want to get the first DocComment from it. Is it possible with PHP?

Reflection seems to have such a feature, but it only works for classes or functions, not files :(

Upvotes: 2

Views: 659

Answers (1)

Buddy
Buddy

Reputation: 1836

There is a basic extension for that kind of things. It's called Tokenizer.

Use token_get_all() function.

Every token in returned array has it's id. There are global constants that define them: http://www.php.net/manual/en/tokens.php.

If you use PHP >= 5.3:

$s = file_get_contents('tokexample.php');

$docblock = reset(array_filter(token_get_all($s), 
                  function($item) { 
                      return $item[0] == T_DOC_COMMENT; 
                  }));

echo $docblock[1];

/**
 *  Title
 *
 *  Description
 *  bla bla..
 *
 *  @since 1.0
 *  @author lolcat
 */

Else:

function filter($item) {
    return $item[0] == T_DOC_COMMENT;
}

$docblock = reset(array_filter(token_get_all($s), "filter"));

echo $docblock[1];

Upvotes: 4

Related Questions