Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Yii Framework 2 apidoc extension Change Log

- Bug #338: Fix deprecation error `Using null as an array offset is deprecated, use an empty string instead` (mspirkov)
- Enh #337: Log invalid tags (mspirkov)
- Enh #339: Add support for PHPStan/Psalm syntax (mspirkov)
- Enh #339, #340: Add support for PHPStan/Psalm syntax (mspirkov)
- Enh #339: Add support for intersection types (mspirkov)
- Bug #339: Fix the mechanism for replacing `static` with FQCN (mspirkov)
- Bug #339: Fix processing of multidimensional arrays (mspirkov)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"yiisoft/yii2": "~2.0.16",
"yiisoft/yii2-bootstrap": "~2.0.0",
"phpdocumentor/reflection": "^5.3.0 || ^6.0.0",
"phpdocumentor/type-resolver": "^1.11",
"phpdocumentor/type-resolver": "^1.12",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a mistake last time. We don't support 1.11.

"nikic/php-parser": "^4.0 || ^5.0",
"cebe/js-search": "~0.9.0",
"cebe/markdown": "^1.0",
Expand Down
104 changes: 59 additions & 45 deletions models/BaseDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace yii\apidoc\models;

use InvalidArgumentException;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
Expand All @@ -22,6 +23,7 @@
use phpDocumentor\Reflection\Php\Property;
use phpDocumentor\Reflection\Php\Trait_;
use phpDocumentor\Reflection\TypeResolver;
use RuntimeException;
use yii\base\BaseObject;
use yii\helpers\StringHelper;

