Skip to content

Commit 3736ffc

Browse files
committed
Log errors when resolving types + correct version of phpdocumentor/type-resolver
1 parent 32fea15 commit 3736ffc

File tree

5 files changed

+80
-60
lines changed

5 files changed

+80
-60
lines changed

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: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use phpDocumentor\Reflection\Php\Property;
2323
use phpDocumentor\Reflection\Php\Trait_;
2424
use phpDocumentor\Reflection\TypeResolver;
25+
use Throwable;
2526
use yii\base\BaseObject;
2627
use yii\helpers\StringHelper;
2728

@@ -242,51 +243,61 @@ public function __construct($parent = null, $reflector = null, $context = null,
242243
$this->templates[(string) $fqsen] = $tag;
243244
unset($this->tags[$i]);
244245
} 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]);
246+
try {
247+
if ($tag->getName() === self::TODO_TAG_NAME) {
248+
$this->todos[] = $tag;
249+
unset($this->tags[$i]);
250+
} elseif ($tag->getName() === self::PHPSTAN_TYPE_ANNOTATION_NAME) {
251+
$tagData = explode(' ', trim($tag->getDescription()), 2);
252+
$phpStanType = new PseudoTypeDoc(
253+
PseudoTypeDoc::TYPE_PHPSTAN,
254+
$this,
255+
trim($tagData[0]),
256+
$typeResolver->resolve(trim($tagData[1]), $this->phpDocContext)
257+
);
258+
$fqsen = $fqsenResolver->resolve($phpStanType->name, $this->phpDocContext);
259+
$this->phpStanTypes[(string) $fqsen] = $phpStanType;
260+
unset($this->tags[$i]);
261+
} elseif ($tag->getName() === self::PSALM_TYPE_ANNOTATION_NAME) {
262+
$tagData = explode('=', trim($tag->getDescription()), 2);
263+
$psalmType = new PseudoTypeDoc(
264+
PseudoTypeDoc::TYPE_PSALM,
265+
$this,
266+
trim($tagData[0]),
267+
$typeResolver->resolve(trim($tagData[1]), $this->phpDocContext)
268+
);
269+
$fqsen = $fqsenResolver->resolve($psalmType->name, $this->phpDocContext);
270+
$this->psalmTypes[(string) $fqsen] = $psalmType;
271+
unset($this->tags[$i]);
272+
} elseif ($tag->getName() === self::PHPSTAN_IMPORT_TYPE_ANNOTATION_NAME) {
273+
$tagData = explode(' from ', trim($tag->getDescription()), 2);
274+
$phpStanTypeImport = new PseudoTypeImportDoc(
275+
PseudoTypeImportDoc::TYPE_PHPSTAN,
276+
trim($tagData[0]),
277+
$fqsenResolver->resolve(trim($tagData[1]), $this->phpDocContext)
278+
);
279+
$fqsen = $fqsenResolver->resolve($phpStanTypeImport->typeName, $this->phpDocContext);
280+
$this->phpStanTypeImports[(string) $fqsen] = $phpStanTypeImport;
281+
unset($this->tags[$i]);
282+
} elseif ($tag->getName() === self::PSALM_IMPORT_TYPE_ANNOTATION_NAME) {
283+
$tagData = explode(' from ', trim($tag->getDescription()), 2);
284+
$psalmTypeImport = new PseudoTypeImportDoc(
285+
PseudoTypeImportDoc::TYPE_PSALM,
286+
trim($tagData[0]),
287+
$fqsenResolver->resolve(trim($tagData[1]), $this->phpDocContext)
288+
);
289+
$fqsen = $fqsenResolver->resolve($psalmTypeImport->typeName, $this->phpDocContext);
290+
$this->psalmTypeImports[(string) $fqsen] = $psalmTypeImport;
291+
unset($this->tags[$i]);
292+
}
293+
} catch (Throwable $e) {
294+
if ($context !== null){
295+
$context->errors[] = [
296+
'line' => $this->startLine,
297+
'file' => $this->sourceFile,
298+
'message' => 'Exception: ' . $e->getMessage(),
299+
];
300+
}
290301
}
291302
} elseif ($tag instanceof InvalidTag && $context !== null) {
292303
$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 <klimov.paul@gmail.com>
1618
* @since 1.1
1719
*/

0 commit comments

Comments
 (0)