Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^96) CHAPTER 7 ■ REFLECTION API
By now, this should be getting repetitive. Dozens of functions in these classes need to be
overridden, and I’ve included only the most critical few in processing an OOP tree. Any func-
tion in the API that returns a Reflection class natively should be converted to a
DocumentingReflection
class and translated, just as getParameters() and getMethods() were
translated.


Updating the Parser to Handle In-Line Tags


Now we need to return to the original documentation parser. The parser you created earlier in
the chapter does not respect any of the in-line PHPDoc tags. As an example, Listing 7-20 shows
a parser capable of processing the in-line link tag.

Listing 7-20. Processing In-Line Link Tags (DocumentingReflection.php)

public static function ParseDocComment($docComment) {

$returnData = $comments = $tags = array();
$tagNames = $tagData = array();

$tokens = docblock_tokenize($docComment,true);

foreach($tokens as $token) {
switch( $token[0] ) {

case DOCBLOCK_INLINETAG:
$inlineTag = trim($token[1], ' @{}');
break;

case DOCBLOCK_ENDINLINETAG:
switch($inlineTag) {
case 'link':
$inlineTagContents = preg_split("/[\s\t]+/", trim($inlineTagData), 2);
$data = '<a href="'. $inlineTagContents[0];
$data .= '">'. $inlineTagContents[1] .'</a>';
break;
}

if(array_key_exists($tagId, $tagData)) {
$tagData[$tagId] .= ' '. $data;
} else {
$tagData[$tagId] = $data;
}

unset($inlineTag, $inlineTagData, $inlineTagContents);
break;

case DOCBLOCK_INLINETAGCONTENTS:
$addData = trim($token[1], ' }');

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

Free download pdf