Skip to content

Commit b72daff

Browse files
committed
[+]: use "phpstan/phpdoc-parser" for more phpdocs + more tests
1 parent db219c8 commit b72daff

File tree

9 files changed

+94
-25
lines changed

9 files changed

+94
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### 0.20.1 (2023-08-11)
4+
5+
- use "phpstan/phpdoc-parser" for more phpdocs
6+
37
### 0.20.0 (2023-07-31)
48

59
- add support for "readonly" properties

src/voku/SimplePhpParser/Model/PHPClass.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,14 @@ public function readObjectFromReflection($clazz): self
224224
*
225225
* @return array
226226
*
227-
* @psalm-return array<string, array{type: null|string, typeFromPhpDocMaybeWithComment: null|string, typeFromPhpDoc: null|string, typeFromPhpDocSimple: null|string, typeFromPhpDocExtended: null|string, typeFromDefaultValue: null|string}>
227+
* @psalm-return array<string, array{
228+
* type: null|string,
229+
* typeFromPhpDocMaybeWithComment: null|string,
230+
* typeFromPhpDoc: null|string,
231+
* typeFromPhpDocSimple: null|string,
232+
* typeFromPhpDocExtended: null|string,
233+
* typeFromDefaultValue: null|string
234+
* }>
228235
*/
229236
public function getPropertiesInfo(
230237
array $access = ['public', 'protected', 'private'],

src/voku/SimplePhpParser/Model/PHPFunction.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,49 @@ protected function readPhpDoc($doc): void
271271
);
272272
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
273273
}
274+
275+
try {
276+
$this->readPhpDocByTokens($docComment);
277+
} catch (\Exception $e) {
278+
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
279+
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
280+
}
281+
}
282+
283+
/**
284+
* @throws \PHPStan\PhpDocParser\Parser\ParserException
285+
*/
286+
private function readPhpDocByTokens(string $docComment): void
287+
{
288+
$tokens = Utils::modernPhpdocTokens($docComment);
289+
290+
$returnContent = null;
291+
foreach ($tokens->getTokens() as $token) {
292+
$content = $token[0];
293+
294+
if ($content === '@return' || $content === '@psalm-return' || $content === '@phpstan-return') {
295+
// reset
296+
$returnContent = '';
297+
298+
continue;
299+
}
300+
301+
// We can stop if we found the end.
302+
if ($content === '*/') {
303+
break;
304+
}
305+
306+
if ($returnContent !== null) {
307+
$returnContent .= $content;
308+
}
309+
}
310+
311+
$returnContent = $returnContent ? \trim($returnContent) : null;
312+
if ($returnContent) {
313+
if (!$this->returnPhpDocRaw) {
314+
$this->returnPhpDocRaw = $returnContent;
315+
}
316+
$this->returnTypeFromPhpDocExtended = Utils::modernPhpdoc($returnContent);
317+
}
274318
}
275319
}

src/voku/SimplePhpParser/Model/PHPParameter.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,13 @@ private function readPhpDoc($doc, string $parameterName): void
287287
}
288288
}
289289

290-
$this->readPhpDocByTokens($docComment, $parameterName);
290+
} catch (\Exception $e) {
291+
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
292+
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
293+
}
291294

295+
try {
296+
$this->readPhpDocByTokens($docComment, $parameterName);
292297
} catch (\Exception $e) {
293298
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
294299
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
@@ -325,7 +330,9 @@ private function readPhpDocByTokens(string $docComment, string $parameterName):
325330

326331
$paramContent = $paramContent ? \trim($paramContent) : null;
327332
if ($paramContent) {
328-
$this->phpDocRaw = $paramContent . ' ' . '$' . $parameterName;
333+
if (!$this->phpDocRaw) {
334+
$this->phpDocRaw = $paramContent . ' ' . '$' . $parameterName;
335+
}
329336
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($paramContent);
330337
}
331338
}

src/voku/SimplePhpParser/Model/PHPProperty.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,13 @@ private function readPhpDoc($doc): void
278278
}
279279
}
280280

281-
$this->readPhpDocByTokens($docComment);
281+
} catch (\Exception $e) {
282+
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
283+
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
284+
}
282285

