Skip to content

Commit 555730f

Browse files
committed
[~]: use "phpstan/phpdoc-parser" as fallback for parameters v2
-> simplified the phpdoc reading process :)
1 parent 1baa2ca commit 555730f

File tree

4 files changed

+62
-66
lines changed

4 files changed

+62
-66
lines changed

src/voku/SimplePhpParser/Model/PHPParameter.php

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ private function readPhpDoc($doc, string $parameterName): void
290290
$this->phpDocRaw = (string) $parsedParamTag;
291291
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagParam);
292292
}
293+
294+
break;
293295
}
294296
}
295297

@@ -298,62 +300,68 @@ private function readPhpDoc($doc, string $parameterName): void
298300

299301
if (!empty($parsedParamTags)) {
300302
foreach ($parsedParamTags as $parsedParamTag) {
301-
if ($parsedParamTag instanceof \phpDocumentor\Reflection\DocBlock\Tags\Generic) {
302-
$spitedData = Utils::splitTypeAndVariable($parsedParamTag);
303-
$parsedParamTagStr = $spitedData['parsedParamTagStr'];
304-
$variableName = $spitedData['variableName'];
303+
if (!$parsedParamTag instanceof \phpDocumentor\Reflection\DocBlock\Tags\Generic) {
304+
continue;
305+
}
305306

306-
// check only the current "param"-tag
307-
if (!$variableName || \strtoupper($parameterName) !== \strtoupper($variableName)) {
308-
continue;
309-
}
307+
$spitedData = Utils::splitTypeAndVariable($parsedParamTag);
308+
$parsedParamTagStr = $spitedData['parsedParamTagStr'];
309+
$variableName = $spitedData['variableName'];
310310

311-
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagStr);
311+
// check only the current "param"-tag
312+
if (!$variableName || \strtoupper($parameterName) !== \strtoupper($variableName)) {
313+
continue;
312314
}
315+
316+
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagStr);
317+
318+
break;
313319
}
314320
}
315321

316-
if (
317-
$doc instanceof Doc
318-
&&
319-
!$this->phpDocRaw
320-
) {
321-
$tokens = Utils::modernPhpdocTokens($doc->getText());
322+
// fallback for e.g. `callable()`
323+
if (!$this->phpDocRaw || !$this->typeFromPhpDocExtended) {
324+
$this->readPhpDocByTokens($docComment, $parameterName);
325+
}
322326

323-
$paramTagFound = null;
324-
$paramContent = '';
325-
foreach ($tokens->getTokens() as $token) {
326-
$content = $token[0] ?? '';
327+
} catch (\Exception $e) {
328+
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
329+
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
330+
}
331+
}
327332

328-
if ($content === '@param' || $content === '@psalm-param' || $content === '@phpstan-param') {
329-
$paramContent = '';
330-
$paramTagFound = true;
333+
/**
334+
* @throws \PHPStan\PhpDocParser\Parser\ParserException
335+
*/
336+
private function readPhpDocByTokens(string $docComment, string $parameterName): void
337+
{
338+
$tokens = Utils::modernPhpdocTokens($docComment);
331339

332-
continue;
333-
}
340+
$paramContent = null;
341+
foreach ($tokens->getTokens() as $token) {
342+
$content = $token[0] ?? '';
334343

335-
if ($content === '$' . $parameterName) {
336-
break;
337-
}
344+
if ($content === '@param' || $content === '@psalm-param' || $content === '@phpstan-param') {
345+
// reset
346+
$paramContent = '';
338347

339-
if (\strpos($content, '$') === 0) {
340-
$paramContent = '';
341-
$paramTagFound = false;
342-
}
348+
continue;
349+
}
343350

344-
if ($paramTagFound) {
345-
$paramContent .= $content;
346-
}
347-
}
351+
// We can stop if we found the param variable e.g. `@param array{foo:int} $param`.
352+
if ($content === '$' . $parameterName) {
353+
break;
354+
}
348355

349-
$paramContent = \trim($paramContent);
350-
if ($paramContent) {
351-
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($paramContent);
352-
}
356+
if ($paramContent !== null) {
357+
$paramContent .= $content;
353358
}
354-
} catch (\Exception $e) {
355-
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
356-
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
359+
}
360+
361+
$paramContent = $paramContent ? \trim($paramContent) : null;
362+
if ($paramContent) {
363+
$this->phpDocRaw = $paramContent . ' ' . '$' . $parameterName;
364+
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($paramContent);
357365
}
358366
}
359367
}

