Skip to content

Commit 069a785

Browse files
authored
Merge pull request #254 from voku/fix_for_phpstorm_stubs
do not resolve types if it's not possible
2 parents f607592 + 2ef4c3d commit 069a785

39 files changed

+1048
-68
lines changed

.github/workflows/push.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4747

4848
- name: Quick check code coverage level
49-
run: php tests/coverage-checker.php 89
49+
run: php tests/coverage-checker.php 91
5050

5151
phpunit:
5252
name: Unit tests for PHP version ${{ matrix.php-versions }} on ${{ matrix.operating-system }}

src/DocBlock/Tags/Author.php

+9-1
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

+10-2
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

@@ -87,6 +87,14 @@ public function getReference() : Fqsen
8787
*/
8888
public function __toString() : string
8989
{
90-
return $this->refers . ($this->description ? ' ' . $this->description->render() : '');
90+
if ($this->description) {
91+
$description = $this->description->render();
92+
} else {
93+
$description = '';
94+
}
95+
96+
$refers = (string) $this->refers;
97+
98+
return $refers . ($description !== '' ? ($refers !== '' ? ' ' : '') . $description : '');
9199
}
92100
}

src/DocBlock/Tags/Deprecated.php

+9-1
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

+27-7
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,15 +139,30 @@ 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
/**
141146
* Returns a string representation for this tag.
142147
*/
143148
public function __toString() : string
144149
{
145-
return $this->filePath . ($this->content ? ' ' . $this->content : '');
150+
$filePath = (string) $this->filePath;
151+
$isDefaultLine = $this->startingLine === 1 && $this->lineCount === 0;
152+
$startingLine = !$isDefaultLine ? (string) $this->startingLine : '';
153+
$lineCount = !$isDefaultLine ? (string) $this->lineCount : '';
154+
$content = (string) $this->content;
155+
156+
return $filePath
157+
. ($startingLine !== ''
158+
? ($filePath !== '' ? ' ' : '') . $startingLine
159+
: '')
160+
. ($lineCount !== ''
161+
? ($filePath !== '' || $startingLine !== '' ? ' ' : '') . $lineCount
162+
: '')
163+
. ($content !== ''
164+
? ($filePath !== '' || $startingLine !== '' || $lineCount !== '' ? ' ' : '') . $content
165+
: '');
146166
}
147167

148168
/**

src/DocBlock/Tags/Generic.php

+7-1
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

+9-1
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

+19-5
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,25 @@ public function __toString() : string
213213
$arguments[] = $argument['type'] . ' $' . $argument['name'];
214214
}
215215

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

223237
/**

src/DocBlock/Tags/Param.php

+33-19
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,19 @@ public static function create(
7575
$isReference = false;
7676

7777
// if the first item that is encountered is not a variable; it is a type
78-
if ($firstPart && $firstPart[0] !== '$') {
78+
if ($firstPart && !self::strStartsWithVariable($firstPart)) {
7979
$type = $typeResolver->resolve($firstPart, $context);
8080
} else {
8181
// first part is not a type; we should prepend it to the parts array for further processing
8282
array_unshift($parts, $firstPart);
8383
}
8484

8585
// if the next item starts with a $ or ...$ or &$ or &...$ it must be the variable name
86-
if (isset($parts[0])
87-
&&
88-
(
89-
strpos($parts[0], '$') === 0
90-
||
91-
strpos($parts[0], '...$') === 0
92-
||
93-
strpos($parts[0], '&$') === 0
94-
||
95-
strpos($parts[0], '&...$') === 0
96-
)
97-
) {
86+
if (isset($parts[0]) && self::strStartsWithVariable($parts[0])) {
9887
$variableName = array_shift($parts);
99-
array_shift($parts);
88+
if ($type) {
89+
array_shift($parts);
90+
}
10091

10192
Assert::notNull($variableName);
10293

@@ -149,10 +140,33 @@ public function isReference() : bool
149140
*/
150141
public function __toString() : string
151142
{
152-
return ($this->type ? $this->type . ' ' : '')
153-
. ($this->isReference() ? '&' : '')
154-
. ($this->isVariadic() ? '...' : '')
155-
. ($this->variableName !== null ? '$' . $this->variableName : '')
156-
. ($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;
153+
}
154+
155+
$type = (string) $this->type;
156+
157+
return $type
158+
. ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '')
159+
. ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : '');
160+
}
161+
162+
private static function strStartsWithVariable(string $str) : bool
163+
{
164+
return strpos($str, '$') === 0
165+
||
166+
strpos($str, '...$') === 0
167+
||
168+
strpos($str, '&$') === 0
169+
||
170+
strpos($str, '&...$') === 0;
157171
}
158172
}

