Skip to content

Commit 97863e0

Browse files
committed
add more unit tests and normalize the "__toString" methods
1 parent bbe0f54 commit 97863e0

34 files changed

+548
-44
lines changed

src/DocBlock/Tags/Author.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ public function getEmail() : string
7171
*/
7272
public function __toString() : string
7373
{
74-
return $this->authorName . ($this->authorEmail !== '' ? ' <' . $this->authorEmail . '>' : '');
74+
if ($this->authorEmail) {
75+
$authorEmail = '<' . $this->authorEmail . '>';
76+
} else {
77+
$authorEmail = '';
78+
}
79+
80+
$authorName = (string) $this->authorName;
81+
82+
return $authorName . ($authorEmail !== '' ? ($authorName !== '' ? ' ' : '') . $authorEmail : '');
7583
}
7684

7785
/**

src/DocBlock/Tags/Covers.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public function getReference() : Fqsen
7272
*/
7373
public function __toString() : string
7474
{
75-
return $this->refers . ($this->description ? ' ' . $this->description->render() : '');
75+
if ($this->description) {
76+
$description = $this->description->render();
77+
} else {
78+
$description = '';
79+
}
80+
81+
$refers = (string) $this->refers;
82+
83+
return $refers . ($description !== '' ? ($refers !== '' ? ' ' : '') . $description : '');
7684
}
7785
}

src/DocBlock/Tags/Deprecated.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ public function getVersion() : ?string
9595
*/
9696
public function __toString() : string
9797
{
98-
return ($this->version ?? '') . ($this->description ? ' ' . $this->description->render() : '');
98+
if ($this->description) {
99+
$description = $this->description->render();
100+
} else {
101+
$description = '';
102+
}
103+
104+
$version = (string) $this->version;
105+
106+
return $version . ($description !== '' ? ($version !== '' ? ' ' : '') . $description : '');
99107
}
100108
}

src/DocBlock/Tags/Example.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ public function getFilePath() : string
142142
*/
143143
public function __toString() : string
144144
{
145-
return $this->filePath . ($this->content ? ' ' . $this->content : '');
145+
$filePath = (string) $this->filePath;
146+
$content = (string) $this->content;
147+
148+
return $filePath . ($content !== '' ? ($filePath !== '' ? ' ' : '') . $content : '');
146149
}
147150

148151
/**

src/DocBlock/Tags/Generic.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ public static function create(
6464
*/
6565
public function __toString() : string
6666
{
67-
return $this->description ? $this->description->render() : '';
67+
if ($this->description) {
68+
$description = $this->description->render();
69+
} else {
70+
$description = '';
71+
}
72+
73+
return $description;
6874
}
6975

7076
/**

src/DocBlock/Tags/Link.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ public function getLink() : string
6565
*/
6666
public function __toString() : string
6767
{
68-
return $this->link . ($this->description ? ' ' . $this->description->render() : '');
68+
if ($this->description) {
69+
$description = $this->description->render();
70+
} else {
71+
$description = '';
72+
}
73+
74+
$link = (string) $this->link;
75+
76+
return $link . ($description !== '' ? ($link !== '' ? ' ' : '') . $description : '');
6977
}
7078
}

src/DocBlock/Tags/Method.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,25 @@ public function __toString() : string
212212
foreach ($this->arguments as $argument) {
213213
$arguments[] = $argument['type'] . ' $' . $argument['name'];
214214
}
215+
$argumentStr = '(' . implode(', ', $arguments) . ')';
215216

216-
return trim(($this->isStatic() ? 'static ' : '')
217-
. (string) $this->returnType . ' '
218-
. $this->methodName
219-
. '(' . implode(', ', $arguments) . ')'
220-
. ($this->description ? ' ' . $this->description->render() : ''));
217+
if ($this->description) {
218+
$description = $this->description->render();
219+
} else {
220+
$description = '';
221+
}
222+
223+
$static = $this->isStatic ? 'static' : '';
224+
225+
$returnType = (string) $this->returnType;
226+
227+
$methodName = (string) $this->methodName;
228+
229+
return $static
230+
. ($returnType !== '' ? ($static !== '' ? ' ' : '') . $returnType : '')
231+
. ($methodName !== '' ? ($static !== '' || $returnType !== '' ? ' ' : '') . $methodName : '')
232+
. $argumentStr
233+
. ($description !== '' ? ' ' . $description : '');
221234
}
222235