src/voku/SimplePhpParser/Model/PHPProperty.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,20 +283,18 @@ private function readPhpDoc($doc): void
283283

284284
if (!empty($parsedParamTags)) {
285285
foreach ($parsedParamTags as $parsedParamTag) {
286-
if ($parsedParamTag instanceof \phpDocumentor\Reflection\DocBlock\Tags\Generic) {
287-
$spitedData = Utils::splitTypeAndVariable($parsedParamTag);
288-
$parsedParamTagStr = $spitedData['parsedParamTagStr'];
289-
290-
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagStr);
286+
if (!$parsedParamTag instanceof \phpDocumentor\Reflection\DocBlock\Tags\Generic) {
287+
continue;
291288
}
289+
290+
$spitedData = Utils::splitTypeAndVariable($parsedParamTag);
291+
$parsedParamTagStr = $spitedData['parsedParamTagStr'];
292+
293+
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagStr);
292294
}
293295
}
294296
} catch (\Exception $e) {
295297
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
296-
297-
// DEBUG
298-
//\var_dump($tmpErrorMessage, $e->getTraceAsString());
299-
300298
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
301299
}
302300
}

src/voku/SimplePhpParser/Parsers/Helper/Utils.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,6 @@ public static function parseDocTypeObject($type): string
334334
return $type . '';
335335
}
336336

337-
/**
338-
* @param string $functionName
339-
*
340-
* @return \ReflectionFunction
341-
*/
342337
public static function createFunctionReflectionInstance(string $functionName): ReflectionFunction
343338
{
344339
static $FUNCTION_REFLECTION_INSTANCE = [];
@@ -356,10 +351,6 @@ public static function createFunctionReflectionInstance(string $functionName): R
356351
}
357352

358353
/**
359-
* @param string $className
360-
*
361-
* @return \ReflectionClass
362-
*
363354
* @phpstan-param class-string $className
364355
*/
365356
public static function createClassReflectionInstance(string $className): ReflectionClass
@@ -384,8 +375,6 @@ public static function createClassReflectionInstance(string $className): Reflect
384375
* @param string[] $additionalTags
385376
*
386377
* @phpstan-param array<string, class-string<\phpDocumentor\Reflection\DocBlock\Tag>> $additionalTags
387-
*
388-
* @return \phpDocumentor\Reflection\DocBlockFactory
389378
*/
390379
public static function createDocBlockInstance(array $additionalTags = []): \phpDocumentor\Reflection\DocBlockFactory
391380
{
@@ -401,13 +390,11 @@ public static function createDocBlockInstance(array $additionalTags = []): \phpD
401390
$typeResolver = new \phpDocumentor\Reflection\TypeResolver($fqsenResolver);
402391

403392
/**
404-
* @noinspection PhpParamsInspection
405393
* @psalm-suppress InvalidArgument - false-positive from "ReflectionDocBlock" + PHP >= 7.2
406394
*/
407395
$tagFactory->addService($descriptionFactory);
408396

409397
/**
410-
* @noinspection PhpParamsInspection
411398
* @psalm-suppress InvalidArgument - false-positive from "ReflectionDocBlock" + PHP >= 7.2
412399
*/
413400
$tagFactory->addService($typeResolver);
@@ -433,6 +420,9 @@ public static function modernPhpdocTokens(string $input): \PHPStan\PhpDocParser\
433420
return new \PHPStan\PhpDocParser\Parser\TokenIterator($LAXER->tokenize($input));
434421
}
435422

423+
/**
424+
* @throws \PHPStan\PhpDocParser\Parser\ParserException
425+
*/
436426
public static function modernPhpdoc(string $input): string
437427
{
438428
static $TYPE_PARSER = null;

tests/ParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ public function testGetMethodsInfoFromExtendedClass(): void
605605
'typeFromPhpDocExtended' => null,
606606
],
607607
'paramsPhpDocRaw' => [
608-
'useRandInt' => null,
608+
'useRandInt' => '?list<int> $useRandInt',
609609
],
610610
'returnPhpDocRaw' => null,
611611
'line' => 90,
@@ -937,7 +937,7 @@ public function testGetMethodsInfo(): void
937937
'typeFromPhpDocExtended' => null,
938938
],
939939
'paramsPhpDocRaw' => [
940-
'useRandInt' => null,
940+
'useRandInt' => '?list<int> $useRandInt',
941941
],
942942
'returnPhpDocRaw' => null,
943943
'line' => 90,

0 commit comments

Comments
 (0)