src/DocBlock/Tags/Property.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ public static function create(
6868
array_unshift($parts, $firstPart);
6969
}
7070

71-
// if the next item starts with a $ or ...$ it must be the variable name
71+
// if the next item starts with a $ it must be the variable name
7272
if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
7373
$variableName = array_shift($parts);
74-
array_shift($parts);
74+
if ($type) {
75+
array_shift($parts);
76+
}
7577

7678
Assert::notNull($variableName);
7779

@@ -96,8 +98,22 @@ public function getVariableName() : ?string
9698
*/
9799
public function __toString() : string
98100
{
99-
return ($this->type ? $this->type . ' ' : '')
100-
. ($this->variableName ? '$' . $this->variableName : '')
101-
. ($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;
109+
} else {
110+
$variableName = '';
111+
}
112+
113+
$type = (string) $this->type;
114+
115+
return $type
116+
. ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '')
117+
. ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : '');
102118
}
103119
}

src/DocBlock/Tags/PropertyRead.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ public static function create(
6868
array_unshift($parts, $firstPart);
6969
}
7070

71-
// if the next item starts with a $ or ...$ it must be the variable name
71+
// if the next item starts with a $ it must be the variable name
7272
if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
7373
$variableName = array_shift($parts);
74-
array_shift($parts);
74+
if ($type) {
75+
array_shift($parts);
76+
}
7577

7678
Assert::notNull($variableName);
7779

@@ -96,8 +98,22 @@ public function getVariableName() : ?string
9698
*/
9799
public function __toString() : string
98100
{
99-
return ($this->type ? $this->type . ' ' : '')
100-
. ($this->variableName ? '$' . $this->variableName : '')
101-
. ($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;
109+
} else {
110+
$variableName = '';
111+
}
112+
113+
$type = (string) $this->type;
114+
115+
return $type
116+
. ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '')
117+
. ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : '');
102118
}
103119
}

src/DocBlock/Tags/PropertyWrite.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ public static function create(
6868
array_unshift($parts, $firstPart);
6969
}
7070

71-
// if the next item starts with a $ or ...$ it must be the variable name
71+
// if the next item starts with a $ it must be the variable name
7272
if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
7373
$variableName = array_shift($parts);
74-
array_shift($parts);
74+
if ($type) {
75+
array_shift($parts);
76+
}
7577

7678
Assert::notNull($variableName);
7779

@@ -96,8 +98,22 @@ public function getVariableName() : ?string
9698
*/
9799
public function __toString() : string
98100
{
99-
return ($this->type ? $this->type . ' ' : '')
100-
. ($this->variableName ? '$' . $this->variableName : '')
101-
. ($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;
109+
} else {
110+
$variableName = '';
111+
}
112+
113+
$type = (string) $this->type;
114+
115+
return $type
116+
. ($variableName !== '' ? ($type !== '' ? ' ' : '') . $variableName : '')
117+
. ($description !== '' ? ($type !== '' || $variableName !== '' ? ' ' : '') . $description : '');
102118
}
103119
}

0 commit comments

Comments
 (0)