Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^84) CHAPTER 7 ■ REFLECTION API
The result of this code is as follows:


1=DOCBLOCK_NEWLINE=

2=DOCBLOCK_WHITESPACE=

36=DOCBLOCK_TEXT=This is a doccomment
1=DOCBLOCK_NEWLINE=

1=DOCBLOCK_NEWLINE=

2=DOCBLOCK_WHITESPACE=
36=DOCBLOCK_TEXT=This doccomment documents the class demo
1=DOCBLOCK_NEWLINE=

This result might look a little confusing, and rightly so—it’s just a bunch of constants. But
the data you need is there.
The result of tokenization is an array of arrays. Each entry array represents one part of the
docblock. Each part is a token constant and its associated data. For the purposes of this token-
ization, you are concerned only with tags and text.

Parsing the Tokens.


Now you’re ready to perform the parsing process. Listing 7-11 shows a simple parser.

Listing 7-11. A Basic Docblock Parser

/**
* This is a doccomment comment
*
* This doccomment documents the class demo
*
* @author Kevin McArthur
* @example This is a multiline example
* and this is more than valid
* @example This is another separate example.
*/
class demo {}

$reflectionClass = new ReflectionClass('demo');
$docComment = $reflectionClass->getDocComment();

$tokens = docblock_tokenize($docComment,true);

$comments = array();
$tags = array();
$tagdata = array();

McArthur_819-9C07.fm Page 84 Friday, February 22, 2008 8:59 AM

Free download pdf