|
16 | 16 |
|
17 | 17 | The PHP reference implementation for Type Language PhpDoc Parser. |
18 | 18 |
|
19 | | -## Resources |
| 19 | +Read [documentation pages](https://phpdoc.io) for more information. |
20 | 20 |
|
21 | | -- [Documentation](https://phpdoc.io) |
| 21 | +## Installation |
| 22 | + |
| 23 | +Type Language PHPDoc Parser is available as Composer repository and can |
| 24 | +be installed using the following command in a root of your project: |
| 25 | + |
| 26 | +```sh |
| 27 | +$ composer require type-lang/phpdoc-parser |
| 28 | +``` |
| 29 | + |
| 30 | +## Quick Start |
| 31 | + |
| 32 | +```php |
| 33 | +$parser = new \TypeLang\PHPDoc\Parser(); |
| 34 | +$result = $parser->parse(<<<'PHPDOC' |
| 35 | + /** |
| 36 | + * Example description {@see some} and blah-blah-blah. |
| 37 | + * |
| 38 | + * @Example\Annotation("foo") |
| 39 | + * @return array<non-empty-string, TypeStatement> |
| 40 | + * @throws \Throwable |
| 41 | + */ |
| 42 | + PHPDOC); |
| 43 | + |
| 44 | +var_dump($result); |
| 45 | +``` |
| 46 | + |
| 47 | +**Expected Output:** |
| 48 | +``` |
| 49 | +TypeLang\PHPDoc\DocBlock { |
| 50 | + -description: TypeLang\PHPDoc\Tag\Description\Description { |
| 51 | + -template: "Example description %1$s and blah-blah-blah." |
| 52 | + -tags: array:1 [ |
| 53 | + 0 => TypeLang\PHPDoc\Tag\Tag { |
| 54 | + #description: TypeLang\PHPDoc\Tag\Description\Description { |
| 55 | + -template: "some" |
| 56 | + -tags: [] |
| 57 | + } |
| 58 | + #name: "see" |
| 59 | + } |
| 60 | + ] |
| 61 | + } |
| 62 | + -tags: array:3 [ |
| 63 | + 0 => TypeLang\PHPDoc\Tag\Tag { |
| 64 | + #description: TypeLang\PHPDoc\Tag\Description\Description { |
| 65 | + -template: "("foo")" |
| 66 | + -tags: [] |
| 67 | + } |
| 68 | + #name: "Example\Annotation" |
| 69 | + } |
| 70 | + 1 => TypeLang\PHPDoc\Tag\Tag { |
| 71 | + #description: TypeLang\PHPDoc\Tag\Description\Description { |
| 72 | + -template: "array<non-empty-string, TypeStatement>" |
| 73 | + -tags: [] |
| 74 | + } |
| 75 | + #name: "return" |
| 76 | + } |
| 77 | + 2 => TypeLang\PHPDoc\Tag\Tag { |
| 78 | + #description: TypeLang\PHPDoc\Tag\Description\Description { |
| 79 | + -template: "\Throwable" |
| 80 | + -tags: [] |
| 81 | + } |
| 82 | + #name: "throws" |
| 83 | + } |
| 84 | + ] |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### Structural Elements |
| 89 | + |
| 90 | +**DocBlock** |
| 91 | + |
| 92 | +DocBlock is a representation of the comment object. |
| 93 | + |
| 94 | +```php |
| 95 | +/** | |
| 96 | + * Hello world | ← DocBlock's description. |
| 97 | + * | |
| 98 | + * @param int $example | ← DocBlock's tag #1. |
| 99 | + * @throws \Throwable Description | ← DocBlock's tag #2. |
| 100 | + */ | |
| 101 | +``` |
| 102 | + |
| 103 | +- `getDescription()` ― Provides a `Description` object. |
| 104 | +- `getTags()` ― Provides a list of `Tag` objects. |
| 105 | + |
| 106 | +```php |
| 107 | +/** @template-implements \Traversable<array-key, Tag> */ |
| 108 | +class DocBlock implements \Traversable |
| 109 | +{ |
| 110 | + public function getDescription(): Description; |
| 111 | + |
| 112 | + /** @return list<Tag> */ |
| 113 | + public function getTags(): array; |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +**Description** |
| 118 | + |
| 119 | +Description is a representation of the description object which may contain |
| 120 | +other tags. |
| 121 | + |
| 122 | +```php |
| 123 | +/** |
| 124 | + ↓↓↓↓↓↓↓↓↓↓↓ | ← This is a nested tag of the description. |
| 125 | + * Hello world {@see some} and blah-blah-blah. | |
| 126 | + ↑↑↑↑↑↑↑↑↑↑↑ ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ | ← This is part of the template. |
| 127 | + */ |
| 128 | +``` |
| 129 | + |
| 130 | +- `getTemplate()` ― Provides a sprintf-formatted template string of the description. |
| 131 | +- `getTags()` ― Provides a list of `Tag` objects. |
| 132 | + |
| 133 | +```php |
| 134 | +/** @template-implements \Traversable<array-key, Tag> */ |
| 135 | +class Description implements \Traversable, \Stringable |
| 136 | +{ |
| 137 | + public function getTemplate(): string; |
| 138 | + |
| 139 | + /** @return list<Tag> */ |
| 140 | + public function getTags(): array; |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +**Tag** |
| 145 | + |
| 146 | +A Tag represents a name (ID) and its contents. |
| 147 | + |
| 148 | +```php |
| 149 | +/** |
| 150 | + ↓↓↓↓↓↓ | ← This is a tag name. |
| 151 | + * @throws \Throwable An error occurred. | |
| 152 | + ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ | ← This is tag description. |
| 153 | + */ |
| 154 | +``` |
| 155 | + |
| 156 | +- `getName()` ― Provides a tag's name (ID). |
| 157 | +- `getDescription()` ― Provides an optional description of the tag. |
| 158 | + |
| 159 | +```php |
| 160 | +class Tag implements \Stringable |
| 161 | +{ |
| 162 | + public function getName(): string; |
| 163 | + |
| 164 | + public function getDescription(): ?Description; |
| 165 | +} |
| 166 | +``` |
0 commit comments