Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^88) CHAPTER 7 ■ REFLECTION API
} else {
$tags[$tagName] = $data;
}
}
$returnData['comments'] = $comments;
$returnData['tags'] = $tags;
$returnData['tokens'] = $tokens;
return $returnData;
}
}
Now you have defined the static function ParseDocComment. This is where you will add
additional processing logic, such as handling in-line tags. But before you get to that, you need
to extend some of the other reflection classes.


Extending Reflection Classes


The next class you will extend is the ReflectionMethod class. This is because it is the most
generalized reflector implementor that still contains a getDocComment() method. You also need
to extend the ReflectionParameter class, but you need to get the data from the associated
method, and thus the method class extension must logically come first. Listing 7-13 shows a
very basic reflection extension that integrates ParseDocComment with the ReflectionMethod class
to create a DocumentingReflectionMethod class.

Listing 7-13. Creating the DocumentingReflectionMethod Class (DocumentingReflection.php)

class DocumentingReflectionMethod extends ReflectionMethod {

protected $_comments, $_tags, $_tokens, $_declaringClass;

public function __construct($object, $method) {
parent::__construct($object, $method);

$docComment = $this->getDocComment();
$this->_declaringClass = $object;

$parsedComment = DocumentingReflection::ParseDocComment($docComment);

$this->_comments = $parsedComment['comments'];
$this->_tags = $parsedComment['tags'];
$this->_tokens = $parsedComment['tokens'];

}

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

Free download pdf