223236
/**

src/DocBlock/Tags/Param.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,23 @@ public function isReference() : bool
140140
*/
141141
public function __toString() : string
142142
{
143-
return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '')
144-
. ($this->isReference() ? '&' : '')
145-
. ($this->isVariadic() ? '...' : '')
146-
. ($this->variableName ? '$' . $this->variableName : '')
147-
. (('' . $this->description) ? ' ' . $this->description : '');
143+
if ($this->description) {
144+
$description = $this->description->render();
145+
} else {
146+
$description = '';
147+
}
148+
149+
$variableName = '';
150+
if ($this->variableName) {
151+
$variableName .= ($this->isReference ? '&' : '') . ($this->isVariadic ? '...' : '');
152+
$variableName .= ($this->variableName ? '$' . $this->variableName : '');
153+
}
154+
155+
$type = (string) $this->type;
156+
157+
return $type
158+
. ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '')
159+
. ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : '');
148160
}
149161

150162
private static function strStartsWithVariable(string $str) : bool

src/DocBlock/Tags/Property.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,22 @@ public function getVariableName() : ?string
9898
*/
9999
public function __toString() : string
100100
{
101-
return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '')
102-
. ($this->variableName ? '$' . $this->variableName : '')
103-
. (('' . $this->description) ? ' ' . $this->description : '');
101+
if ($this->description) {
102+
$description = $this->description->render();
103+
} else {
104+
$description = '';
105+
}
106+
107+
if ($this->variableName) {
108+
$variableName = ($this->variableName ? '$' . $this->variableName : '');
109+
} else {
110+
$variableName = '';
111+
}
112+
113+
$type = (string) $this->type;
114+
115+
return $type
116+
. ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '')
117+
. ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : '');
104118
}
105119
}

src/DocBlock/Tags/PropertyRead.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,22 @@ public function getVariableName() : ?string
9898
*/
9999
public function __toString() : string
100100
{
101-
return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '')
102-
. ($this->variableName ? '$' . $this->variableName : '')
103-
. (('' . $this->description) ? ' ' . $this->description : '');
101+
if ($this->description) {
102+
$description = $this->description->render();
103+
} else {
104+
$description = '';
105+
}
106+
107+
if ($this->variableName) {
108+
$variableName = ($this->variableName ? '$' . $this->variableName : '');
109+
} else {
110+
$variableName = '';
111+
}
112+
113+
$type = (string) $this->type;
114+
115+
return $type
116+
. ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '')
117+
. ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : '');
104118
}
105119
}

src/DocBlock/Tags/PropertyWrite.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,22 @@ public function getVariableName() : ?string
9898
*/
9999
public function __toString() : string
100100
{
101-
return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '')
102-
. ($this->variableName ? '$' . $this->variableName : '')
103-
. (('' . $this->description) ? ' ' . $this->description : '');
101+
if ($this->description) {
102+
$description = $this->description->render();
103+
} else {
104+
$description = '';
105+
}
106+
107+
if ($this->variableName) {
108+
$variableName = ($this->variableName ? '$' . $this->variableName : '');
109+
} else {
110+
$variableName = '';
111+
}
112+
113+
$type = (string) $this->type;
114+
115+
return $type
116+
. ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '')
117+
. ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : '');
104118
}
105119
}

src/DocBlock/Tags/Return_.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public static function create(
5151

5252
public function __toString() : string
5353
{
54-
return ($this->type ?: 'mixed') . ' ' . (string) $this->description;
54+
if ($this->description) {
55+
$description = $this->description->render();
56+
} else {
57+
$description = '';
58+
}
59+
60+
$type = $this->type ? '' . $this->type : 'mixed';
61+
62+
return $type . ($description !== '' ? ($type !== '' ? ' ' : '') . $description : '');
5563
}
5664
}

src/DocBlock/Tags/See.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ public function getReference() : Reference
7777
*/
7878
public function __toString() : string
7979
{
80-
return $this->refers . ($this->description ? ' ' . $this->description->render() : '');
80+
if ($this->description) {
81+
$description = $this->description->render();
82+
} else {
83+
$description = '';
84+
}
85+
86+
$refers = (string) $this->refers;
87+
88+
return $refers . ($description !== '' ? ($refers !== '' ? ' ' : '') . $description : '');
8189
}
8290
}

