Merge branch 'master' of github.com:boenrobot/ReflectionDocBlock into version-and-derivatives

This commit is contained in:
Vasil Rangelov
2012-11-13 19:05:14 +02:00
12 changed files with 112 additions and 92 deletions
+2 -4
View File
@@ -83,7 +83,7 @@ class DocBlock implements \Reflector
list($short, $long, $tags) = $this->splitDocBlock($docblock); list($short, $long, $tags) = $this->splitDocBlock($docblock);
$this->short_description = $short; $this->short_description = $short;
$this->long_description = new DocBlock\LongDescription($long); $this->long_description = new DocBlock\Description($long);
$this->parseTags($tags); $this->parseTags($tags);
$this->namespace = $namespace; $this->namespace = $namespace;
@@ -222,9 +222,7 @@ class DocBlock implements \Reflector
// create proper Tag objects // create proper Tag objects
foreach ($result as $key => $tag_line) { foreach ($result as $key => $tag_line) {
$tag = DocBlock\Tag::createInstance($tag_line); $result[$key] = DocBlock\Tag::createInstance($tag_line, $this);
$tag->setDocBlock($this);
$result[$key] = $tag;
} }
} }
@@ -13,13 +13,13 @@
namespace phpDocumentor\Reflection\DocBlock; namespace phpDocumentor\Reflection\DocBlock;
/** /**
* Parses a Long Description of a DocBlock. * Parses a Description of a DocBlock or tag.
* *
* @author Mike van Riel <mike.vanriel@naenius.com> * @author Mike van Riel <mike.vanriel@naenius.com>
* @license http://www.opensource.org/licenses/mit-license.php MIT * @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org * @link http://phpdoc.org
*/ */
class LongDescription implements \Reflector class Description implements \Reflector
{ {
/** @var string */ /** @var string */
protected $contents = ''; protected $contents = '';
@@ -30,15 +30,20 @@ class LongDescription implements \Reflector
/** @var \phpDocumentor\Reflection\DocBlock\Tags[] */ /** @var \phpDocumentor\Reflection\DocBlock\Tags[] */
protected $tags = array(); protected $tags = array();
/** @var DocBlock The DocBlock which this description belongs to. */
protected $docblock = null;
/** /**
* Parses the string for inline tags and if the Markdown class is included; * Parses the string for inline tags and if the Markdown class is included;
* format the found text. * format the found text.
* *
* @param string $content the DocBlock contents without asterisks. * @param string $content The DocBlock contents without asterisks.
* @param DocBlock $docblock The DocBlock which this description belongs to.
*/ */
public function __construct($content) public function __construct($content, DocBlock $docblock = null)
{ {
$this->contents = trim($content); $this->contents = trim($content);
$this->docblock = $docblock;
} }
/** /**
@@ -97,7 +102,8 @@ class LongDescription implements \Reflector
); );
for ($i=1, $l = count($this->parsedContents); $i<$l; $i += 2) { for ($i=1, $l = count($this->parsedContents); $i<$l; $i += 2) {
$this->parsedContents[$i] = Tag::createInstance( $this->parsedContents[$i] = Tag::createInstance(
$this->parsedContents[$i] $this->parsedContents[$i],
$this->docblock
); );
} }
+22 -24
View File
@@ -12,6 +12,8 @@
namespace phpDocumentor\Reflection\DocBlock; namespace phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock;
/** /**
* Parses a tag definition for a DocBlock. * Parses a tag definition for a DocBlock.
* *
@@ -36,8 +38,8 @@ class Tag implements \Reflector
/** @var int Line number of the tag */ /** @var int Line number of the tag */
protected $line_number = 0; protected $line_number = 0;
/** @var \phpDocumentor\Reflection\DocBlock docblock class */ /** @var DocBlock The DocBlock which this tag belongs to. */
protected $docblock; protected $docblock = null;
/** /**
* @var array An array with a tag as a key, and an FQCN to a class that * @var array An array with a tag as a key, and an FQCN to a class that
@@ -85,13 +87,16 @@ class Tag implements \Reflector
* Factory method responsible for instantiating the correct sub type. * Factory method responsible for instantiating the correct sub type.
* *
* @param string $tag_line The text for this tag, including description. * @param string $tag_line The text for this tag, including description.
* @param DocBlock $docblock The DocBlock which this tag belongs to.
* *
* @throws \InvalidArgumentException if an invalid tag line was presented. * @throws \InvalidArgumentException if an invalid tag line was presented.
* *
* @return \phpDocumentor\Reflection\DocBlock\Tag * @return static A new tag object.
*/ */
final public static function createInstance($tag_line) final public static function createInstance(
{ $tag_line,
DocBlock $docblock = null
) {
if (!preg_match( if (!preg_match(
'/^@([\w\-\_\\\\]+)(?:\s*([^\s].*)|$)?/us', '/^@([\w\-\_\\\\]+)(?:\s*([^\s].*)|$)?/us',
$tag_line, $tag_line,
@@ -106,10 +111,15 @@ class Tag implements \Reflector
$handler = self::$tagHandlerMappings[$matches[1]]; $handler = self::$tagHandlerMappings[$matches[1]];
return new $handler( return new $handler(
$matches[1], $matches[1],
isset($matches[2]) ? $matches[2] : '' isset($matches[2]) ? $matches[2] : '',
$docblock
); );
} }
return new self($matches[1], isset($matches[2]) ? $matches[2] : ''); return new self(
$matches[1],
isset($matches[2]) ? $matches[2] : '',
$docblock
);
} }
/** /**
@@ -149,12 +159,14 @@ class Tag implements \Reflector
* *
* @param string $type Name of the tag. * @param string $type Name of the tag.
* @param string $content The contents of the given tag. * @param string $content The contents of the given tag.
* @param DocBlock $docblock The DocBlock which this tag belongs to.
*/ */
public function __construct($type, $content) public function __construct($type, $content, DocBlock $docblock = null)
{ {
$this->tag = $type; $this->tag = $type;
$this->content = $content; $this->content = $content;
$this->description = $content; $this->description = trim($content);
$this->docblock = $docblock;
} }
/** /**
@@ -196,7 +208,7 @@ class Tag implements \Reflector
public function getParsedDescription() public function getParsedDescription()
{ {
if (null === $this->parsedDescription) { if (null === $this->parsedDescription) {
$description = new LongDescription($this->description); $description = new Description($this->description, $this->docblock);
$this->parsedDescription = $description->getParsedContents(); $this->parsedDescription = $description->getParsedContents();
} }
return $this->parsedDescription; return $this->parsedDescription;
@@ -224,20 +236,6 @@ class Tag implements \Reflector
return $this->line_number; return $this->line_number;
} }
/**
* Inject the docblock class
*
* This exposes some common functionality contained in the docblock abstract.
*
* @param object $docblock Object containing the DocBlock.
*
* @return void
*/
public function setDocBlock($docblock)
{
$this->docblock = $docblock;
}
/** /**
* Builds a string representation of this object. * Builds a string representation of this object.
* *
@@ -12,6 +12,7 @@
namespace phpDocumentor\Reflection\DocBlock\Tag; namespace phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Tag; use phpDocumentor\Reflection\DocBlock\Tag;
/** /**
@@ -33,12 +34,17 @@ class AuthorTag extends Tag
* Parses a tag and populates the member variables. * Parses a tag and populates the member variables.
* *
* @param string $type Tag identifier for this tag (should be 'author'). * @param string $type Tag identifier for this tag (should be 'author').
* @param string $content The contents of the given tag. * @param string $content Contents for this tag.
* @param DocBlock $docblock The DocBlock which this tag belongs to.
*/ */
public function __construct($type, $content) public function __construct($type, $content, DocBlock $docblock = null)
{ {
parent::__construct($type, $content); parent::__construct($type, $content, $docblock);
if (preg_match('/^([^\<]*)(\<([^\>]*)\>)?$/', $content, $matches)) { if (preg_match(
'/^([^\<]*)(\<([^\>]*)\>)?$/',
$this->description,
$matches
)) {
$this->name = trim($matches[1]); $this->name = trim($matches[1]);
if (isset($matches[3])) { if (isset($matches[3])) {
$this->email = trim($matches[3]); $this->email = trim($matches[3]);
@@ -12,6 +12,7 @@
namespace phpDocumentor\Reflection\DocBlock\Tag; namespace phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Tag; use phpDocumentor\Reflection\DocBlock\Tag;
/** /**
@@ -30,12 +31,13 @@ class LinkTag extends Tag
* Parses a tag and populates the member variables. * Parses a tag and populates the member variables.
* *
* @param string $type Tag identifier for this tag (should be 'link'). * @param string $type Tag identifier for this tag (should be 'link').
* @param string $content The contents of the given tag. * @param string $content Contents for this tag.
* @param DocBlock $docblock The DocBlock which this tag belongs to.
*/ */
public function __construct($type, $content) public function __construct($type, $content, DocBlock $docblock = null)
{ {
$this->tag = $type; parent::__construct($type, $content, $docblock);
$pieces = explode(' ', $content); $pieces = explode(' ', $this->description);
if (count($pieces) > 1) { if (count($pieces) > 1) {
$this->link = array_shift($pieces); $this->link = array_shift($pieces);
@@ -12,6 +12,9 @@
namespace phpDocumentor\Reflection\DocBlock\Tag; namespace phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Tag;
/** /**
* Reflection class for a @method in a Docblock. * Reflection class for a @method in a Docblock.
* *
@@ -32,12 +35,12 @@ class MethodTag extends ReturnTag
* Parses a tag and populates the member variables. * Parses a tag and populates the member variables.
* *
* @param string $type Tag identifier for this tag (should be 'method'). * @param string $type Tag identifier for this tag (should be 'method').
* @param string $content The contents of the given tag. * @param string $content Contents for this tag.
* @param DocBlock $docblock The DocBlock which this tag belongs to.
*/ */
public function __construct($type, $content) public function __construct($type, $content, DocBlock $docblock = null)
{ {
$this->tag = $type; Tag::__construct($type, $content, $docblock);
$this->content = $content;
$matches = array(); $matches = array();
// 1. none or more whitespace // 1. none or more whitespace
@@ -51,7 +54,7 @@ class MethodTag extends ReturnTag
if (preg_match( if (preg_match(
'/^[\s]*(?:([\w\|_\\\\]+)[\s]+)?(?:[\w_]+\(\)[\s]+)?([\w\|_\\\\]+)' '/^[\s]*(?:([\w\|_\\\\]+)[\s]+)?(?:[\w_]+\(\)[\s]+)?([\w\|_\\\\]+)'
.'\(([^\)]*)\)[\s]*(.*)/u', .'\(([^\)]*)\)[\s]*(.*)/u',
$content, $this->description,
$matches $matches
)) { )) {
list( list(
@@ -12,6 +12,9 @@
namespace phpDocumentor\Reflection\DocBlock\Tag; namespace phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Tag;
/** /**
* Reflection class for a @param tag in a Docblock. * Reflection class for a @param tag in a Docblock.
* *
@@ -31,14 +34,14 @@ class ParamTag extends ReturnTag
* *
* @param string $type Tag identifier for this tag (should be 'param'). * @param string $type Tag identifier for this tag (should be 'param').
* @param string $content Contents for this tag. * @param string $content Contents for this tag.
* @param DocBlock $docblock The DocBlock which this tag belongs to.
*/ */
public function __construct($type, $content) public function __construct($type, $content, DocBlock $docblock = null)
{ {
$this->tag = $type; Tag::__construct($type, $content, $docblock);
$this->content = $content;
$content = preg_split( $content = preg_split(
'/(\s+)/u', '/(\s+)/u',
trim($content), $this->description,
3, 3,
PREG_SPLIT_DELIM_CAPTURE PREG_SPLIT_DELIM_CAPTURE
); );
@@ -12,6 +12,7 @@
namespace phpDocumentor\Reflection\DocBlock\Tag; namespace phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Tag; use phpDocumentor\Reflection\DocBlock\Tag;
/** /**
@@ -31,13 +32,12 @@ class ReturnTag extends Tag
* *
* @param string $type Tag identifier for this tag (should be 'return'). * @param string $type Tag identifier for this tag (should be 'return').
* @param string $content Contents for this tag. * @param string $content Contents for this tag.
* @param DocBlock $docblock The DocBlock which this tag belongs to.
*/ */
public function __construct($type, $content) public function __construct($type, $content, DocBlock $docblock = null)
{ {
$this->tag = $type; parent::__construct($type, $content, $docblock);
$this->content = $content; $content = preg_split('/[\ \t]+/u', $this->description, 2);
$content = preg_split('/[\ \t]+/u', trim($content), 2);
// any output is considered a type // any output is considered a type
$this->type = array_shift($content); $this->type = array_shift($content);
@@ -12,6 +12,7 @@
namespace phpDocumentor\Reflection\DocBlock\Tag; namespace phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Tag; use phpDocumentor\Reflection\DocBlock\Tag;
/** /**
@@ -31,11 +32,11 @@ class SeeTag extends Tag
* *
* @param string $type Tag identifier for this tag (should be 'see'). * @param string $type Tag identifier for this tag (should be 'see').
* @param string $content Contents for this tag. * @param string $content Contents for this tag.
* @param DocBlock $docblock The DocBlock which this tag belongs to.
*/ */
public function __construct($type, $content) public function __construct($type, $content, DocBlock $docblock = null)
{ {
$this->tag = $type; parent::__construct($type, $content, $docblock);
$this->content = $content;
$content = preg_split('/\s+/u', $content); $content = preg_split('/\s+/u', $content);
// any output is considered a type // any output is considered a type
@@ -12,6 +12,9 @@
namespace phpDocumentor\Reflection\DocBlock\Tag; namespace phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Tag;
/** /**
* Reflection class for a @var tag in a Docblock. * Reflection class for a @var tag in a Docblock.
* *
@@ -26,12 +29,12 @@ class VarTag extends ParamTag
* *
* @param string $type Tag identifier for this tag (should be 'var'). * @param string $type Tag identifier for this tag (should be 'var').
* @param string $content Contents for this tag. * @param string $content Contents for this tag.
* @param DocBlock $docblock The DocBlock which this tag belongs to.
*/ */
public function __construct($type, $content) public function __construct($type, $content, DocBlock $docblock = null)
{ {
$this->tag = $type; Tag::__construct($type, $content, $docblock);
$this->content = $content; $content = preg_split('/\s+/u', $this->description);
$content = preg_split('/\s+/u', $content);
if (count($content) == 0) { if (count($content) == 0) {
return; return;
@@ -1,6 +1,6 @@
<?php <?php
/** /**
* phpDocumentor Long Description Test * phpDocumentor Description Test
* *
* PHP Version 5.3 * PHP Version 5.3
* *
@@ -13,21 +13,21 @@
namespace phpDocumentor\Reflection\DocBlock; namespace phpDocumentor\Reflection\DocBlock;
/** /**
* Test class for phpDocumentor\Reflection\DocBlock\LongDescription * Test class for phpDocumentor\Reflection\DocBlock\Description
* *
* @author Vasil Rangelov <boen.robot@gmail.com> * @author Vasil Rangelov <boen.robot@gmail.com>
* @copyright 2010-2011 Mike van Riel / Naenius. (http://www.naenius.com) * @copyright 2010-2011 Mike van Riel / Naenius. (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT * @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org * @link http://phpdoc.org
*/ */
class LongDescriptionTest extends \PHPUnit_Framework_TestCase class DescriptionTest extends \PHPUnit_Framework_TestCase
{ {
public function testConstruct() public function testConstruct()
{ {
$fixture = <<<LONGDESC $fixture = <<<LONGDESC
This is text for a description. This is text for a description.
LONGDESC; LONGDESC;
$object = new LongDescription($fixture); $object = new Description($fixture);
$this->assertSame($fixture, $object->getContents()); $this->assertSame($fixture, $object->getContents());
$parsedContents = $object->getParsedContents(); $parsedContents = $object->getParsedContents();
@@ -41,7 +41,7 @@ LONGDESC;
This is text for a {@link http://phpdoc.org/ description} that uses inline This is text for a {@link http://phpdoc.org/ description} that uses inline
tags. tags.
LONGDESC; LONGDESC;
$object = new LongDescription($fixture); $object = new Description($fixture);
$this->assertSame($fixture, $object->getContents()); $this->assertSame($fixture, $object->getContents());
$parsedContents = $object->getParsedContents(); $parsedContents = $object->getParsedContents();
@@ -64,7 +64,7 @@ tags.',
{@link http://phpdoc.org/ This} is text for a description that uses inline {@link http://phpdoc.org/ This} is text for a description that uses inline
tags. tags.
LONGDESC; LONGDESC;
$object = new LongDescription($fixture); $object = new Description($fixture);
$this->assertSame($fixture, $object->getContents()); $this->assertSame($fixture, $object->getContents());
$parsedContents = $object->getParsedContents(); $parsedContents = $object->getParsedContents();
@@ -88,7 +88,7 @@ tags.',
This is text for a description with {@internal inline tag with This is text for a description with {@internal inline tag with
{@link http://phpdoc.org another inline tag} in it}. {@link http://phpdoc.org another inline tag} in it}.
LONGDESC; LONGDESC;
$object = new LongDescription($fixture); $object = new Description($fixture);
$this->assertSame($fixture, $object->getContents()); $this->assertSame($fixture, $object->getContents());
$parsedContents = $object->getParsedContents(); $parsedContents = $object->getParsedContents();
@@ -119,7 +119,7 @@ LONGDESC;
$fixture = <<<LONGDESC $fixture = <<<LONGDESC
This is text for a description containing { that is literal. This is text for a description containing { that is literal.
LONGDESC; LONGDESC;
$object = new LongDescription($fixture); $object = new Description($fixture);
$this->assertSame($fixture, $object->getContents()); $this->assertSame($fixture, $object->getContents());
$parsedContents = $object->getParsedContents(); $parsedContents = $object->getParsedContents();
@@ -133,7 +133,7 @@ LONGDESC;
This is text for a description containing {@internal inline tag that has { that This is text for a description containing {@internal inline tag that has { that
is literal}. is literal}.
LONGDESC; LONGDESC;
$object = new LongDescription($fixture); $object = new Description($fixture);
$this->assertSame($fixture, $object->getContents()); $this->assertSame($fixture, $object->getContents());
$parsedContents = $object->getParsedContents(); $parsedContents = $object->getParsedContents();
@@ -160,7 +160,7 @@ is literal'),
$fixture = <<<LONGDESC $fixture = <<<LONGDESC
This is text for a description with {} that is not a tag. This is text for a description with {} that is not a tag.
LONGDESC; LONGDESC;
$object = new LongDescription($fixture); $object = new Description($fixture);
$this->assertSame($fixture, $object->getContents()); $this->assertSame($fixture, $object->getContents());
$parsedContents = $object->getParsedContents(); $parsedContents = $object->getParsedContents();
@@ -177,7 +177,7 @@ LONGDESC;
This is text for a description with {@internal inline tag with {} that is not an This is text for a description with {@internal inline tag with {} that is not an
inline tag}. inline tag}.
LONGDESC; LONGDESC;
$object = new LongDescription($fixture); $object = new Description($fixture);
$this->assertSame($fixture, $object->getContents()); $this->assertSame($fixture, $object->getContents());
$parsedContents = $object->getParsedContents(); $parsedContents = $object->getParsedContents();
@@ -204,7 +204,7 @@ inline tag'),
$fixture = <<<LONGDESC $fixture = <<<LONGDESC
This is text for a description with literal {{@}link}. This is text for a description with literal {{@}link}.
LONGDESC; LONGDESC;
$object = new LongDescription($fixture); $object = new Description($fixture);
$this->assertSame($fixture, $object->getContents()); $this->assertSame($fixture, $object->getContents());
$parsedContents = $object->getParsedContents(); $parsedContents = $object->getParsedContents();
@@ -221,7 +221,7 @@ LONGDESC;
This is text for a description with an {@internal inline tag with literal This is text for a description with an {@internal inline tag with literal
{{@}link{} in it}. {{@}link{} in it}.
LONGDESC; LONGDESC;
$object = new LongDescription($fixture); $object = new Description($fixture);
$this->assertSame($fixture, $object->getContents()); $this->assertSame($fixture, $object->getContents());
$parsedContents = $object->getParsedContents(); $parsedContents = $object->getParsedContents();
@@ -162,14 +162,14 @@ DOCBLOCK;
} }
require 'data:text/plain;base64,'. base64_encode( require 'data:text/plain;base64,'. base64_encode(
<<<'TAG_HANDLER' <<<DOCBLOCK_EXTENSION
<?php <?php
class MyReflectionDocBlock extends \phpDocumentor\Reflection\DocBlock { class MyReflectionDocBlock extends \phpDocumentor\Reflection\DocBlock {
protected function splitDocBlock($comment) { protected function splitDocBlock(\$comment) {
return array('', '', 'Invalid tag block'); return array('', '', 'Invalid tag block');
} }
} }
TAG_HANDLER DOCBLOCK_EXTENSION
); );
new \MyReflectionDocBlock(''); new \MyReflectionDocBlock('');