286+
try {
287+
$this->readPhpDocByTokens($docComment);
283288
} catch (\Exception $e) {
284289
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
285290
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
@@ -293,26 +298,28 @@ private function readPhpDocByTokens(string $docComment): void
293298
{
294299
$tokens = Utils::modernPhpdocTokens($docComment);
295300

296-
$paramContent = null;
301+
$varContent = null;
297302
foreach ($tokens->getTokens() as $token) {
298303
$content = $token[0];
299304

300305
if ($content === '@var' || $content === '@psalm-var' || $content === '@phpstan-var') {
301306
// reset
302-
$paramContent = '';
307+
$varContent = '';
303308

304309
continue;
305310
}
306311

307-
if ($paramContent !== null) {
308-
$paramContent .= $content;
312+
if ($varContent !== null) {
313+
$varContent .= $content;
309314
}
310315
}
311316

312-
$paramContent = $paramContent ? \trim($paramContent) : null;
313-
if ($paramContent) {
314-
$this->phpDocRaw = $paramContent;
315-
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($paramContent);
317+
$varContent = $varContent ? \trim($varContent) : null;
318+
if ($varContent) {
319+
if (!$this->phpDocRaw) {
320+
$this->phpDocRaw = $varContent;
321+
}
322+
$this->typeFromPhpDocExtended = Utils::modernPhpdoc($varContent);
316323
}
317324
}
318325
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,7 @@ public static function parseDocTypeObject($type): string
246246
}
247247

248248
if ($type instanceof \phpDocumentor\Reflection\Types\Object_) {
249-
$tmpObject = (string) $type->getFqsen();
250-
if ($tmpObject) {
251-
return $tmpObject;
252-
}
253-
254-
return 'object';
249+
return $type->__toString();
255250
}
256251

257252
if (\is_array($type) || $type instanceof \phpDocumentor\Reflection\Types\Compound) {

src/voku/SimplePhpParser/Parsers/PhpCodeParser.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,10 @@ public static function getPhpFiles(
8282
): ParserContainer {
8383
foreach ($autoloaderProjectPaths as $projectPath) {
8484
if (\file_exists($projectPath) && \is_file($projectPath)) {
85-
/** @noinspection PhpIncludeInspection */
8685
require_once $projectPath;
8786
} elseif (\file_exists($projectPath . '/vendor/autoload.php')) {
88-
/** @noinspection PhpIncludeInspection */
8987
require_once $projectPath . '/vendor/autoload.php';
9088
} elseif (\file_exists($projectPath . '/../vendor/autoload.php')) {
91-
/** @noinspection PhpIncludeInspection */
9289
require_once $projectPath . '/../vendor/autoload.php';
9390
}
9491
}

tests/Dummy13.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public function __construct(int $lall)
1919
$this->lall = $lall;
2020
}
2121

22+
/**
23+
* @return callable(): int<0,1>
24+
*/
25+
public function callableTest(): callable {
26+
return static function() { return 1; };
27+
}
28+
2229
/**
2330
* {@inheritdoc}
2431
*/

tests/ParserTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ public function testReadonlyClass(): void
143143
static::assertSame(true, $isReadonly);
144144

145145
static::assertSame('callable(int): string', $phpClasses[Dummy13::class]->properties['lall']->typeFromPhpDocExtended);
146-
147146
$isReadonly = $phpClasses[Dummy13::class]->properties['lall']->is_readonly;
148147
static::assertSame(true, $isReadonly);
148+
149+
static::assertSame('callable(): int<0, 1>', $phpClasses[Dummy13::class]->methods['callableTest']->returnTypeFromPhpDocExtended);
149150
} else {
150151
static::markTestSkipped('only for PHP >= 8.2');
151152
}
@@ -202,12 +203,12 @@ public function testSimpleOneClassWithTrait(): void
202203
);
203204

204205
static::assertSame(
205-
null,
206+
'array{stdClass: \stdClass, numbers: int|float $lall',
206207
$phpClasses[Dummy8::class]->methods['foo_broken']->parameters['lall']->phpDocRaw
207208
);
208209

209210
static::assertSame(
210-
null,
211+
'array{stdClass: \stdClass, numbers: int|float <foo/>',
211212
$phpClasses[Dummy8::class]->methods['foo_broken']->returnPhpDocRaw
212213
);
213214

@@ -440,7 +441,7 @@ public function testGetFunctionsInfo(): void
440441
'typeFromPhpDocMaybeWithComment' => '\\Dummy',
441442
'typeFromPhpDoc' => 'Dummy',
442443
'typeFromPhpDocSimple' => '\\Dummy',
443-
'typeFromPhpDocExtended' => '\\Dummy',
444+
'typeFromPhpDocExtended' => 'Dummy',
444445
],
445446
'paramsPhpDocRaw' => [
446447
'foo' => null,

0 commit comments

Comments
 (0)