Skip to content

Commit cae72c6

Browse files
authored
Log errors when resolving types + correct version of phpdocumentor/type-resolver (#340)
1 parent 32fea15 commit cae72c6

File tree

6 files changed

+84
-61
lines changed

6 files changed

+84
-61
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Yii Framework 2 apidoc extension Change Log
66

77
- Bug #338: Fix deprecation error `Using null as an array offset is deprecated, use an empty string instead` (mspirkov)
88
- Enh #337: Log invalid tags (mspirkov)
9-
- Enh #339: Add support for PHPStan/Psalm syntax (mspirkov)
9+
- Enh #339, #340: Add support for PHPStan/Psalm syntax (mspirkov)
1010
- Enh #339: Add support for intersection types (mspirkov)
1111
- Bug #339: Fix the mechanism for replacing `static` with FQCN (mspirkov)
1212
- Bug #339: Fix processing of multidimensional arrays (mspirkov)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"yiisoft/yii2": "~2.0.16",
2626
"yiisoft/yii2-bootstrap": "~2.0.0",
2727
"phpdocumentor/reflection": "^5.3.0 || ^6.0.0",
28-
"phpdocumentor/type-resolver": "^1.11",
28+
"phpdocumentor/type-resolver": "^1.12",
2929
"nikic/php-parser": "^4.0 || ^5.0",
3030
"cebe/js-search": "~0.9.0",
3131
"cebe/markdown": "^1.0",

models/BaseDoc.php

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace yii\apidoc\models;
1010

11+
use InvalidArgumentException;
1112
use phpDocumentor\Reflection\DocBlock\Tag;
1213
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
1314
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
@@ -22,6 +23,7 @@
2223
use phpDocumentor\Reflection\Php\Property;
2324
use phpDocumentor\Reflection\Php\Trait_;
2425
use phpDocumentor\Reflection\TypeResolver;
26+
use RuntimeException;
2527
use yii\base\BaseObject;
2628
use yii\helpers\StringHelper;
2729

@@ -242,51 +244,63 @@ public function __construct($parent = null, $reflector = null, $context = null,
242244
$this->templates[(string) $fqsen] = $tag;
243245
unset($this->tags[$i]);
244246
} elseif ($tag instanceof Generic) {
245-
if ($tag->getName() === self::TODO_TAG_NAME) {
246-
$this->todos[] = $tag;
247-
unset($this->tags[$i]);
248-
} elseif ($tag->getName() === self::PHPSTAN_TYPE_ANNOTATION_NAME) {
249-
$tagData = explode(' ', trim($tag->getDescription()), 2);
250-
$phpStanType = new PseudoTypeDoc(
251-
PseudoTypeDoc::TYPE_PHPSTAN,
252-
$this,
253-
trim($tagData[0]),
254-
$typeResolver->resolve(trim($tagData[1]), $this->phpDocContext)
255-
);
256-
$fqsen = $fqsenResolver->resolve($phpStanType->name, $this->phpDocContext);
257-
$this->phpStanTypes[(string) $fqsen] = $phpStanType;
258-
unset($this->tags[$i]);
259-
} elseif ($tag->getName() === self::PSALM_TYPE_ANNOTATION_NAME) {
260-
$tagData = explode('=', trim($tag->getDescription()), 2);
261-
$psalmType = new PseudoTypeDoc(
262-
PseudoTypeDoc::TYPE_PSALM,
263-
$this,
264-
trim($tagData[0]),
265-
$typeResolver->resolve(trim($tagData[1]), $this->phpDocContext)
266-
);
267-
$fqsen = $fqsenResolver->resolve($psalmType->name, $this->phpDocContext);
268-
$this->psalmTypes[(string) $fqsen] = $psalmType;
269-
unset($this->tags[$i]);
270-
} elseif ($tag->getName() === self::PHPSTAN_IMPORT_TYPE_ANNOTATION_NAME) {
271-
$tagData = explode(' from ', trim($tag->getDescription()), 2);
272-
$phpStanTypeImport = new PseudoTypeImportDoc(
273-
PseudoTypeImportDoc::TYPE_PHPSTAN,
274-
trim($tagData[0]),
275-
$fqsenResolver->resolve(trim($tagData[1]), $this->phpDocContext)
276-
);
277-
$fqsen = $fqsenResolver->resolve($phpStanTypeImport->typeName, $this->phpDocContext);
278-
$this->phpStanTypeImports[(string) $fqsen] = $phpStanTypeImport;
279-
unset($this->tags[$i]);
280-
} elseif ($tag->getName() === self::PSALM_IMPORT_TYPE_ANNOTATION_NAME) {
281-
$tagData = explode(' from ', trim($tag->getDescription()), 2);
282-
$psalmTypeImport = new PseudoTypeImportDoc(
283-
PseudoTypeImportDoc::TYPE_PSALM,
284-
trim($tagData[0]),
285-
$fqsenResolver->resolve(trim($tagData[1]), $this->phpDocContext)
286-
);
287-
$fqsen = $fqsenResolver->resolve($psalmTypeImport->typeName, $this->phpDocContext);
288-
$this->psalmTypeImports[(string) $fqsen] = $psalmTypeImport;
289-
unset($this->tags[$i]);
247+
try {
248+
if ($tag->getName() === self::TODO_TAG_NAME) {
249+
$this->todos[] = $tag;
250+
unset($this->tags[$i]);
251+
} elseif ($tag->getName() === self::PHPSTAN_TYPE_ANNOTATION_NAME) {
252+
$tagData = explode(' ', trim($tag->getDescription()), 2);
253+
$phpStanType = new PseudoTypeDoc(
254+
PseudoTypeDoc::TYPE_PHPSTAN,
255+
$this,
256+
trim($tagData[0]),
257+
$typeResolver->resolve(trim($tagData[1]), $this->phpDocContext)
258+
);
259+
$fqsen = $fqsenResolver->resolve($phpStanType->name, $this->phpDocContext);
260+
$this->phpStanTypes[(string) $fqsen] = $phpStanType;
261+
unset($this->tags[$i]);
262+
} elseif ($tag->getName() === self::PSALM_TYPE_ANNOTATION_NAME) {
263+
$tagData = explode('=', trim($tag->getDescription()), 2);
264+
$psalmType = new PseudoTypeDoc(
265+
PseudoTypeDoc::TYPE_PSALM,
266+
$this,
267+
trim($tagData[0]),
268+
$typeResolver->resolve(trim($tagData[1]), $this->phpDocContext)
269+
);
270+
$fqsen = $fqsenResolver->resolve($psalmType->name, $this->phpDocContext);
271+
$this->psalmTypes[(string) $fqsen] = $psalmType;
272+
unset($this->tags[$i]);
273+
} elseif ($tag->getName() === self::PHPSTAN_IMPORT_TYPE_ANNOTATION_NAME) {
274+
$tagData = explode(' from ', trim($tag->getDescription()), 2);
275+
$phpStanTypeImport = new PseudoTypeImportDoc(
276+
PseudoTypeImportDoc::TYPE_PHPSTAN,
277+
trim($tagData[0]),
278+
$fqsenResolver->resolve(trim($tagData[1]), $this->phpDocContext)
279+
);
280+
$fqsen = $fqsenResolver->resolve($phpStanTypeImport->typeName, $this->phpDocContext);
281+
$this->phpStanTypeImports[(string) $fqsen] = $phpStanTypeImport;
282+
unset($this->tags[$i]);
283+
} elseif ($tag->getName() === self::PSALM_IMPORT_TYPE_ANNOTATION_NAME) {
284+
$tagData = explode(' from ', trim($tag->getDescription()), 2);
285+
$psalmTypeImport = new PseudoTypeImportDoc(
286+
PseudoTypeImportDoc::TYPE_PSALM,
287+
trim($tagData[0]),
288+
$fqsenResolver->resolve(trim($tagData[1]), $this->phpDocContext)
289+
);
290+
$fqsen = $fqsenResolver->resolve($psalmTypeImport->typeName, $this->phpDocContext);
291+
$this->psalmTypeImports[(string) $fqsen] = $psalmTypeImport;
292+
unset($this->tags[$i]);
293+
}
294+
} catch (InvalidArgumentException | RuntimeException $e) {
295+
if ($context !== null){
296+
$context->errors[] = [
297+
'line' => $this->startLine,
298+
'file' => $this->sourceFile,
299+
'message' => 'Exception: ' . $e->getMessage(),
300+
];
301+
} else {
302+
throw $e;
303+
}
290304
}
291305
} elseif ($tag instanceof InvalidTag && $context !== null) {
292306
$exception = $tag->getException();

tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__18.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,91 +233,91 @@ Array
233233

234234
[33] => Array
235235
(
236-
[line] => 32
236+
[line] => 34
237237
[file] => /tests/data/api/animal/Dog.php
238238
[message] => No short description for Method 'getThreeStringsArray'
239239
)
240240

241241
[34] => Array
242242
(
243-
[line] => 40
243+
[line] => 42
244244
[file] => /tests/data/api/animal/Dog.php
245245
[message] => No short description for Method 'testOffsetAccess'
246246
)
247247

248248
[35] => Array
249249
(
250-
[line] => 48
250+
[line] => 50
251251
[file] => /tests/data/api/animal/Dog.php
252252
[message] => No short description for Method 'getNonEmptyList'
253253
)
254254

255255
[36] => Array
256256
(
257-
[line] => 56
257+
[line] => 58
258258
[file] => /tests/data/api/animal/Dog.php
259259
[message] => No short description for Method 'getListWithoutGenerics'
260260
)
261261

262262
[37] => Array
263263
(
264-
[line] => 64
264+
[line] => 66
265265
[file] => /tests/data/api/animal/Dog.php
266266
[message] => No short description for Method 'getNonEmptyListWithoutGenerics'
267267
)
268268

269269
[38] => Array
270270
(
271-
[line] => 72
271+
[line] => 74
272272
[file] => /tests/data/api/animal/Dog.php
273273
[message] => No short description for Method 'getClassWithTwoGenerics'
274274
)
275275

276276
[39] => Array
277277
(
278-
[line] => 79
278+
[line] => 81
279279
[file] => /tests/data/api/animal/Dog.php
280280
[message] => No short description for Method 'methodWithInvalidReturnTag'
281281
)
282282

283283
[40] => Array
284284
(
285-
[line] => 86
285+
[line] => 88
286286
[file] => /tests/data/api/animal/Dog.php
287287
[message] => No short description for Method 'getArrayOfStatic'
288288
)
289289

290290
[41] => Array
291291
(
292-
[line] => 93
292+
[line] => 95
293293
[file] => /tests/data/api/animal/Dog.php
294294
[message] => No short description for Method 'getArrayWithStaticGeneric'
295295
)
296296

297297
[42] => Array
298298
(
299-
[line] => 100
299+
[line] => 102
300300
[file] => /tests/data/api/animal/Dog.php
301301
[message] => No short description for Method 'getIterableWithStaticGeneric'
302302
)
303303

304304
[43] => Array
305305
(
306-
[line] => 107
306+
[line] => 109
307307
[file] => /tests/data/api/animal/Dog.php
308308
[message] => No short description for Method 'getArrayShapeWithStaticGeneric'
309309
)
310310

311311
[44] => Array
312312
(
313-
[line] => 114
313+
[line] => 116
314314
[file] => /tests/data/api/animal/Dog.php
315315
[message] => No short description for Method 'getObjectShapeWithStaticGeneric'
316316
)
317317

318318
[45] => Array
319319
(
320-
[line] => 121
320+
[line] => 123
321321
[file] => /tests/data/api/animal/Dog.php
322322
[message] => No short description for Method 'getStaticOrNull'
323323
)

tests/commands/__snapshots__/ApiControllerTest__testGenerateBootstrap__19.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ Array
3030

3131
[4] => Array
3232
(
33-
[line] => 79
33+
[line] => 20
34+
[file] => /tests/data/api/animal/Dog.php
35+
[message] => Exception: "\yiiunit\apidoc\data\api\animal\invalid-type" is not a valid Fqsen.
36+
)
37+
38+
[5] => Array
39+
(
40+
[line] => 81
3441
[file] => /tests/data/api/animal/Dog.php
3542
[message] => Invalid tag: @return invalid-type. Exception message: "\yiiunit\apidoc\data\api\animal\invalid-type" is not a valid Fqsen.
3643
)

tests/data/api/animal/Dog.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*
1313
* @phpstan-type MyArray array{foo: int, bar: string}
1414
*
15+
* @phpstan-type InvalidType invalid-type
16+
*
1517
* @author Paul Klimov <[email protected]>
1618
* @since 1.1
1719
*/

0 commit comments

Comments
 (0)