Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 7 ■ REFLECTION API^91

Next, you need to create an extension class for ReflectionParameter. Since there is no
doccomment associated with parameters, you will need to obtain the parameter data from
the param tags of the associated methods’ doccomments. To do this, you must customize
ReflectionParameter to fetch the data from the method, as follows.

public void ReflectionParameter::__construct(mixed function, mixed parameter)

Notice that the function parameter is of mixed type. This parameter may be passed a
numeric array consisting of a class name string or object instance and a method name string.
The code for extending ReflectionParameter is shown in Listing 7-15.

Listing 7-15. Extending ReflectionParameter (DocumentingReflection.php)

class DocumentingReflectionParameter extends ReflectionParameter {
protected $_reflectionMethod, $_reflectionClass, $_comment, $_type;

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

$this->_comment = '';
$this->_type = 'undefined';

$this->_reflectionMethod =
new DocumentingReflectionMethod($method[0], $method[1]);

$tags = $this->_reflectionMethod->getParsedTags();

if(array_key_exists('param', $tags)) {
$params = $tags['param'];

if(is_array($params)) {
foreach($params as $param) {
if($this->_isParamTag($this->getName(), $param)) {
$paramFound = $param;
}
}
} else {
if($this->_isParamTag($this->getName(), $params)) {
$paramFound = $params;
}
}
if(isset($paramFound)) {
$tokens = preg_split("/[\s\t]+/", $paramFound, 3);
$this->_comment = $tokens[2];
$this->_type = $tokens[0];
}
}
}

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

Free download pdf