Refactored the source tag and written test

This commit is contained in:
Mike van Riel
2015-06-22 08:12:13 +02:00
committed by Mike van Riel
parent d29061ea4d
commit 3b060b555e
4 changed files with 245 additions and 85 deletions
+2 -3
View File
@@ -47,13 +47,12 @@ final class Link extends BaseTag
public static function create($body, DescriptionFactory $descriptionFactory = null, Context $context = null)
{
Assert::string($body);
Assert::notNull($descriptionFactory);
$parts = preg_split('/\s+/Su', $body, 2);
$link = $parts[0];
$description = isset($parts[1]) ? $descriptionFactory->create($parts[1], $context) : null;
return new static($link, $description);
return new static($parts[0], $description);
}
/**
+3 -3
View File
@@ -22,7 +22,7 @@ use Webmozart\Assert\Assert;
/**
* Reflection class for the {@}param tag in a Docblock.
*/
class Param extends BaseTag
final class Param extends BaseTag
{
/** @var string */
protected $name = 'param';
@@ -31,10 +31,10 @@ class Param extends BaseTag
private $type;
/** @var string */
protected $variableName = '';
private $variableName = '';
/** @var bool determines whether this is a variadic argument */
protected $isVariadic = false;
private $isVariadic = false;
/**
* @param string $variableName
+42 -79
View File
@@ -1,80 +1,68 @@
<?php
/**
* phpDocumentor
* This file is part of phpDocumentor.
*
* PHP Version 5.3
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Vasil Rangelov <[email protected]>
* @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
* @copyright 2010-2015 Mike van Riel<[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\Types\Context;
use Webmozart\Assert\Assert;
/**
* Reflection class for a @source tag in a Docblock.
* Reflection class for a {@}source tag in a Docblock.
*/
class Source extends Tag
final class Source extends BaseTag
{
/**
* @var int The starting line, relative to the structural element's
* location.
*/
protected $startingLine = 1;
/** @var string */
protected $name = 'source';
/**
* @var int|null The number of lines, relative to the starting line. NULL
* means "to the end".
*/
protected $lineCount = null;
/** @var int The starting line, relative to the structural element's location. */
private $startingLine = 1;
/**
* {@inheritdoc}
*/
public function getContent()
/** @var int|null The number of lines, relative to the starting line. NULL means "to the end". */
private $lineCount = null;
public function __construct($startingLine, $lineCount = null, Description $description = null)
{
if (null === $this->description) {
$this->description
= "{$this->startingLine} {$this->lineCount} {$this->description}";
}
Assert::integerish($startingLine);
Assert::nullOrIntegerish($lineCount);
return $this->description;
$this->startingLine = (int)$startingLine;
$this->lineCount = $lineCount !== null ? (int)$lineCount : null;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public function setContent($content)
public static function create($body, DescriptionFactory $descriptionFactory = null, Context $context = null)
{
parent::setContent($content);
if (preg_match(
'/^
# Starting line
([1-9]\d*)
\s*
# Number of lines
(?:
((?1))
\s+
)?
# Description
(.*)
$/sux',
$this->description,
$matches
)) {
$this->startingLine = (int)$matches[1];
if (isset($matches[2]) && '' !== $matches[2]) {
$this->lineCount = (int)$matches[2];
Assert::stringNotEmpty($body);
Assert::notNull($descriptionFactory);
$startingLine = 1;
$lineCount = null;
$description = null;
// Starting line / Number of lines / Description
if (preg_match('/^([1-9]\d*)\s*(?:((?1))\s+)?(.*)$/sux', $body, $matches)) {
$startingLine = (int)$matches[1];
if (isset($matches[2]) && $matches[2] !== '') {
$lineCount = (int)$matches[2];
}
$this->setDescription($matches[3]);
$this->description = $content;
$description = $matches[3];
}
return $this;
return new static($startingLine, $lineCount, $descriptionFactory->create($description, $context));
}
/**
@@ -88,22 +76,6 @@ class Source extends Tag
return $this->startingLine;
}
/**
* Sets the starting line.
*
* @param int $startingLine The new starting line, relative to the
* structural element's location.
*
* @return $this
*/
public function setStartingLine($startingLine)
{
$this->startingLine = $startingLine;
$this->description = null;
return $this;
}
/**
* Returns the number of lines.
*
@@ -115,19 +87,10 @@ class Source extends Tag
return $this->lineCount;
}
/**
* Sets the number of lines.
*
* @param int|null $lineCount The new number of lines, relative to the
* starting line. NULL means "to the end".
*
* @return $this
*/
public function setLineCount($lineCount)
public function __toString()
{
$this->lineCount = $lineCount;
$this->description = null;
return $this;
return $this->startingLine
. ($this->lineCount !== null ? ' ' . $this->lineCount : '')
. ($this->description ? ' ' . $this->description->render() : '');
}
}