From c70be2a745e5944a768a23994341bd256fd3f40e Mon Sep 17 00:00:00 2001 From: Vasil Rangelov Date: Tue, 13 Nov 2012 18:39:54 +0200 Subject: [PATCH] Added reflection for @source tag. --- .../Reflection/DocBlock/Tag/SourceTag.php | 83 ++++++++++++ .../Reflection/DocBlock/Tag/SourceTagTest.php | 118 ++++++++++++++++++ 2 files changed, 201 insertions(+) create mode 100644 src/phpDocumentor/Reflection/DocBlock/Tag/SourceTag.php create mode 100644 tests/phpDocumentor/Reflection/DocBlock/Tag/SourceTagTest.php diff --git a/src/phpDocumentor/Reflection/DocBlock/Tag/SourceTag.php b/src/phpDocumentor/Reflection/DocBlock/Tag/SourceTag.php new file mode 100644 index 0000000..09b4612 --- /dev/null +++ b/src/phpDocumentor/Reflection/DocBlock/Tag/SourceTag.php @@ -0,0 +1,83 @@ + + * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com) + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link http://phpdoc.org + */ + +namespace phpDocumentor\Reflection\DocBlock\Tag; + +use phpDocumentor\Reflection\DocBlock\Tag; + +/** + * Reflection class for a @source tag in a Docblock. + * + * @author Vasil Rangelov + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link http://phpdoc.org + */ +class SourceTag extends Tag +{ + /** + * @var int The starting line, relative to the structural element's + * location. + */ + protected $startingLine = 1; + + /** + * @var int|null The number of lines, relative to the starting line. NULL + * means "to the end". + */ + protected $lineCount = null; + + /** + * Parses a tag and populates the member variables. + * + * @param string $type Tag identifier for this tag (should be 'source'). + * @param string $content Contents for this tag. + * @param DocBlock $docblock The DocBlock which this tag belongs to. + */ + public function __construct($type, $content, DocBlock $docblock = null) + { + parent::__construct($type, $content, $docblock); + $content = preg_split('/[\ \t]+/u', $this->description, 2); + if (preg_match( + '/^([1-9]\d*)\s*(?:([1-9]\d*)\s+)?(.*)$/su', + $this->description, + $matches + )) { + $this->startingLine = (int)$matches[1]; + if (isset($matches[2]) && '' !== $matches[2]) { + $this->lineCount = (int)$matches[2]; + } + $this->description = $matches[3]; + } + } + + /** + * Returns the starting line. + * + * @return int The starting line, relative to the structural element's + * location. + */ + public function getStartingLine() + { + return $this->startingLine; + } + + /** + * Returns the number of lines. + * + * @return int|null The number of lines, relative to the starting line. NULL + * means "to the end". + */ + public function getLineCount() + { + return $this->lineCount; + } +} diff --git a/tests/phpDocumentor/Reflection/DocBlock/Tag/SourceTagTest.php b/tests/phpDocumentor/Reflection/DocBlock/Tag/SourceTagTest.php new file mode 100644 index 0000000..7667598 --- /dev/null +++ b/tests/phpDocumentor/Reflection/DocBlock/Tag/SourceTagTest.php @@ -0,0 +1,118 @@ + + * @copyright 2010-2011 Mike van Riel / Naenius. (http://www.naenius.com) + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link http://phpdoc.org + */ + +namespace phpDocumentor\Reflection\DocBlock\Tag; + +/** + * Test class for \phpDocumentor\Reflection\DocBlock\Tag\SourceTag + * + * @author Vasil Rangelov + * @copyright 2010-2011 Mike van Riel / Naenius. (http://www.naenius.com) + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link http://phpdoc.org + */ +class SourceTagTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test that the \phpDocumentor\Reflection\DocBlock\Tag\SourceTag can + * understand the @source DocBlock. + * + * @param string $type + * @param string $content + * @param string $exContent + * @param string $exStartingLine + * @param string $exLineCount + * + * @covers \phpDocumentor\Reflection\DocBlock\Tag\SourceTag::__construct + * @covers \phpDocumentor\Reflection\DocBlock\Tag\SourceTag::getStartingLine + * @covers \phpDocumentor\Reflection\DocBlock\Tag\SourceTag::getLineCount + * @dataProvider provideDataForConstuctor + * + * @return void + */ + public function testConstructorParesInputsIntoCorrectFields( + $type, + $content, + $exContent, + $exDescription, + $exStartingLine, + $exLineCount + ) { + $tag = new SourceTag($type, $content); + + $this->assertEquals($type, $tag->getName()); + $this->assertEquals($exContent, $tag->getContent()); + $this->assertEquals($exDescription, $tag->getDescription()); + $this->assertEquals($exStartingLine, $tag->getStartingLine()); + $this->assertEquals($exLineCount, $tag->getLineCount()); + } + + /** + * Data provider for testConstructorParesInputsIntoCorrectFields + * + * @return array + */ + public function provideDataForConstuctor() + { + // $type, $content, $exContent, $exDescription, $exStartingLine, $exLineCount + return array( + array( + 'source', + '2', + '2', + '', + 2, + null + ), + array( + 'source', + 'Testing', + 'Testing', + 'Testing', + 1, + null + ), + array( + 'source', + '2 Testing', + '2 Testing', + 'Testing', + 2, + null + ), + array( + 'source', + '2 3 Testing comments', + '2 3 Testing comments', + 'Testing comments', + 2, + 3 + ), + array( + 'source', + '2 -1 Testing comments', + '2 -1 Testing comments', + '-1 Testing comments', + 2, + null + ), + array( + 'source', + '-1 1 Testing comments', + '-1 1 Testing comments', + '-1 1 Testing comments', + 1, + null + ), + ); + } +}