src/DocBlock/Tags/Since.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ public function getVersion() : ?string
8989
*/
9090
public function __toString() : string
9191
{
92-
return (string) $this->version . ($this->description ? ' ' . (string) $this->description : '');
92+
if ($this->description) {
93+
$description = $this->description->render();
94+
} else {
95+
$description = '';
96+
}
97+
98+
$version = (string) $this->version;
99+
100+
return $version . ($description !== '' ? ($version !== '' ? ' ' : '') . $description : '');
93101
}
94102
}

src/DocBlock/Tags/Source.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,18 @@ public function getLineCount() : ?int
9696

9797
public function __toString() : string
9898
{
99-
return $this->startingLine
100-
. ($this->lineCount !== null ? ' ' . $this->lineCount : '')
101-
. ($this->description ? ' ' . (string) $this->description : '');
99+
if ($this->description) {
100+
$description = $this->description->render();
101+
} else {
102+
$description = '';
103+
}
104+
105+
$startingLine = (string) $this->startingLine;
106+
107+
$lineCount = $this->lineCount !== null ? '' . $this->lineCount : '';
108+
109+
return $startingLine
110+
. ($lineCount !== '' ? ($startingLine !== '' ? ' ' : '') . $lineCount : '')
111+
. ($description !== '' ? ($startingLine !== '' || $lineCount !== '' ? ' ' : '') . $description : '');
102112
}
103113
}

src/DocBlock/Tags/Throws.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public static function create(
5151

5252
public function __toString() : string
5353
{
54-
return (string) $this->type . ' ' . (string) $this->description;
54+
if ($this->description) {
55+
$description = $this->description->render();
56+
} else {
57+
$description = '';
58+
}
59+
60+
$type = (string) $this->type;
61+
62+
return $type . ($description !== '' ? ($type !== '' ? ' ' : '') . $description : '');
5563
}
5664
}

src/DocBlock/Tags/Uses.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public function getReference() : Fqsen
7171
*/
7272
public function __toString() : string
7373
{
74-
return $this->refers . ' ' . (string) $this->description;
74+
if ($this->description) {
75+
$description = $this->description->render();
76+
} else {
77+
$description = '';
78+
}
79+
80+
$refers = (string) $this->refers;
81+
82+
return $refers . ($description !== '' ? ($refers !== '' ? ' ' : '') . $description : '');
7583
}
7684
}

src/DocBlock/Tags/Var_.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,22 @@ public function getVariableName() : ?string
9999
*/
100100
public function __toString() : string
101101
{
102-
return ($this->type ? $this->type . ($this->variableName ? ' ' : '') : '')
103-
. ($this->variableName ? '$' . $this->variableName : '')
104-
. (('' . $this->description) ? ' ' . $this->description : '');
102+
if ($this->description) {
103+
$description = $this->description->render();
104+
} else {
105+
$description = '';
106+
}
107+
108+
if ($this->variableName) {
109+
$variableName = ($this->variableName ? '$' . $this->variableName : '');
110+
} else {
111+
$variableName = '';
112+
}
113+
114+
$type = (string) $this->type;
115+
116+
return $type
117+
. ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '')
118+
. ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : '');
105119
}
106120
}

src/DocBlock/Tags/Version.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ public function getVersion() : ?string
9292
*/
9393
public function __toString() : string
9494
{
95-
return ((string) $this->version) .
96-
($this->description instanceof Description ? ' ' . $this->description->render() : '');
95+
if ($this->description) {
96+
$description = $this->description->render();
97+
} else {
98+
$description = '';
99+
}
100+
101+
$version = (string) $this->version;
102+
103+
return $version . ($description !== '' ? ($version !== '' ? ' ' : '') . $description : '');
97104
}
98105
}

tests/unit/DocBlock/Tags/AuthorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,23 @@ public function testStringRepresentationIsReturned() : void
116116
$fixture = new Author('Mike van Riel', 'mike@phpdoc.org');
117117

118118
$this->assertSame('Mike van Riel <mike@phpdoc.org>', (string) $fixture);
119+
120+
// ---
121+
122+
$fixture = new Author('0', 'zero@foo.bar');
123+
124+
$this->assertSame('0 <zero@foo.bar>', (string) $fixture);
125+
}
126+
127+
/**
128+
* @covers ::__construct
129+
* @covers ::__toString
130+
*/
131+
public function testStringRepresentationIsReturnedWithoutName() : void
132+
{
133+
$fixture = new Author('', 'mike@phpdoc.org');
134+
135+
$this->assertSame('<mike@phpdoc.org>', (string) $fixture);
119136
}
120137

121138
/**

0 commit comments

Comments
 (0)