mirror of
https://github.com/barryvdh/ReflectionDocBlock.git
synced 2026-08-18 01:57:13 +00:00
Merge branch 'master' of git://github.com/phpDocumentor/ReflectionDocBlock
This commit is contained in:
@@ -36,14 +36,36 @@ class DocBlock implements \Reflector
|
||||
*/
|
||||
protected $tags = array();
|
||||
|
||||
/** @var string the current namespace */
|
||||
protected $namespace = '\\';
|
||||
|
||||
/** @var string[] List of namespace aliases => Fully Qualified Namespace */
|
||||
protected $namespace_aliases = array();
|
||||
|
||||
/**
|
||||
* Parses the given docblock and populates the member fields.
|
||||
*
|
||||
* @param string|\Reflector $docblock A docblock comment (including asterisks)
|
||||
* The constructor may also receive namespace information such as the
|
||||
* current namespace and aliases. This information is used in the
|
||||
* {@link expandType()} method to transform a relative Type into a FQCN.
|
||||
*
|
||||
* For example the param and return tags use this to expand their type
|
||||
* information.
|
||||
*
|
||||
* @param \Reflector|string $docblock A docblock comment (including asterisks)
|
||||
* or reflector supporting the getDocComment method.
|
||||
* @param string $namespace The namespace where this DocBlock resides in;
|
||||
* defaults to `\`.
|
||||
* @param string[] $namespace_aliases a list of namespace aliases as
|
||||
* provided by the `use` keyword; the key of the array is the alias name
|
||||
* or last part of the alias array if no alias name is provided.
|
||||
*
|
||||
* @throws \InvalidArgumentException if the given argument does not have the
|
||||
* getDocComment method.
|
||||
*/
|
||||
public function __construct($docblock)
|
||||
{
|
||||
public function __construct(
|
||||
$docblock, $namespace = '\\', $namespace_aliases = array()
|
||||
) {
|
||||
if (is_object($docblock)) {
|
||||
if (!method_exists($docblock, 'getDocComment')) {
|
||||
throw new \InvalidArgumentException(
|
||||
@@ -61,6 +83,9 @@ class DocBlock implements \Reflector
|
||||
$this->short_description = $short;
|
||||
$this->long_description = new DocBlock\LongDescription($long);
|
||||
$this->parseTags($tags);
|
||||
|
||||
$this->namespace = $namespace;
|
||||
$this->namespace_aliases = $namespace_aliases;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -192,7 +217,9 @@ class DocBlock implements \Reflector
|
||||
|
||||
// create proper Tag objects
|
||||
foreach ($result as $key => $tag_line) {
|
||||
$result[$key] = DocBlock\Tag::createInstance($tag_line);
|
||||
$tag = DocBlock\Tag::createInstance($tag_line);
|
||||
$tag->setDocBlock($this);
|
||||
$result[$key] = $tag;
|
||||
}
|
||||
|
||||
$this->tags = $result;
|
||||
@@ -271,6 +298,84 @@ class DocBlock implements \Reflector
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to expand a type to it's full namespaced equivalent (FQCN).
|
||||
*
|
||||
* This method will take the given type and examine the current namespace
|
||||
* and namespace aliases to see whether it should expand it into a FQCN
|
||||
* as defined by the rules in PHP.
|
||||
*
|
||||
* @param string $type Type to expand into full namespaced
|
||||
* equivalent.
|
||||
* @param string[] $ignore_keywords Whether to ignore given keywords, when
|
||||
* null it will use the default keywords: 'string', 'int', 'integer',
|
||||
* 'bool', 'boolean', 'float', 'double', 'object', 'mixed', 'array',
|
||||
* 'resource', 'void', 'null', 'callback', 'false', 'true'.
|
||||
* Default value for this parameter is null.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function expandType($type, $ignore_keywords = null)
|
||||
{
|
||||
if ($type === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($ignore_keywords === null) {
|
||||
$ignore_keywords = array(
|
||||
'string', 'int', 'integer', 'bool', 'boolean', 'float', 'double',
|
||||
'object', 'mixed', 'array', 'resource', 'void', 'null',
|
||||
'callback', 'false', 'true'
|
||||
);
|
||||
}
|
||||
|
||||
$namespace = '';
|
||||
if ($this->namespace != 'default' && $this->namespace != 'global') {
|
||||
$namespace = rtrim($this->namespace, '\\') . '\\';
|
||||
}
|
||||
|
||||
$type = explode('|', $type);
|
||||
foreach ($type as &$item) {
|
||||
$item = trim($item);
|
||||
|
||||
// add support for array notation
|
||||
$is_array = false;
|
||||
if (substr($item, -2) == '[]') {
|
||||
$item = substr($item, 0, -2);
|
||||
$is_array = true;
|
||||
}
|
||||
|
||||
if ((substr($item, 0, 1) != '\\')
|
||||
&& (!in_array(strtolower($item), $ignore_keywords))
|
||||
) {
|
||||
$type_parts = explode('\\', $item);
|
||||
|
||||
// if the first segment is an alias; replace with full name
|
||||
if (isset($this->namespace_aliases[$type_parts[0]])) {
|
||||
$type_parts[0] = $this->namespace_aliases[$type_parts[0]];
|
||||
$item = implode('\\', $type_parts);
|
||||
} else {
|
||||
// otherwise prepend the current namespace
|
||||
$item = $namespace . $item;
|
||||
}
|
||||
}
|
||||
|
||||
// full paths always start with a slash
|
||||
if (isset($item[0]) && ($item[0] !== '\\')
|
||||
&& (!in_array(strtolower($item), $ignore_keywords))
|
||||
) {
|
||||
$item = '\\' . $item;
|
||||
}
|
||||
|
||||
// re-add the array notation markers
|
||||
if ($is_array) {
|
||||
$item .= '[]';
|
||||
}
|
||||
}
|
||||
|
||||
return implode('|', $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a string representation of this object.
|
||||
*
|
||||
|
||||
@@ -33,7 +33,7 @@ class Tag implements \Reflector
|
||||
/** @var int line number of the tag */
|
||||
protected $line_number = 0;
|
||||
|
||||
/** @var object docblock class */
|
||||
/** @var \phpDocumentor\Reflection\DocBlock docblock class */
|
||||
protected $docblock;
|
||||
|
||||
/**
|
||||
|
||||
@@ -63,7 +63,12 @@ class ParamTag extends Tag
|
||||
public function getTypes()
|
||||
{
|
||||
$types = explode('|', $this->type);
|
||||
array_walk($types, 'trim');
|
||||
foreach ($types as &$type) {
|
||||
$type = !empty($type) && $this->docblock
|
||||
? $this->docblock->expandType($type)
|
||||
: trim($type);
|
||||
}
|
||||
|
||||
return $types;
|
||||
}
|
||||
|
||||
@@ -74,7 +79,7 @@ class ParamTag extends Tag
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
return $this->docblock->expandType($this->type);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -42,25 +42,4 @@ class ReturnTag extends ParamTag
|
||||
$this->description = implode(' ', $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the variable.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTypes()
|
||||
{
|
||||
$types = explode('|', $this->type);
|
||||
array_walk($types, 'trim');
|
||||
return $types;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the variable.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user