Skip to content

Commit 6a0529e

Browse files
committed
[+]: fix for modern phpdocs
-> see phpDocumentor/ReflectionDocBlock#263
1 parent 43c5b59 commit 6a0529e

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

composer.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
"require": {
1919
"php": ">=7.2",
2020
"amphp/parallel": "~1.4",
21-
"phpdocumentor/type-resolver": "~1.3",
22-
"phpdocumentor/reflection-docblock": "~5.1",
21+
"phpdocumentor/type-resolver": "~1.4",
22+
"phpdocumentor/reflection-docblock": "~5.2",
2323
"phpdocumentor/reflection-common": "~2.2",
2424
"phpstan/phpdoc-parser": "~0.4",
25+
"voku/better-reflection": "dev-master",
2526
"voku/simple-cache": "~4.0",
26-
"nikic/php-parser": "~4.10",
27-
"ondrejmirtes/better-reflection": "~4.3"
27+
"nikic/php-parser": "~4.10"
2828
},
2929
"require-dev": {
3030
"phpunit/phpunit": "~6.0 || ~7.0 || ~9.0"

src/voku/SimplePhpParser/Model/PHPFunction.php

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace voku\SimplePhpParser\Model;
66

77
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
8+
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
89
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
910
use PhpParser\Node\Stmt\Function_;
1011
use Roave\BetterReflection\Reflection\ReflectionFunction;
@@ -271,6 +272,10 @@ protected function readPhpDoc(string $docComment): void
271272
if ($this->returnTypeFromPhpDoc) {
272273
$this->returnTypeFromPhpDocExtended = Utils::modernPhpdoc($this->returnTypeFromPhpDoc);
273274
}
275+
} elseif (!empty($parsedReturnTag) && $parsedReturnTag[0] instanceof InvalidTag) {
276+
$parsedReturnTagReturn = (string) $parsedReturnTag[0];
277+
278+
$this->returnTypeFromPhpDocExtended = Utils::modernPhpdoc($parsedReturnTagReturn);
274279
}
275280

276281
/** @noinspection AdditionOperationOnArraysInspection */

src/voku/SimplePhpParser/Model/PHPParameter.php

+11
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ private function readPhpDoc(string $docComment, string $parameterName): void
282282
if ($this->typeFromPhpDoc) {
283283
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($this->typeFromPhpDoc);
284284
}
285+
} elseif ($parsedParamTag instanceof \phpDocumentor\Reflection\DocBlock\Tags\InvalidTag) {
286+
$parsedParamTagParam = (string) $parsedParamTag;
287+
288+
\preg_match('#\$\p{L}*#u', $parsedParamTagParam, $variableName);
289+
if (
290+
\count($variableName) > 0
291+
&&
292+
\strtoupper('$' . $parameterName) === \strtoupper($variableName[0])
293+
) {
294+
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagParam);
295+
}
285296
}
286297
}
287298
}

tests/Dummy8.php

+25
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,29 @@ public function getFieldArray($RowOffset, $OrderByField, $OrderByDir): array
2121
['foo' => 2]
2222
];
2323
}
24+
25+
/**
26+
* @return list<int>
27+
*/
28+
public function foo_list() {
29+
return [4, 1, 2, 3, 4];
30+
}
31+
32+
/**
33+
* @param array{stdClass: \stdClass, numbers: int|float} $lall
34+
*
35+
* @return array{stdClass: \stdClass, numbers: int|float}
36+
*/
37+
public function foo_mixed($lall) {
38+
return $lall;
39+
}
40+
41+
/**
42+
* @param array{stdClass: \stdClass, numbers: int|float $lall
43+
*
44+
* @return array{stdClass: \stdClass, numbers: int|float
45+
*/
46+
public function foo_broken($lall) {
47+
return $lall;
48+
}
2449
}

tests/ParserTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ public function testSimpleOneClassWithTrait(): void
7979
$method = $phpClasses[Dummy8::class]->methods['getLallTrait'];
8080

8181
static::assertSame('getLallTrait', $method->name);
82+
83+
static::assertSame(
84+
'array{stdClass: \stdClass, numbers: (int|float)}',
85+
$phpClasses[Dummy8::class]->methods['foo_mixed']->returnTypeFromPhpDocExtended
86+
);
87+
88+
static::assertSame(
89+
'array{stdClass: \stdClass, numbers: (int|float)}',
90+
$phpClasses[Dummy8::class]->methods['foo_mixed']->parameters['lall']->typeFromPhpDocExtended
91+
);
92+
93+
static::assertNull($phpClasses[Dummy8::class]->methods['foo_broken']->returnTypeFromPhpDocExtended);
94+
95+
static::assertNull($phpClasses[Dummy8::class]->methods['foo_broken']->parameters['lall']->typeFromPhpDocExtended);
8296
}
8397

8498
public function testSimpleOneTrait(): void

0 commit comments

Comments
 (0)