Skip to content

Commit 4e6d7ec

Browse files
committed
add more unit tests and normalize the "__toString" methods v2
1 parent 7b7c22d commit 4e6d7ec

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

src/DocBlock/Tags/Covers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static function create(
4949
?FqsenResolver $resolver = null,
5050
?TypeContext $context = null
5151
) : self {
52-
Assert::notEmpty($body);
52+
Assert::stringNotEmpty($body);
5353
Assert::notNull($descriptionFactory);
5454
Assert::notNull($resolver);
5555

src/DocBlock/Tags/Example.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ final class Example implements Tag, Factory\StaticMethod
4545
/** @var string|null */
4646
private $content;
4747

48-
public function __construct(string $filePath, bool $isURI, int $startingLine, int $lineCount, ?string $content)
49-
{
50-
Assert::notEmpty($filePath);
48+
public function __construct(
49+
string $filePath,
50+
bool $isURI,
51+
int $startingLine,
52+
int $lineCount,
53+
?string $content
54+
) {
55+
Assert::stringNotEmpty($filePath);
5156
Assert::greaterThanEq($startingLine, 1);
5257
Assert::greaterThanEq($lineCount, 0);
5358

@@ -64,7 +69,7 @@ public function __construct(string $filePath, bool $isURI, int $startingLine, in
6469
public function getContent() : string
6570
{
6671
if ($this->content === null || $this->content === '') {
67-
$filePath = '"' . $this->filePath . '"';
72+
$filePath = $this->filePath;
6873
if ($this->isURI) {
6974
$filePath = $this->isUriRelative($this->filePath)
7075
? str_replace('%2F', '/', rawurlencode($this->filePath))
@@ -85,7 +90,7 @@ public function getDescription() : ?string
8590
public static function create(string $body) : ?Tag
8691
{
8792
// File component: File path in quotes or File URI / Source information
88-
if (!preg_match('/^(?:\"([^\"]+)\"|(\S+))(?:\s+(.*))?$/sux', $body, $matches)) {
93+
if (!preg_match('/^\s*(?:(\"[^\"]+\")|(\S+))(?:\s+(.*))?$/sux', $body, $matches)) {
8994
return null;
9095
}
9196

@@ -134,7 +139,7 @@ public static function create(string $body) : ?Tag
134139
*/
135140
public function getFilePath() : string
136141
{
137-
return $this->filePath;
142+
return trim($this->filePath, '"');
138143
}
139144

140145
/**
@@ -143,9 +148,15 @@ public function getFilePath() : string
143148
public function __toString() : string
144149
{
145150
$filePath = (string) $this->filePath;
151+
$isDefaultLine = $this->startingLine === 1 && $this->lineCount === 0;
152+
$startingLine = !$isDefaultLine ? (string) $this->startingLine : '';
153+
$lineCount = !$isDefaultLine ? (string) $this->lineCount : '';
146154
$content = (string) $this->content;
147155

148-
return $filePath . ($content !== '' ? ($filePath !== '' ? ' ' : '') . $content : '');
156+
return $filePath
157+
. ($startingLine !== '' ? ($filePath !== '' ? ' ' : '') . $startingLine : '')
158+
. ($lineCount !== '' ? ($filePath !== '' || $startingLine !== '' ? ' ' : '') . $lineCount : '')
159+
. ($content !== '' ? ($filePath !== '' || $startingLine !== '' || $lineCount !== '' ? ' ' : '') . $content : '');
149160
}
150161

151162
/**

tests/unit/DocBlock/Tags/ExampleTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,52 @@ public function testLengthIsParsed() : void
9393
$this->assertEquals(5, $tag->getLineCount());
9494
}
9595

96+
/**
97+
* @covers ::create
98+
* @covers ::__toString
99+
*/
100+
public function testStringRepresentationIsReturned() : void
101+
{
102+
$tag = Example::create('"example1.php" 10 5 test text');
103+
104+
$this->assertSame('"example1.php" 10 5 test text', (string) $tag);
105+
106+
// ---
107+
108+
$tag = Example::create('file://example1.php');
109+
110+
$this->assertSame('file://example1.php', (string) $tag);
111+
112+
// ---
113+
114+
$tag = Example::create('0 foo bar');
115+
116+
$this->assertSame('0 foo bar', (string) $tag);
117+
118+
// ---
119+
120+
$tag = Example::create('$redisCluster->pttl(\'key\');');
121+
122+
$this->assertSame('$redisCluster->pttl(\'key\');', (string) $tag);
123+
124+
// ---
125+
126+
$tag = Example::create(' "example1.php" 10 5 test text ');
127+
128+
$this->assertSame('"example1.php" 10 5 test text', (string) $tag);
129+
}
130+
131+
/**
132+
* @covers ::create
133+
* @covers ::__toString
134+
*/
135+
public function testStringRepresentationIsReturnedWithoutDescription() : void
136+
{
137+
$tag = Example::create('');
138+
139+
$this->assertSame('', (string) $tag);
140+
}
141+
96142
/**
97143
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
98144
*

0 commit comments

Comments
 (0)