Expand Down Expand Up @@ -242,51 +244,63 @@ public function __construct($parent = null, $reflector = null, $context = null,
$this->templates[(string) $fqsen] = $tag;
unset($this->tags[$i]);
} elseif ($tag instanceof Generic) {
if ($tag->getName() === self::TODO_TAG_NAME) {
$this->todos[] = $tag;
unset($this->tags[$i]);
} elseif ($tag->getName() === self::PHPSTAN_TYPE_ANNOTATION_NAME) {
$tagData = explode(' ', trim($tag->getDescription()), 2);
$phpStanType = new PseudoTypeDoc(
PseudoTypeDoc::TYPE_PHPSTAN,
$this,
trim($tagData[0]),
$typeResolver->resolve(trim($tagData[1]), $this->phpDocContext)
);
$fqsen = $fqsenResolver->resolve($phpStanType->name, $this->phpDocContext);
$this->phpStanTypes[(string) $fqsen] = $phpStanType;
unset($this->tags[$i]);
} elseif ($tag->getName() === self::PSALM_TYPE_ANNOTATION_NAME) {
$tagData = explode('=', trim($tag->getDescription()), 2);
$psalmType = new PseudoTypeDoc(
PseudoTypeDoc::TYPE_PSALM,
$this,
trim($tagData[0]),
$typeResolver->resolve(trim($tagData[1]), $this->phpDocContext)
);
$fqsen = $fqsenResolver->resolve($psalmType->name, $this->phpDocContext);
$this->psalmTypes[(string) $fqsen] = $psalmType;
unset($this->tags[$i]);
} elseif ($tag->getName() === self::PHPSTAN_IMPORT_TYPE_ANNOTATION_NAME) {
$tagData = explode(' from ', trim($tag->getDescription()), 2);
$phpStanTypeImport = new PseudoTypeImportDoc(
PseudoTypeImportDoc::TYPE_PHPSTAN,
trim($tagData[0]),
$fqsenResolver->resolve(trim($tagData[1]), $this->phpDocContext)
);
$fqsen = $fqsenResolver->resolve($phpStanTypeImport->typeName, $this->phpDocContext);
$this->phpStanTypeImports[(string) $fqsen] = $phpStanTypeImport;
unset($this->tags[$i]);
} elseif ($tag->getName() === self::PSALM_IMPORT_TYPE_ANNOTATION_NAME) {
$tagData = explode(' from ', trim($tag->getDescription()), 2);
$psalmTypeImport = new PseudoTypeImportDoc(
PseudoTypeImportDoc::TYPE_PSALM,
trim($tagData[0]),
$fqsenResolver->resolve(trim($tagData[1]), $this->phpDocContext)
);
$fqsen = $fqsenResolver->resolve($psalmTypeImport->typeName, $this->phpDocContext);
$this->psalmTypeImports[(string) $fqsen] = $psalmTypeImport;
unset($this->tags[$i]);
try {
if ($tag->getName() === self::TODO_TAG_NAME) {
$this->todos[] = $tag;
unset($this->tags[$i]);
} elseif ($tag->getName() === self::PHPSTAN_TYPE_ANNOTATION_NAME) {
$tagData = explode(' ', trim($tag->getDescription()), 2);
$phpStanType = new PseudoTypeDoc(
PseudoTypeDoc::TYPE_PHPSTAN,
$this,
trim($tagData[0]),
$typeResolver->resolve(trim($tagData[1]), $this->phpDocContext)
);
$fqsen = $fqsenResolver->resolve($phpStanType->name, $this->phpDocContext);
$this->phpStanTypes[(string) $fqsen] = $phpStanType;
unset($this->tags[$i]);
} elseif ($tag->getName() === self::PSALM_TYPE_ANNOTATION_NAME) {
$tagData = explode('=', trim($tag->getDescription()), 2);
$psalmType = new PseudoTypeDoc(
PseudoTypeDoc::TYPE_PSALM,
$this,
trim($tagData[0]),
$typeResolver->resolve(trim($tagData[1]), $this->phpDocContext)
);
$fqsen = $fqsenResolver->resolve($psalmType->name, $this->phpDocContext);
$this->psalmTypes[(string) $fqsen] = $psalmType;
unset($this->tags[$i]);
} elseif ($tag->getName() === self::PHPSTAN_IMPORT_TYPE_ANNOTATION_NAME) {
$tagData = explode(' from ', trim($tag->getDescription()), 2);
$phpStanTypeImport = new PseudoTypeImportDoc(
PseudoTypeImportDoc::TYPE_PHPSTAN,
trim($tagData[0]),
$fqsenResolver->resolve(trim($tagData[1]), $this->phpDocContext)
);
$fqsen = $fqsenResolver->resolve($phpStanTypeImport->typeName, $this->phpDocContext);
$this->phpStanTypeImports[(string) $fqsen] = $phpStanTypeImport;
unset($this->tags[$i]);
} elseif ($tag->getName() === self::PSALM_IMPORT_TYPE_ANNOTATION_NAME) {
$tagData = explode(' from ', trim($tag->getDescription()), 2);
$psalmTypeImport = new PseudoTypeImportDoc(
PseudoTypeImportDoc::TYPE_PSALM,
trim($tagData[0]),
$fqsenResolver->resolve(trim($tagData[1]), $this->phpDocContext)
);
$fqsen = $fqsenResolver->resolve($psalmTypeImport->typeName, $this->phpDocContext);
$this->psalmTypeImports[(string) $fqsen] = $psalmTypeImport;
unset($this->tags[$i]);
}
} catch (InvalidArgumentException | RuntimeException $e) {
if ($context !== null){
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be caught and logged, just like InvalidTag, because incorrect types should not block documentation generation.

$context->errors[] = [
'line' => $this->startLine,
'file' => $this->sourceFile,
'message' => 'Exception: ' . $e->getMessage(),
];
} else {
throw $e;
}
}
} elseif ($tag instanceof InvalidTag && $context !== null) {
$exception = $tag->getException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,91 +233,91 @@ Array

[33] => Array
(
[line] => 32
[line] => 34
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'getThreeStringsArray'
)

[34] => Array
(
[line] => 40
[line] => 42
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'testOffsetAccess'
)

[35] => Array
(
[line] => 48
[line] => 50
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'getNonEmptyList'
)

[36] => Array
(
[line] => 56
[line] => 58
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'getListWithoutGenerics'
)

[37] => Array
(
[line] => 64
[line] => 66
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'getNonEmptyListWithoutGenerics'
)

[38] => Array
(
[line] => 72
[line] => 74
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'getClassWithTwoGenerics'
)

[39] => Array
(
[line] => 79
[line] => 81
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'methodWithInvalidReturnTag'
)

[40] => Array
(
[line] => 86
[line] => 88
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'getArrayOfStatic'
)

[41] => Array
(
[line] => 93
[line] => 95
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'getArrayWithStaticGeneric'
)

[42] => Array
(
[line] => 100
[line] => 102
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'getIterableWithStaticGeneric'
)

[43] => Array
(
[line] => 107
[line] => 109
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'getArrayShapeWithStaticGeneric'
)

[44] => Array
(
[line] => 114
[line] => 116
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'getObjectShapeWithStaticGeneric'
)

[45] => Array
(
[line] => 121
[line] => 123
[file] => /tests/data/api/animal/Dog.php
[message] => No short description for Method 'getStaticOrNull'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ Array

[4] => Array
(
[line] => 79
[line] => 20
[file] => /tests/data/api/animal/Dog.php
[message] => Exception: "\yiiunit\apidoc\data\api\animal\invalid-type" is not a valid Fqsen.
)

[5] => Array
(
[line] => 81
[file] => /tests/data/api/animal/Dog.php
[message] => Invalid tag: @return invalid-type. Exception message: "\yiiunit\apidoc\data\api\animal\invalid-type" is not a valid Fqsen.
)
Expand Down
2 changes: 2 additions & 0 deletions tests/data/api/animal/Dog.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*
* @phpstan-type MyArray array{foo: int, bar: string}
*
* @phpstan-type InvalidType invalid-type
*
* @author Paul Klimov <[email protected]>
* @since 1.1
*/
Expand Down
Loading