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
}

0 commit comments

Comments
 (0)