Skip to content

Commit db219c8

Browse files
committed
[+]: use "phpstan/phpdoc-parser" for properties
1 parent 53074e8 commit db219c8

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

src/voku/SimplePhpParser/Model/PHPParameter.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,11 @@ private function readPhpDoc($doc, string $parameterName): void
253253

254254
$parsedParamTagParam = (string) $parsedParamTag;
255255
$spitedData = Utils::splitTypeAndVariable($parsedParamTag);
256-
$parsedParamTagStr = $spitedData['parsedParamTagStr'];
257256
$variableName = $spitedData['variableName'];
258257

259258
// check only the current "param"-tag
260259
if ($variableName && \strtoupper($parameterName) === \strtoupper($variableName)) {
261-
$this->phpDocRaw = (string) $parsedParamTag;
260+
$this->phpDocRaw = $parsedParamTagParam;
262261
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagParam);
263262
}
264263

@@ -285,15 +284,10 @@ private function readPhpDoc($doc, string $parameterName): void
285284
}
286285

287286
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagStr);
288-
289-
break;
290287
}
291288
}
292289

293-
// fallback for e.g. `callable()`
294-
if (!$this->phpDocRaw || !$this->typeFromPhpDocExtended) {
295-
$this->readPhpDocByTokens($docComment, $parameterName);
296-
}
290+
$this->readPhpDocByTokens($docComment, $parameterName);
297291

298292
} catch (\Exception $e) {
299293
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);

src/voku/SimplePhpParser/Model/PHPProperty.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,14 @@ private function readPhpDoc($doc): void
235235

236236
if (!empty($parsedParamTags)) {
237237
foreach ($parsedParamTags as $parsedParamTag) {
238+
$parsedParamTagParam = (string) $parsedParamTag;
239+
238240
if ($parsedParamTag instanceof \phpDocumentor\Reflection\DocBlock\Tags\Var_) {
239241
$type = $parsedParamTag->getType();
240242

241243
$this->typeFromPhpDoc = Utils::normalizePhpType($type . '');
242244

243-
$typeFromPhpDocMaybeWithCommentTmp = \trim((string) $parsedParamTag);
245+
$typeFromPhpDocMaybeWithCommentTmp = \trim($parsedParamTagParam);
244246
if (
245247
$typeFromPhpDocMaybeWithCommentTmp
246248
&&
@@ -255,8 +257,8 @@ private function readPhpDoc($doc): void
255257
}
256258
}
257259

258-
$this->phpDocRaw = (string) $parsedParamTag;
259-
$this->typeFromPhpDocExtended = Utils::modernPhpdoc((string) $parsedParamTag);
260+
$this->phpDocRaw = $parsedParamTagParam;
261+
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagParam);
260262
}
261263
}
262264

@@ -275,9 +277,42 @@ private function readPhpDoc($doc): void
275277
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagStr);
276278
}
277279
}
280+
281+
$this->readPhpDocByTokens($docComment);
282+
278283
} catch (\Exception $e) {
279284
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
280285
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
281286
}
282287
}
288+
289+
/**
290+
* @throws \PHPStan\PhpDocParser\Parser\ParserException
291+
*/
292+
private function readPhpDocByTokens(string $docComment): void
293+
{
294+
$tokens = Utils::modernPhpdocTokens($docComment);
295+
296+
$paramContent = null;
297+
foreach ($tokens->getTokens() as $token) {
298+
$content = $token[0];
299+
300+
if ($content === '@var' || $content === '@psalm-var' || $content === '@phpstan-var') {
301+
// reset
302+
$paramContent = '';
303+
304+
continue;
305+
}
306+
307+
if ($paramContent !== null) {
308+
$paramContent .= $content;
309+
}
310+
}
311+
312+
$paramContent = $paramContent ? \trim($paramContent) : null;
313+
if ($paramContent) {
314+
$this->phpDocRaw = $paramContent;
315+
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($paramContent);
316+
}
317+
}
283318
}

tests/Dummy13.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010
readonly final class Dummy13 implements \voku\tests\DummyInterface
1111
{
12+
/**
13+
* @var callable(int): string
14+
*/
1215
public int $lall;
1316

1417
public function __construct(int $lall)

tests/ParserTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ public function testReadonlyClass(): void
142142
$isReadonly = $phpClasses[Dummy13::class]->is_readonly;
143143
static::assertSame(true, $isReadonly);
144144

145+
static::assertSame('callable(int): string', $phpClasses[Dummy13::class]->properties['lall']->typeFromPhpDocExtended);
146+
145147
$isReadonly = $phpClasses[Dummy13::class]->properties['lall']->is_readonly;
146148
static::assertSame(true, $isReadonly);
147149
} else {

0 commit comments

Comments
 (0)