Skip to content

Commit 3b060b5

Browse files
committed
Refactored the source tag and written test
1 parent d29061e commit 3b060b5

File tree

4 files changed

+245
-85
lines changed

4 files changed

+245
-85
lines changed

src/DocBlock/Tags/Link.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ public function __construct($link, Description $description = null)
4747
public static function create($body, DescriptionFactory $descriptionFactory = null, Context $context = null)
4848
{
4949
Assert::string($body);
50+
Assert::notNull($descriptionFactory);
5051

5152
$parts = preg_split('/\s+/Su', $body, 2);
52-
53-
$link = $parts[0];
5453
$description = isset($parts[1]) ? $descriptionFactory->create($parts[1], $context) : null;
5554

56-
return new static($link, $description);
55+
return new static($parts[0], $description);
5756
}
5857

5958
/**

src/DocBlock/Tags/Param.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* Reflection class for the {@}param tag in a Docblock.
2424
*/
25-
class Param extends BaseTag
25+
final class Param extends BaseTag
2626
{
2727
/** @var string */
2828
protected $name = 'param';
@@ -31,10 +31,10 @@ class Param extends BaseTag
3131
private $type;
3232

3333
/** @var string */
34-
protected $variableName = '';
34+
private $variableName = '';
3535

3636
/** @var bool determines whether this is a variadic argument */
37-
protected $isVariadic = false;
37+
private $isVariadic = false;
3838

3939
/**
4040
* @param string $variableName

src/DocBlock/Tags/Source.php

Lines changed: 42 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,68 @@
11
<?php
22
/**
3-
* phpDocumentor
3+
* This file is part of phpDocumentor.
44
*
5-
* PHP Version 5.3
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
67
*
7-
* @author Vasil Rangelov <[email protected]>
8-
* @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
8+
* @copyright 2010-2015 Mike van Riel<[email protected]>
99
* @license http://www.opensource.org/licenses/mit-license.php MIT
1010
* @link http://phpdoc.org
1111
*/
1212

1313
namespace phpDocumentor\Reflection\DocBlock\Tags;
1414

15-
use phpDocumentor\Reflection\DocBlock\Tag;
15+
use phpDocumentor\Reflection\DocBlock\Description;
16+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
17+
use phpDocumentor\Reflection\Types\Context;
18+
use Webmozart\Assert\Assert;
1619

1720
/**
18-
* Reflection class for a @source tag in a Docblock.
21+
* Reflection class for a {@}source tag in a Docblock.
1922
*/
20-
class Source extends Tag
23+
final class Source extends BaseTag
2124
{
22-
/**
23-
* @var int The starting line, relative to the structural element's
24-
* location.
25-
*/
26-
protected $startingLine = 1;
25+
/** @var string */
26+
protected $name = 'source';
2727

28-
/**
29-
* @var int|null The number of lines, relative to the starting line. NULL
30-
* means "to the end".
31-
*/
32-
protected $lineCount = null;
28+
/** @var int The starting line, relative to the structural element's location. */
29+
private $startingLine = 1;
3330

34-
/**
35-
* {@inheritdoc}
36-
*/
37-
public function getContent()
31+
/** @var int|null The number of lines, relative to the starting line. NULL means "to the end". */
32+
private $lineCount = null;
33+
34+
public function __construct($startingLine, $lineCount = null, Description $description = null)
3835
{
39-
if (null === $this->description) {
40-
$this->description
41-
= "{$this->startingLine} {$this->lineCount} {$this->description}";
42-
}
36+
Assert::integerish($startingLine);
37+
Assert::nullOrIntegerish($lineCount);
4338

44-
return $this->description;
39+
$this->startingLine = (int)$startingLine;
40+
$this->lineCount = $lineCount !== null ? (int)$lineCount : null;
41+
$this->description = $description;
4542
}
4643

4744
/**
4845
* {@inheritdoc}
4946
*/
50-
public function setContent($content)
47+
public static function create($body, DescriptionFactory $descriptionFactory = null, Context $context = null)
5148
{
52-
parent::setContent($content);
53-
if (preg_match(
54-
'/^
55-
# Starting line
56-
([1-9]\d*)
57-
\s*
58-
# Number of lines
59-
(?:
60-
((?1))
61-
\s+
62-
)?
63-
# Description
64-
(.*)
65-
$/sux',
66-
$this->description,
67-
$matches
68-
)) {
69-
$this->startingLine = (int)$matches[1];
70-
if (isset($matches[2]) && '' !== $matches[2]) {
71-
$this->lineCount = (int)$matches[2];
49+
Assert::stringNotEmpty($body);
50+
Assert::notNull($descriptionFactory);
51+
52+
$startingLine = 1;
53+
$lineCount = null;
54+
$description = null;
55+
56+
// Starting line / Number of lines / Description
57+
if (preg_match('/^([1-9]\d*)\s*(?:((?1))\s+)?(.*)$/sux', $body, $matches)) {
58+
$startingLine = (int)$matches[1];
59+
if (isset($matches[2]) && $matches[2] !== '') {
60+
$lineCount = (int)$matches[2];
7261
}
73-
$this->setDescription($matches[3]);
74-
$this->description = $content;
62+
$description = $matches[3];
7563
}
7664

77-
return $this;
65+
return new static($startingLine, $lineCount, $descriptionFactory->create($description, $context));
7866
}
7967

8068
/**
@@ -88,22 +76,6 @@ public function getStartingLine()
8876
return $this->startingLine;
8977
}
9078

91-
/**
92-
* Sets the starting line.
93-
*
94-
* @param int $startingLine The new starting line, relative to the
95-
* structural element's location.
96-
*
97-
* @return $this
98-
*/
99-
public function setStartingLine($startingLine)
100-
{
101-
$this->startingLine = $startingLine;
102-
103-
$this->description = null;
104-
return $this;
105-
}
106-
10779
/**
10880
* Returns the number of lines.
10981
*
@@ -115,19 +87,10 @@ public function getLineCount()
11587
return $this->lineCount;
11688
}
11789

118-
/**
119-
* Sets the number of lines.
120-
*
121-
* @param int|null $lineCount The new number of lines, relative to the
122-
* starting line. NULL means "to the end".
123-
*
124-
* @return $this
125-
*/
126-
public function setLineCount($lineCount)
90+
public function __toString()
12791
{
128-
$this->lineCount = $lineCount;
129-
130-
$this->description = null;
131-
return $this;
92+
return $this->startingLine
93+
. ($this->lineCount !== null ? ' ' . $this->lineCount : '')
94+
. ($this->description ? ' ' . $this->description->render() : '');
13295
}
13396
}

0 commit comments

Comments
 (0)