@@ -47,59 +47,59 @@ discard block |
||
47 | 47 | */ |
48 | 48 | class DescriptionFactory |
49 | 49 | { |
50 | - /** @var TagFactory */ |
|
51 | - private $tagFactory; |
|
52 | - |
|
53 | - /** |
|
54 | - * Initializes this factory with the means to construct (inline) tags. |
|
55 | - */ |
|
56 | - public function __construct(TagFactory $tagFactory) |
|
57 | - { |
|
58 | - $this->tagFactory = $tagFactory; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Returns the parsed text of this description. |
|
63 | - */ |
|
64 | - public function create(string $contents, ?TypeContext $context = null): Description |
|
65 | - { |
|
66 | - $tokens = $this->lex($contents); |
|
67 | - $count = count($tokens); |
|
68 | - $tagCount = 0; |
|
69 | - $tags = []; |
|
70 | - |
|
71 | - for ($i = 1; $i < $count; $i += 2) { |
|
72 | - $tags[] = $this->tagFactory->create($tokens[$i], $context); |
|
73 | - $tokens[$i] = '%' . ++$tagCount . '$s'; |
|
74 | - } |
|
75 | - |
|
76 | - //In order to allow "literal" inline tags, the otherwise invalid |
|
77 | - //sequence "{@}" is changed to "@", and "{}" is changed to "}". |
|
78 | - //"%" is escaped to "%%" because of vsprintf. |
|
79 | - //See unit tests for examples. |
|
80 | - for ($i = 0; $i < $count; $i += 2) { |
|
81 | - $tokens[$i] = str_replace(['{@}', '{}', '%'], ['@', '}', '%%'], $tokens[$i]); |
|
82 | - } |
|
83 | - |
|
84 | - return new Description(implode('', $tokens), $tags); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Strips the contents from superfluous whitespace and splits the description into a series of tokens. |
|
89 | - * |
|
90 | - * @return string[] A series of tokens of which the description text is composed. |
|
91 | - */ |
|
92 | - private function lex(string $contents): array |
|
93 | - { |
|
94 | - $contents = $this->removeSuperfluousStartingWhitespace($contents); |
|
95 | - |
|
96 | - // performance optimalization; if there is no inline tag, don't bother splitting it up. |
|
97 | - if (strpos($contents, '{@') === false) { |
|
98 | - return [$contents]; |
|
99 | - } |
|
100 | - |
|
101 | - return Utils::pregSplit( |
|
102 | - '/\{ |
|
50 | + /** @var TagFactory */ |
|
51 | + private $tagFactory; |
|
52 | + |
|
53 | + /** |
|
54 | + * Initializes this factory with the means to construct (inline) tags. |
|
55 | + */ |
|
56 | + public function __construct(TagFactory $tagFactory) |
|
57 | + { |
|
58 | + $this->tagFactory = $tagFactory; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Returns the parsed text of this description. |
|
63 | + */ |
|
64 | + public function create(string $contents, ?TypeContext $context = null): Description |
|
65 | + { |
|
66 | + $tokens = $this->lex($contents); |
|
67 | + $count = count($tokens); |
|
68 | + $tagCount = 0; |
|
69 | + $tags = []; |
|
70 | + |
|
71 | + for ($i = 1; $i < $count; $i += 2) { |
|
72 | + $tags[] = $this->tagFactory->create($tokens[$i], $context); |
|
73 | + $tokens[$i] = '%' . ++$tagCount . '$s'; |
|
74 | + } |
|
75 | + |
|
76 | + //In order to allow "literal" inline tags, the otherwise invalid |
|
77 | + //sequence "{@}" is changed to "@", and "{}" is changed to "}". |
|
78 | + //"%" is escaped to "%%" because of vsprintf. |
|
79 | + //See unit tests for examples. |
|
80 | + for ($i = 0; $i < $count; $i += 2) { |
|
81 | + $tokens[$i] = str_replace(['{@}', '{}', '%'], ['@', '}', '%%'], $tokens[$i]); |
|
82 | + } |
|
83 | + |
|
84 | + return new Description(implode('', $tokens), $tags); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Strips the contents from superfluous whitespace and splits the description into a series of tokens. |
|
89 | + * |
|
90 | + * @return string[] A series of tokens of which the description text is composed. |
|
91 | + */ |
|
92 | + private function lex(string $contents): array |
|
93 | + { |
|
94 | + $contents = $this->removeSuperfluousStartingWhitespace($contents); |
|
95 | + |
|
96 | + // performance optimalization; if there is no inline tag, don't bother splitting it up. |
|
97 | + if (strpos($contents, '{@') === false) { |
|
98 | + return [$contents]; |
|
99 | + } |
|
100 | + |
|
101 | + return Utils::pregSplit( |
|
102 | + '/\{ |
|
103 | 103 | # "{@}" is not a valid inline tag. This ensures that we do not treat it as one, but treat it literally. |
104 | 104 | (?!@\}) |
105 | 105 | # We want to capture the whole tag line, but without the inline tag delimiters. |
@@ -123,56 +123,56 @@ discard block |
||
123 | 123 | # nested inline tags. |
124 | 124 | ) |
125 | 125 | \}/Sux', |
126 | - $contents, |
|
127 | - 0, |
|
128 | - PREG_SPLIT_DELIM_CAPTURE |
|
129 | - ); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Removes the superfluous from a multi-line description. |
|
134 | - * |
|
135 | - * When a description has more than one line then it can happen that the second and subsequent lines have an |
|
136 | - * additional indentation. This is commonly in use with tags like this: |
|
137 | - * |
|
138 | - * {@}since 1.1.0 This is an example |
|
139 | - * description where we have an |
|
140 | - * indentation in the second and |
|
141 | - * subsequent lines. |
|
142 | - * |
|
143 | - * If we do not normalize the indentation then we have superfluous whitespace on the second and subsequent |
|
144 | - * lines and this may cause rendering issues when, for example, using a Markdown converter. |
|
145 | - */ |
|
146 | - private function removeSuperfluousStartingWhitespace(string $contents): string |
|
147 | - { |
|
148 | - $lines = Utils::pregSplit("/\r\n?|\n/", $contents); |
|
149 | - |
|
150 | - // if there is only one line then we don't have lines with superfluous whitespace and |
|
151 | - // can use the contents as-is |
|
152 | - if (count($lines) <= 1) { |
|
153 | - return $contents; |
|
154 | - } |
|
155 | - |
|
156 | - // determine how many whitespace characters need to be stripped |
|
157 | - $startingSpaceCount = 9999999; |
|
158 | - for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) { |
|
159 | - // lines with a no length do not count as they are not indented at all |
|
160 | - if (trim($lines[$i]) === '') { |
|
161 | - continue; |
|
162 | - } |
|
163 | - |
|
164 | - // determine the number of prefixing spaces by checking the difference in line length before and after |
|
165 | - // an ltrim |
|
166 | - $startingSpaceCount = min($startingSpaceCount, strlen($lines[$i]) - strlen(ltrim($lines[$i]))); |
|
167 | - } |
|
168 | - |
|
169 | - // strip the number of spaces from each line |
|
170 | - if ($startingSpaceCount > 0) { |
|
171 | - for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) { |
|
172 | - $lines[$i] = substr($lines[$i], $startingSpaceCount); |
|
173 | - } |
|
174 | - } |
|
175 | - |
|
176 | - return implode("\n", $lines); |
|
177 | - } |
|
126 | + $contents, |
|
127 | + 0, |
|
128 | + PREG_SPLIT_DELIM_CAPTURE |
|
129 | + ); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Removes the superfluous from a multi-line description. |
|
134 | + * |
|
135 | + * When a description has more than one line then it can happen that the second and subsequent lines have an |
|
136 | + * additional indentation. This is commonly in use with tags like this: |
|
137 | + * |
|
138 | + * {@}since 1.1.0 This is an example |
|
139 | + * description where we have an |
|
140 | + * indentation in the second and |
|
141 | + * subsequent lines. |
|
142 | + * |
|
143 | + * If we do not normalize the indentation then we have superfluous whitespace on the second and subsequent |
|
144 | + * lines and this may cause rendering issues when, for example, using a Markdown converter. |
|
145 | + */ |
|
146 | + private function removeSuperfluousStartingWhitespace(string $contents): string |
|
147 | + { |
|
148 | + $lines = Utils::pregSplit("/\r\n?|\n/", $contents); |
|
149 | + |
|
150 | + // if there is only one line then we don't have lines with superfluous whitespace and |
|
151 | + // can use the contents as-is |
|
152 | + if (count($lines) <= 1) { |
|
153 | + return $contents; |
|
154 | + } |
|
155 | + |
|
156 | + // determine how many whitespace characters need to be stripped |
|
157 | + $startingSpaceCount = 9999999; |
|
158 | + for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) { |
|
159 | + // lines with a no length do not count as they are not indented at all |
|
160 | + if (trim($lines[$i]) === '') { |
|
161 | + continue; |
|
162 | + } |
|
163 | + |
|
164 | + // determine the number of prefixing spaces by checking the difference in line length before and after |
|
165 | + // an ltrim |
|
166 | + $startingSpaceCount = min($startingSpaceCount, strlen($lines[$i]) - strlen(ltrim($lines[$i]))); |
|
167 | + } |
|
168 | + |
|
169 | + // strip the number of spaces from each line |
|
170 | + if ($startingSpaceCount > 0) { |
|
171 | + for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) { |
|
172 | + $lines[$i] = substr($lines[$i], $startingSpaceCount); |
|
173 | + } |
|
174 | + } |
|
175 | + |
|
176 | + return implode("\n", $lines); |
|
177 | + } |
|
178 | 178 | } |
@@ -70,7 +70,7 @@ |
||
70 | 70 | |
71 | 71 | for ($i = 1; $i < $count; $i += 2) { |
72 | 72 | $tags[] = $this->tagFactory->create($tokens[$i], $context); |
73 | - $tokens[$i] = '%' . ++$tagCount . '$s'; |
|
73 | + $tokens[$i] = '%'.++$tagCount.'$s'; |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | //In order to allow "literal" inline tags, the otherwise invalid |
@@ -52,64 +52,64 @@ |
||
52 | 52 | */ |
53 | 53 | class Description |
54 | 54 | { |
55 | - /** @var string */ |
|
56 | - private $bodyTemplate; |
|
55 | + /** @var string */ |
|
56 | + private $bodyTemplate; |
|
57 | 57 | |
58 | - /** @var Tag[] */ |
|
59 | - private $tags; |
|
58 | + /** @var Tag[] */ |
|
59 | + private $tags; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Initializes a Description with its body (template) and a listing of the tags used in the body template. |
|
63 | - * |
|
64 | - * @param Tag[] $tags |
|
65 | - */ |
|
66 | - public function __construct(string $bodyTemplate, array $tags = []) |
|
67 | - { |
|
68 | - $this->bodyTemplate = $bodyTemplate; |
|
69 | - $this->tags = $tags; |
|
70 | - } |
|
61 | + /** |
|
62 | + * Initializes a Description with its body (template) and a listing of the tags used in the body template. |
|
63 | + * |
|
64 | + * @param Tag[] $tags |
|
65 | + */ |
|
66 | + public function __construct(string $bodyTemplate, array $tags = []) |
|
67 | + { |
|
68 | + $this->bodyTemplate = $bodyTemplate; |
|
69 | + $this->tags = $tags; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Returns the body template. |
|
74 | - */ |
|
75 | - public function getBodyTemplate(): string |
|
76 | - { |
|
77 | - return $this->bodyTemplate; |
|
78 | - } |
|
72 | + /** |
|
73 | + * Returns the body template. |
|
74 | + */ |
|
75 | + public function getBodyTemplate(): string |
|
76 | + { |
|
77 | + return $this->bodyTemplate; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Returns the tags for this DocBlock. |
|
82 | - * |
|
83 | - * @return Tag[] |
|
84 | - */ |
|
85 | - public function getTags(): array |
|
86 | - { |
|
87 | - return $this->tags; |
|
88 | - } |
|
80 | + /** |
|
81 | + * Returns the tags for this DocBlock. |
|
82 | + * |
|
83 | + * @return Tag[] |
|
84 | + */ |
|
85 | + public function getTags(): array |
|
86 | + { |
|
87 | + return $this->tags; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Renders this description as a string where the provided formatter will format the tags in the expected string |
|
92 | - * format. |
|
93 | - */ |
|
94 | - public function render(?Formatter $formatter = null): string |
|
95 | - { |
|
96 | - if ($formatter === null) { |
|
97 | - $formatter = new PassthroughFormatter(); |
|
98 | - } |
|
90 | + /** |
|
91 | + * Renders this description as a string where the provided formatter will format the tags in the expected string |
|
92 | + * format. |
|
93 | + */ |
|
94 | + public function render(?Formatter $formatter = null): string |
|
95 | + { |
|
96 | + if ($formatter === null) { |
|
97 | + $formatter = new PassthroughFormatter(); |
|
98 | + } |
|
99 | 99 | |
100 | - $tags = []; |
|
101 | - foreach ($this->tags as $tag) { |
|
102 | - $tags[] = '{' . $formatter->format($tag) . '}'; |
|
103 | - } |
|
100 | + $tags = []; |
|
101 | + foreach ($this->tags as $tag) { |
|
102 | + $tags[] = '{' . $formatter->format($tag) . '}'; |
|
103 | + } |
|
104 | 104 | |
105 | - return vsprintf($this->bodyTemplate, $tags); |
|
106 | - } |
|
105 | + return vsprintf($this->bodyTemplate, $tags); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Returns a plain string representation of this description. |
|
110 | - */ |
|
111 | - public function __toString(): string |
|
112 | - { |
|
113 | - return $this->render(); |
|
114 | - } |
|
108 | + /** |
|
109 | + * Returns a plain string representation of this description. |
|
110 | + */ |
|
111 | + public function __toString(): string |
|
112 | + { |
|
113 | + return $this->render(); |
|
114 | + } |
|
115 | 115 | } |
@@ -99,7 +99,7 @@ |
||
99 | 99 | |
100 | 100 | $tags = []; |
101 | 101 | foreach ($this->tags as $tag) { |
102 | - $tags[] = '{' . $formatter->format($tag) . '}'; |
|
102 | + $tags[] = '{'.$formatter->format($tag).'}'; |
|
103 | 103 | } |
104 | 104 | |
105 | 105 | return vsprintf($this->bodyTemplate, $tags); |
@@ -68,281 +68,281 @@ |
||
68 | 68 | */ |
69 | 69 | final class StandardTagFactory implements TagFactory |
70 | 70 | { |
71 | - /** PCRE regular expression matching a tag name. */ |
|
72 | - public const REGEX_TAGNAME = '[\w\-\_\\\\:]+'; |
|
73 | - |
|
74 | - /** |
|
75 | - * @var array<class-string<Tag>> An array with a tag as a key, and an |
|
76 | - * FQCN to a class that handles it as an array value. |
|
77 | - */ |
|
78 | - private $tagHandlerMappings = [ |
|
79 | - 'author' => Author::class, |
|
80 | - 'covers' => Covers::class, |
|
81 | - 'deprecated' => Deprecated::class, |
|
82 | - // 'example' => '\phpDocumentor\Reflection\DocBlock\Tags\Example', |
|
83 | - 'link' => LinkTag::class, |
|
84 | - 'method' => Method::class, |
|
85 | - 'param' => Param::class, |
|
86 | - 'property-read' => PropertyRead::class, |
|
87 | - 'property' => Property::class, |
|
88 | - 'property-write' => PropertyWrite::class, |
|
89 | - 'return' => Return_::class, |
|
90 | - 'see' => SeeTag::class, |
|
91 | - 'since' => Since::class, |
|
92 | - 'source' => Source::class, |
|
93 | - 'throw' => Throws::class, |
|
94 | - 'throws' => Throws::class, |
|
95 | - 'uses' => Uses::class, |
|
96 | - 'var' => Var_::class, |
|
97 | - 'version' => Version::class, |
|
98 | - ]; |
|
99 | - |
|
100 | - /** |
|
101 | - * @var array<class-string<Tag>> An array with a anotation s a key, and an |
|
102 | - * FQCN to a class that handles it as an array value. |
|
103 | - */ |
|
104 | - private $annotationMappings = []; |
|
105 | - |
|
106 | - /** |
|
107 | - * @var ReflectionParameter[][] a lazy-loading cache containing parameters |
|
108 | - * for each tagHandler that has been used. |
|
109 | - */ |
|
110 | - private $tagHandlerParameterCache = []; |
|
111 | - |
|
112 | - /** @var FqsenResolver */ |
|
113 | - private $fqsenResolver; |
|
114 | - |
|
115 | - /** |
|
116 | - * @var mixed[] an array representing a simple Service Locator where we can store parameters and |
|
117 | - * services that can be inserted into the Factory Methods of Tag Handlers. |
|
118 | - */ |
|
119 | - private $serviceLocator = []; |
|
120 | - |
|
121 | - /** |
|
122 | - * Initialize this tag factory with the means to resolve an FQSEN and optionally a list of tag handlers. |
|
123 | - * |
|
124 | - * If no tag handlers are provided than the default list in the {@see self::$tagHandlerMappings} property |
|
125 | - * is used. |
|
126 | - * |
|
127 | - * @see self::registerTagHandler() to add a new tag handler to the existing default list. |
|
128 | - * |
|
129 | - * @param array<class-string<Tag>> $tagHandlers |
|
130 | - */ |
|
131 | - public function __construct(FqsenResolver $fqsenResolver, ?array $tagHandlers = null) |
|
132 | - { |
|
133 | - $this->fqsenResolver = $fqsenResolver; |
|
134 | - if ($tagHandlers !== null) { |
|
135 | - $this->tagHandlerMappings = $tagHandlers; |
|
136 | - } |
|
137 | - |
|
138 | - $this->addService($fqsenResolver, FqsenResolver::class); |
|
139 | - } |
|
140 | - |
|
141 | - public function create(string $tagLine, ?TypeContext $context = null): Tag |
|
142 | - { |
|
143 | - if (!$context) { |
|
144 | - $context = new TypeContext(''); |
|
145 | - } |
|
146 | - |
|
147 | - [$tagName, $tagBody] = $this->extractTagParts($tagLine); |
|
148 | - |
|
149 | - return $this->createTag(trim($tagBody), $tagName, $context); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @param mixed $value |
|
154 | - */ |
|
155 | - public function addParameter(string $name, $value): void |
|
156 | - { |
|
157 | - $this->serviceLocator[$name] = $value; |
|
158 | - } |
|
159 | - |
|
160 | - public function addService(object $service, ?string $alias = null): void |
|
161 | - { |
|
162 | - $this->serviceLocator[$alias ?: get_class($service)] = $service; |
|
163 | - } |
|
164 | - |
|
165 | - public function registerTagHandler(string $tagName, string $handler): void |
|
166 | - { |
|
167 | - Assert::stringNotEmpty($tagName); |
|
168 | - Assert::classExists($handler); |
|
169 | - Assert::implementsInterface($handler, Tag::class); |
|
170 | - |
|
171 | - if (strpos($tagName, '\\') && $tagName[0] !== '\\') { |
|
172 | - throw new InvalidArgumentException( |
|
173 | - 'A namespaced tag must have a leading backslash as it must be fully qualified' |
|
174 | - ); |
|
175 | - } |
|
176 | - |
|
177 | - $this->tagHandlerMappings[$tagName] = $handler; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Extracts all components for a tag. |
|
182 | - * |
|
183 | - * @return string[] |
|
184 | - */ |
|
185 | - private function extractTagParts(string $tagLine): array |
|
186 | - { |
|
187 | - $matches = []; |
|
188 | - if (!preg_match('/^@(' . self::REGEX_TAGNAME . ')((?:[\s\(\{])\s*([^\s].*)|$)/us', $tagLine, $matches)) { |
|
189 | - throw new InvalidArgumentException( |
|
190 | - 'The tag "' . $tagLine . '" does not seem to be wellformed, please check it for errors' |
|
191 | - ); |
|
192 | - } |
|
193 | - |
|
194 | - if (count($matches) < 3) { |
|
195 | - $matches[] = ''; |
|
196 | - } |
|
197 | - |
|
198 | - return array_slice($matches, 1); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Creates a new tag object with the given name and body or returns null if the tag name was recognized but the |
|
203 | - * body was invalid. |
|
204 | - */ |
|
205 | - private function createTag(string $body, string $name, TypeContext $context): Tag |
|
206 | - { |
|
207 | - $handlerClassName = $this->findHandlerClassName($name, $context); |
|
208 | - $arguments = $this->getArgumentsForParametersFromWiring( |
|
209 | - $this->fetchParametersForHandlerFactoryMethod($handlerClassName), |
|
210 | - $this->getServiceLocatorWithDynamicParameters($context, $name, $body) |
|
211 | - ); |
|
212 | - |
|
213 | - try { |
|
214 | - $callable = [$handlerClassName, 'create']; |
|
215 | - Assert::isCallable($callable); |
|
216 | - /** @phpstan-var callable(string): ?Tag $callable */ |
|
217 | - $tag = call_user_func_array($callable, $arguments); |
|
218 | - |
|
219 | - return $tag ?? InvalidTag::create($body, $name); |
|
220 | - } catch (InvalidArgumentException $e) { |
|
221 | - return InvalidTag::create($body, $name)->withError($e); |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Determines the Fully Qualified Class Name of the Factory or Tag (containing a Factory Method `create`). |
|
227 | - * |
|
228 | - * @return class-string<Tag> |
|
229 | - */ |
|
230 | - private function findHandlerClassName(string $tagName, TypeContext $context): string |
|
231 | - { |
|
232 | - $handlerClassName = Generic::class; |
|
233 | - if (isset($this->tagHandlerMappings[$tagName])) { |
|
234 | - $handlerClassName = $this->tagHandlerMappings[$tagName]; |
|
235 | - } elseif ($this->isAnnotation($tagName)) { |
|
236 | - // TODO: Annotation support is planned for a later stage and as such is disabled for now |
|
237 | - $tagName = (string) $this->fqsenResolver->resolve($tagName, $context); |
|
238 | - if (isset($this->annotationMappings[$tagName])) { |
|
239 | - $handlerClassName = $this->annotationMappings[$tagName]; |
|
240 | - } |
|
241 | - } |
|
242 | - |
|
243 | - return $handlerClassName; |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Retrieves the arguments that need to be passed to the Factory Method with the given Parameters. |
|
248 | - * |
|
249 | - * @param ReflectionParameter[] $parameters |
|
250 | - * @param mixed[] $locator |
|
251 | - * |
|
252 | - * @return mixed[] A series of values that can be passed to the Factory Method of the tag whose parameters |
|
253 | - * is provided with this method. |
|
254 | - */ |
|
255 | - private function getArgumentsForParametersFromWiring(array $parameters, array $locator): array |
|
256 | - { |
|
257 | - $arguments = []; |
|
258 | - foreach ($parameters as $parameter) { |
|
259 | - $type = $parameter->getType(); |
|
260 | - $typeHint = null; |
|
261 | - if ($type instanceof ReflectionNamedType) { |
|
262 | - $typeHint = $type->getName(); |
|
263 | - if ($typeHint === 'self') { |
|
264 | - $declaringClass = $parameter->getDeclaringClass(); |
|
265 | - if ($declaringClass !== null) { |
|
266 | - $typeHint = $declaringClass->getName(); |
|
267 | - } |
|
268 | - } |
|
269 | - } |
|
270 | - |
|
271 | - if (isset($locator[$typeHint])) { |
|
272 | - $arguments[] = $locator[$typeHint]; |
|
273 | - continue; |
|
274 | - } |
|
275 | - |
|
276 | - $parameterName = $parameter->getName(); |
|
277 | - if (isset($locator[$parameterName])) { |
|
278 | - $arguments[] = $locator[$parameterName]; |
|
279 | - continue; |
|
280 | - } |
|
281 | - |
|
282 | - $arguments[] = null; |
|
283 | - } |
|
284 | - |
|
285 | - return $arguments; |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Retrieves a series of ReflectionParameter objects for the static 'create' method of the given |
|
290 | - * tag handler class name. |
|
291 | - * |
|
292 | - * @param class-string $handlerClassName |
|
293 | - * |
|
294 | - * @return ReflectionParameter[] |
|
295 | - */ |
|
296 | - private function fetchParametersForHandlerFactoryMethod(string $handlerClassName): array |
|
297 | - { |
|
298 | - if (!isset($this->tagHandlerParameterCache[$handlerClassName])) { |
|
299 | - $methodReflection = new ReflectionMethod($handlerClassName, 'create'); |
|
300 | - $this->tagHandlerParameterCache[$handlerClassName] = $methodReflection->getParameters(); |
|
301 | - } |
|
302 | - |
|
303 | - return $this->tagHandlerParameterCache[$handlerClassName]; |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Returns a copy of this class' Service Locator with added dynamic parameters, |
|
308 | - * such as the tag's name, body and Context. |
|
309 | - * |
|
310 | - * @param TypeContext $context The Context (namespace and aliasses) that may be |
|
311 | - * passed and is used to resolve FQSENs. |
|
312 | - * @param string $tagName The name of the tag that may be |
|
313 | - * passed onto the factory method of the Tag class. |
|
314 | - * @param string $tagBody The body of the tag that may be |
|
315 | - * passed onto the factory method of the Tag class. |
|
316 | - * |
|
317 | - * @return mixed[] |
|
318 | - */ |
|
319 | - private function getServiceLocatorWithDynamicParameters( |
|
320 | - TypeContext $context, |
|
321 | - string $tagName, |
|
322 | - string $tagBody |
|
323 | - ): array { |
|
324 | - return array_merge( |
|
325 | - $this->serviceLocator, |
|
326 | - [ |
|
327 | - 'name' => $tagName, |
|
328 | - 'body' => $tagBody, |
|
329 | - TypeContext::class => $context, |
|
330 | - ] |
|
331 | - ); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Returns whether the given tag belongs to an annotation. |
|
336 | - * |
|
337 | - * @todo this method should be populated once we implement Annotation notation support. |
|
338 | - */ |
|
339 | - private function isAnnotation(string $tagContent): bool |
|
340 | - { |
|
341 | - // 1. Contains a namespace separator |
|
342 | - // 2. Contains parenthesis |
|
343 | - // 3. Is present in a list of known annotations (make the algorithm smart by first checking is the last part |
|
344 | - // of the annotation class name matches the found tag name |
|
345 | - |
|
346 | - return false; |
|
347 | - } |
|
71 | + /** PCRE regular expression matching a tag name. */ |
|
72 | + public const REGEX_TAGNAME = '[\w\-\_\\\\:]+'; |
|
73 | + |
|
74 | + /** |
|
75 | + * @var array<class-string<Tag>> An array with a tag as a key, and an |
|
76 | + * FQCN to a class that handles it as an array value. |
|
77 | + */ |
|
78 | + private $tagHandlerMappings = [ |
|
79 | + 'author' => Author::class, |
|
80 | + 'covers' => Covers::class, |
|
81 | + 'deprecated' => Deprecated::class, |
|
82 | + // 'example' => '\phpDocumentor\Reflection\DocBlock\Tags\Example', |
|
83 | + 'link' => LinkTag::class, |
|
84 | + 'method' => Method::class, |
|
85 | + 'param' => Param::class, |
|
86 | + 'property-read' => PropertyRead::class, |
|
87 | + 'property' => Property::class, |
|
88 | + 'property-write' => PropertyWrite::class, |
|
89 | + 'return' => Return_::class, |
|
90 | + 'see' => SeeTag::class, |
|
91 | + 'since' => Since::class, |
|
92 | + 'source' => Source::class, |
|
93 | + 'throw' => Throws::class, |
|
94 | + 'throws' => Throws::class, |
|
95 | + 'uses' => Uses::class, |
|
96 | + 'var' => Var_::class, |
|
97 | + 'version' => Version::class, |
|
98 | + ]; |
|
99 | + |
|
100 | + /** |
|
101 | + * @var array<class-string<Tag>> An array with a anotation s a key, and an |
|
102 | + * FQCN to a class that handles it as an array value. |
|
103 | + */ |
|
104 | + private $annotationMappings = []; |
|
105 | + |
|
106 | + /** |
|
107 | + * @var ReflectionParameter[][] a lazy-loading cache containing parameters |
|
108 | + * for each tagHandler that has been used. |
|
109 | + */ |
|
110 | + private $tagHandlerParameterCache = []; |
|
111 | + |
|
112 | + /** @var FqsenResolver */ |
|
113 | + private $fqsenResolver; |
|
114 | + |
|
115 | + /** |
|
116 | + * @var mixed[] an array representing a simple Service Locator where we can store parameters and |
|
117 | + * services that can be inserted into the Factory Methods of Tag Handlers. |
|
118 | + */ |
|
119 | + private $serviceLocator = []; |
|
120 | + |
|
121 | + /** |
|
122 | + * Initialize this tag factory with the means to resolve an FQSEN and optionally a list of tag handlers. |
|
123 | + * |
|
124 | + * If no tag handlers are provided than the default list in the {@see self::$tagHandlerMappings} property |
|
125 | + * is used. |
|
126 | + * |
|
127 | + * @see self::registerTagHandler() to add a new tag handler to the existing default list. |
|
128 | + * |
|
129 | + * @param array<class-string<Tag>> $tagHandlers |
|
130 | + */ |
|
131 | + public function __construct(FqsenResolver $fqsenResolver, ?array $tagHandlers = null) |
|
132 | + { |
|
133 | + $this->fqsenResolver = $fqsenResolver; |
|
134 | + if ($tagHandlers !== null) { |
|
135 | + $this->tagHandlerMappings = $tagHandlers; |
|
136 | + } |
|
137 | + |
|
138 | + $this->addService($fqsenResolver, FqsenResolver::class); |
|
139 | + } |
|
140 | + |
|
141 | + public function create(string $tagLine, ?TypeContext $context = null): Tag |
|
142 | + { |
|
143 | + if (!$context) { |
|
144 | + $context = new TypeContext(''); |
|
145 | + } |
|
146 | + |
|
147 | + [$tagName, $tagBody] = $this->extractTagParts($tagLine); |
|
148 | + |
|
149 | + return $this->createTag(trim($tagBody), $tagName, $context); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @param mixed $value |
|
154 | + */ |
|
155 | + public function addParameter(string $name, $value): void |
|
156 | + { |
|
157 | + $this->serviceLocator[$name] = $value; |
|
158 | + } |
|
159 | + |
|
160 | + public function addService(object $service, ?string $alias = null): void |
|
161 | + { |
|
162 | + $this->serviceLocator[$alias ?: get_class($service)] = $service; |
|
163 | + } |
|
164 | + |
|
165 | + public function registerTagHandler(string $tagName, string $handler): void |
|
166 | + { |
|
167 | + Assert::stringNotEmpty($tagName); |
|
168 | + Assert::classExists($handler); |
|
169 | + Assert::implementsInterface($handler, Tag::class); |
|
170 | + |
|
171 | + if (strpos($tagName, '\\') && $tagName[0] !== '\\') { |
|
172 | + throw new InvalidArgumentException( |
|
173 | + 'A namespaced tag must have a leading backslash as it must be fully qualified' |
|
174 | + ); |
|
175 | + } |
|
176 | + |
|
177 | + $this->tagHandlerMappings[$tagName] = $handler; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Extracts all components for a tag. |
|
182 | + * |
|
183 | + * @return string[] |
|
184 | + */ |
|
185 | + private function extractTagParts(string $tagLine): array |
|
186 | + { |
|
187 | + $matches = []; |
|
188 | + if (!preg_match('/^@(' . self::REGEX_TAGNAME . ')((?:[\s\(\{])\s*([^\s].*)|$)/us', $tagLine, $matches)) { |
|
189 | + throw new InvalidArgumentException( |
|
190 | + 'The tag "' . $tagLine . '" does not seem to be wellformed, please check it for errors' |
|
191 | + ); |
|
192 | + } |
|
193 | + |
|
194 | + if (count($matches) < 3) { |
|
195 | + $matches[] = ''; |
|
196 | + } |
|
197 | + |
|
198 | + return array_slice($matches, 1); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Creates a new tag object with the given name and body or returns null if the tag name was recognized but the |
|
203 | + * body was invalid. |
|
204 | + */ |
|
205 | + private function createTag(string $body, string $name, TypeContext $context): Tag |
|
206 | + { |
|
207 | + $handlerClassName = $this->findHandlerClassName($name, $context); |
|
208 | + $arguments = $this->getArgumentsForParametersFromWiring( |
|
209 | + $this->fetchParametersForHandlerFactoryMethod($handlerClassName), |
|
210 | + $this->getServiceLocatorWithDynamicParameters($context, $name, $body) |
|
211 | + ); |
|
212 | + |
|
213 | + try { |
|
214 | + $callable = [$handlerClassName, 'create']; |
|
215 | + Assert::isCallable($callable); |
|
216 | + /** @phpstan-var callable(string): ?Tag $callable */ |
|
217 | + $tag = call_user_func_array($callable, $arguments); |
|
218 | + |
|
219 | + return $tag ?? InvalidTag::create($body, $name); |
|
220 | + } catch (InvalidArgumentException $e) { |
|
221 | + return InvalidTag::create($body, $name)->withError($e); |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Determines the Fully Qualified Class Name of the Factory or Tag (containing a Factory Method `create`). |
|
227 | + * |
|
228 | + * @return class-string<Tag> |
|
229 | + */ |
|
230 | + private function findHandlerClassName(string $tagName, TypeContext $context): string |
|
231 | + { |
|
232 | + $handlerClassName = Generic::class; |
|
233 | + if (isset($this->tagHandlerMappings[$tagName])) { |
|
234 | + $handlerClassName = $this->tagHandlerMappings[$tagName]; |
|
235 | + } elseif ($this->isAnnotation($tagName)) { |
|
236 | + // TODO: Annotation support is planned for a later stage and as such is disabled for now |
|
237 | + $tagName = (string) $this->fqsenResolver->resolve($tagName, $context); |
|
238 | + if (isset($this->annotationMappings[$tagName])) { |
|
239 | + $handlerClassName = $this->annotationMappings[$tagName]; |
|
240 | + } |
|
241 | + } |
|
242 | + |
|
243 | + return $handlerClassName; |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Retrieves the arguments that need to be passed to the Factory Method with the given Parameters. |
|
248 | + * |
|
249 | + * @param ReflectionParameter[] $parameters |
|
250 | + * @param mixed[] $locator |
|
251 | + * |
|
252 | + * @return mixed[] A series of values that can be passed to the Factory Method of the tag whose parameters |
|
253 | + * is provided with this method. |
|
254 | + */ |
|
255 | + private function getArgumentsForParametersFromWiring(array $parameters, array $locator): array |
|
256 | + { |
|
257 | + $arguments = []; |
|
258 | + foreach ($parameters as $parameter) { |
|
259 | + $type = $parameter->getType(); |
|
260 | + $typeHint = null; |
|
261 | + if ($type instanceof ReflectionNamedType) { |
|
262 | + $typeHint = $type->getName(); |
|
263 | + if ($typeHint === 'self') { |
|
264 | + $declaringClass = $parameter->getDeclaringClass(); |
|
265 | + if ($declaringClass !== null) { |
|
266 | + $typeHint = $declaringClass->getName(); |
|
267 | + } |
|
268 | + } |
|
269 | + } |
|
270 | + |
|
271 | + if (isset($locator[$typeHint])) { |
|
272 | + $arguments[] = $locator[$typeHint]; |
|
273 | + continue; |
|
274 | + } |
|
275 | + |
|
276 | + $parameterName = $parameter->getName(); |
|
277 | + if (isset($locator[$parameterName])) { |
|
278 | + $arguments[] = $locator[$parameterName]; |
|
279 | + continue; |
|
280 | + } |
|
281 | + |
|
282 | + $arguments[] = null; |
|
283 | + } |
|
284 | + |
|
285 | + return $arguments; |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Retrieves a series of ReflectionParameter objects for the static 'create' method of the given |
|
290 | + * tag handler class name. |
|
291 | + * |
|
292 | + * @param class-string $handlerClassName |
|
293 | + * |
|
294 | + * @return ReflectionParameter[] |
|
295 | + */ |
|
296 | + private function fetchParametersForHandlerFactoryMethod(string $handlerClassName): array |
|
297 | + { |
|
298 | + if (!isset($this->tagHandlerParameterCache[$handlerClassName])) { |
|
299 | + $methodReflection = new ReflectionMethod($handlerClassName, 'create'); |
|
300 | + $this->tagHandlerParameterCache[$handlerClassName] = $methodReflection->getParameters(); |
|
301 | + } |
|
302 | + |
|
303 | + return $this->tagHandlerParameterCache[$handlerClassName]; |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Returns a copy of this class' Service Locator with added dynamic parameters, |
|
308 | + * such as the tag's name, body and Context. |
|
309 | + * |
|
310 | + * @param TypeContext $context The Context (namespace and aliasses) that may be |
|
311 | + * passed and is used to resolve FQSENs. |
|
312 | + * @param string $tagName The name of the tag that may be |
|
313 | + * passed onto the factory method of the Tag class. |
|
314 | + * @param string $tagBody The body of the tag that may be |
|
315 | + * passed onto the factory method of the Tag class. |
|
316 | + * |
|
317 | + * @return mixed[] |
|
318 | + */ |
|
319 | + private function getServiceLocatorWithDynamicParameters( |
|
320 | + TypeContext $context, |
|
321 | + string $tagName, |
|
322 | + string $tagBody |
|
323 | + ): array { |
|
324 | + return array_merge( |
|
325 | + $this->serviceLocator, |
|
326 | + [ |
|
327 | + 'name' => $tagName, |
|
328 | + 'body' => $tagBody, |
|
329 | + TypeContext::class => $context, |
|
330 | + ] |
|
331 | + ); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Returns whether the given tag belongs to an annotation. |
|
336 | + * |
|
337 | + * @todo this method should be populated once we implement Annotation notation support. |
|
338 | + */ |
|
339 | + private function isAnnotation(string $tagContent): bool |
|
340 | + { |
|
341 | + // 1. Contains a namespace separator |
|
342 | + // 2. Contains parenthesis |
|
343 | + // 3. Is present in a list of known annotations (make the algorithm smart by first checking is the last part |
|
344 | + // of the annotation class name matches the found tag name |
|
345 | + |
|
346 | + return false; |
|
347 | + } |
|
348 | 348 | } |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | |
141 | 141 | public function create(string $tagLine, ?TypeContext $context = null): Tag |
142 | 142 | { |
143 | - if (!$context) { |
|
143 | + if ( ! $context) { |
|
144 | 144 | $context = new TypeContext(''); |
145 | 145 | } |
146 | 146 | |
@@ -185,9 +185,9 @@ discard block |
||
185 | 185 | private function extractTagParts(string $tagLine): array |
186 | 186 | { |
187 | 187 | $matches = []; |
188 | - if (!preg_match('/^@(' . self::REGEX_TAGNAME . ')((?:[\s\(\{])\s*([^\s].*)|$)/us', $tagLine, $matches)) { |
|
188 | + if ( ! preg_match('/^@('.self::REGEX_TAGNAME.')((?:[\s\(\{])\s*([^\s].*)|$)/us', $tagLine, $matches)) { |
|
189 | 189 | throw new InvalidArgumentException( |
190 | - 'The tag "' . $tagLine . '" does not seem to be wellformed, please check it for errors' |
|
190 | + 'The tag "'.$tagLine.'" does not seem to be wellformed, please check it for errors' |
|
191 | 191 | ); |
192 | 192 | } |
193 | 193 | |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | */ |
296 | 296 | private function fetchParametersForHandlerFactoryMethod(string $handlerClassName): array |
297 | 297 | { |
298 | - if (!isset($this->tagHandlerParameterCache[$handlerClassName])) { |
|
298 | + if ( ! isset($this->tagHandlerParameterCache[$handlerClassName])) { |
|
299 | 299 | $methodReflection = new ReflectionMethod($handlerClassName, 'create'); |
300 | 300 | $this->tagHandlerParameterCache[$handlerClassName] = $methodReflection->getParameters(); |
301 | 301 | } |
@@ -28,130 +28,130 @@ |
||
28 | 28 | */ |
29 | 29 | class Serializer |
30 | 30 | { |
31 | - /** @var string The string to indent the comment with. */ |
|
32 | - protected $indentString = ' '; |
|
33 | - |
|
34 | - /** @var int The number of times the indent string is repeated. */ |
|
35 | - protected $indent = 0; |
|
36 | - |
|
37 | - /** @var bool Whether to indent the first line with the given indent amount and string. */ |
|
38 | - protected $isFirstLineIndented = true; |
|
39 | - |
|
40 | - /** @var int|null The max length of a line. */ |
|
41 | - protected $lineLength; |
|
42 | - |
|
43 | - /** @var Formatter A custom tag formatter. */ |
|
44 | - protected $tagFormatter; |
|
45 | - /** @var string */ |
|
46 | - private $lineEnding; |
|
47 | - |
|
48 | - /** |
|
49 | - * Create a Serializer instance. |
|
50 | - * |
|
51 | - * @param int $indent The number of times the indent string is repeated. |
|
52 | - * @param string $indentString The string to indent the comment with. |
|
53 | - * @param bool $indentFirstLine Whether to indent the first line. |
|
54 | - * @param int|null $lineLength The max length of a line or NULL to disable line wrapping. |
|
55 | - * @param Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter. |
|
56 | - * @param string $lineEnding Line ending used in the output, by default \n is used. |
|
57 | - */ |
|
58 | - public function __construct( |
|
59 | - int $indent = 0, |
|
60 | - string $indentString = ' ', |
|
61 | - bool $indentFirstLine = true, |
|
62 | - ?int $lineLength = null, |
|
63 | - ?Formatter $tagFormatter = null, |
|
64 | - string $lineEnding = "\n" |
|
65 | - ) { |
|
66 | - $this->indent = $indent; |
|
67 | - $this->indentString = $indentString; |
|
68 | - $this->isFirstLineIndented = $indentFirstLine; |
|
69 | - $this->lineLength = $lineLength; |
|
70 | - $this->tagFormatter = $tagFormatter ?: new PassthroughFormatter(); |
|
71 | - $this->lineEnding = $lineEnding; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Generate a DocBlock comment. |
|
76 | - * |
|
77 | - * @param DocBlock $docblock The DocBlock to serialize. |
|
78 | - * |
|
79 | - * @return string The serialized doc block. |
|
80 | - */ |
|
81 | - public function getDocComment(DocBlock $docblock): string |
|
82 | - { |
|
83 | - $indent = str_repeat($this->indentString, $this->indent); |
|
84 | - $firstIndent = $this->isFirstLineIndented ? $indent : ''; |
|
85 | - // 3 === strlen(' * ') |
|
86 | - $wrapLength = $this->lineLength ? $this->lineLength - strlen($indent) - 3 : null; |
|
87 | - |
|
88 | - $text = $this->removeTrailingSpaces( |
|
89 | - $indent, |
|
90 | - $this->addAsterisksForEachLine( |
|
91 | - $indent, |
|
92 | - $this->getSummaryAndDescriptionTextBlock($docblock, $wrapLength) |
|
93 | - ) |
|
94 | - ); |
|
95 | - |
|
96 | - $comment = $firstIndent . "/**\n"; |
|
97 | - if ($text) { |
|
98 | - $comment .= $indent . ' * ' . $text . "\n"; |
|
99 | - $comment .= $indent . " *\n"; |
|
100 | - } |
|
101 | - |
|
102 | - $comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment); |
|
103 | - |
|
104 | - return str_replace("\n", $this->lineEnding, $comment . $indent . ' */'); |
|
105 | - } |
|
106 | - |
|
107 | - private function removeTrailingSpaces(string $indent, string $text): string |
|
108 | - { |
|
109 | - return str_replace( |
|
110 | - sprintf("\n%s * \n", $indent), |
|
111 | - sprintf("\n%s *\n", $indent), |
|
112 | - $text |
|
113 | - ); |
|
114 | - } |
|
115 | - |
|
116 | - private function addAsterisksForEachLine(string $indent, string $text): string |
|
117 | - { |
|
118 | - return str_replace( |
|
119 | - "\n", |
|
120 | - sprintf("\n%s * ", $indent), |
|
121 | - $text |
|
122 | - ); |
|
123 | - } |
|
124 | - |
|
125 | - private function getSummaryAndDescriptionTextBlock(DocBlock $docblock, ?int $wrapLength): string |
|
126 | - { |
|
127 | - $text = $docblock->getSummary() . ((string) $docblock->getDescription() ? "\n\n" . $docblock->getDescription() |
|
128 | - : ''); |
|
129 | - if ($wrapLength !== null) { |
|
130 | - $text = wordwrap($text, $wrapLength); |
|
131 | - |
|
132 | - return $text; |
|
133 | - } |
|
134 | - |
|
135 | - return $text; |
|
136 | - } |
|
137 | - |
|
138 | - private function addTagBlock(DocBlock $docblock, ?int $wrapLength, string $indent, string $comment): string |
|
139 | - { |
|
140 | - foreach ($docblock->getTags() as $tag) { |
|
141 | - $tagText = $this->tagFormatter->format($tag); |
|
142 | - if ($wrapLength !== null) { |
|
143 | - $tagText = wordwrap($tagText, $wrapLength); |
|
144 | - } |
|
145 | - |
|
146 | - $tagText = str_replace( |
|
147 | - "\n", |
|
148 | - sprintf("\n%s * ", $indent), |
|
149 | - $tagText |
|
150 | - ); |
|
151 | - |
|
152 | - $comment .= sprintf("%s * %s\n", $indent, $tagText); |
|
153 | - } |
|
154 | - |
|
155 | - return $comment; |
|
156 | - } |
|
31 | + /** @var string The string to indent the comment with. */ |
|
32 | + protected $indentString = ' '; |
|
33 | + |
|
34 | + /** @var int The number of times the indent string is repeated. */ |
|
35 | + protected $indent = 0; |
|
36 | + |
|
37 | + /** @var bool Whether to indent the first line with the given indent amount and string. */ |
|
38 | + protected $isFirstLineIndented = true; |
|
39 | + |
|
40 | + /** @var int|null The max length of a line. */ |
|
41 | + protected $lineLength; |
|
42 | + |
|
43 | + /** @var Formatter A custom tag formatter. */ |
|
44 | + protected $tagFormatter; |
|
45 | + /** @var string */ |
|
46 | + private $lineEnding; |
|
47 | + |
|
48 | + /** |
|
49 | + * Create a Serializer instance. |
|
50 | + * |
|
51 | + * @param int $indent The number of times the indent string is repeated. |
|
52 | + * @param string $indentString The string to indent the comment with. |
|
53 | + * @param bool $indentFirstLine Whether to indent the first line. |
|
54 | + * @param int|null $lineLength The max length of a line or NULL to disable line wrapping. |
|
55 | + * @param Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter. |
|
56 | + * @param string $lineEnding Line ending used in the output, by default \n is used. |
|
57 | + */ |
|
58 | + public function __construct( |
|
59 | + int $indent = 0, |
|
60 | + string $indentString = ' ', |
|
61 | + bool $indentFirstLine = true, |
|
62 | + ?int $lineLength = null, |
|
63 | + ?Formatter $tagFormatter = null, |
|
64 | + string $lineEnding = "\n" |
|
65 | + ) { |
|
66 | + $this->indent = $indent; |
|
67 | + $this->indentString = $indentString; |
|
68 | + $this->isFirstLineIndented = $indentFirstLine; |
|
69 | + $this->lineLength = $lineLength; |
|
70 | + $this->tagFormatter = $tagFormatter ?: new PassthroughFormatter(); |
|
71 | + $this->lineEnding = $lineEnding; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Generate a DocBlock comment. |
|
76 | + * |
|
77 | + * @param DocBlock $docblock The DocBlock to serialize. |
|
78 | + * |
|
79 | + * @return string The serialized doc block. |
|
80 | + */ |
|
81 | + public function getDocComment(DocBlock $docblock): string |
|
82 | + { |
|
83 | + $indent = str_repeat($this->indentString, $this->indent); |
|
84 | + $firstIndent = $this->isFirstLineIndented ? $indent : ''; |
|
85 | + // 3 === strlen(' * ') |
|
86 | + $wrapLength = $this->lineLength ? $this->lineLength - strlen($indent) - 3 : null; |
|
87 | + |
|
88 | + $text = $this->removeTrailingSpaces( |
|
89 | + $indent, |
|
90 | + $this->addAsterisksForEachLine( |
|
91 | + $indent, |
|
92 | + $this->getSummaryAndDescriptionTextBlock($docblock, $wrapLength) |
|
93 | + ) |
|
94 | + ); |
|
95 | + |
|
96 | + $comment = $firstIndent . "/**\n"; |
|
97 | + if ($text) { |
|
98 | + $comment .= $indent . ' * ' . $text . "\n"; |
|
99 | + $comment .= $indent . " *\n"; |
|
100 | + } |
|
101 | + |
|
102 | + $comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment); |
|
103 | + |
|
104 | + return str_replace("\n", $this->lineEnding, $comment . $indent . ' */'); |
|
105 | + } |
|
106 | + |
|
107 | + private function removeTrailingSpaces(string $indent, string $text): string |
|
108 | + { |
|
109 | + return str_replace( |
|
110 | + sprintf("\n%s * \n", $indent), |
|
111 | + sprintf("\n%s *\n", $indent), |
|
112 | + $text |
|
113 | + ); |
|
114 | + } |
|
115 | + |
|
116 | + private function addAsterisksForEachLine(string $indent, string $text): string |
|
117 | + { |
|
118 | + return str_replace( |
|
119 | + "\n", |
|
120 | + sprintf("\n%s * ", $indent), |
|
121 | + $text |
|
122 | + ); |
|
123 | + } |
|
124 | + |
|
125 | + private function getSummaryAndDescriptionTextBlock(DocBlock $docblock, ?int $wrapLength): string |
|
126 | + { |
|
127 | + $text = $docblock->getSummary() . ((string) $docblock->getDescription() ? "\n\n" . $docblock->getDescription() |
|
128 | + : ''); |
|
129 | + if ($wrapLength !== null) { |
|
130 | + $text = wordwrap($text, $wrapLength); |
|
131 | + |
|
132 | + return $text; |
|
133 | + } |
|
134 | + |
|
135 | + return $text; |
|
136 | + } |
|
137 | + |
|
138 | + private function addTagBlock(DocBlock $docblock, ?int $wrapLength, string $indent, string $comment): string |
|
139 | + { |
|
140 | + foreach ($docblock->getTags() as $tag) { |
|
141 | + $tagText = $this->tagFormatter->format($tag); |
|
142 | + if ($wrapLength !== null) { |
|
143 | + $tagText = wordwrap($tagText, $wrapLength); |
|
144 | + } |
|
145 | + |
|
146 | + $tagText = str_replace( |
|
147 | + "\n", |
|
148 | + sprintf("\n%s * ", $indent), |
|
149 | + $tagText |
|
150 | + ); |
|
151 | + |
|
152 | + $comment .= sprintf("%s * %s\n", $indent, $tagText); |
|
153 | + } |
|
154 | + |
|
155 | + return $comment; |
|
156 | + } |
|
157 | 157 | } |
@@ -93,15 +93,15 @@ discard block |
||
93 | 93 | ) |
94 | 94 | ); |
95 | 95 | |
96 | - $comment = $firstIndent . "/**\n"; |
|
96 | + $comment = $firstIndent."/**\n"; |
|
97 | 97 | if ($text) { |
98 | - $comment .= $indent . ' * ' . $text . "\n"; |
|
99 | - $comment .= $indent . " *\n"; |
|
98 | + $comment .= $indent.' * '.$text."\n"; |
|
99 | + $comment .= $indent." *\n"; |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | $comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment); |
103 | 103 | |
104 | - return str_replace("\n", $this->lineEnding, $comment . $indent . ' */'); |
|
104 | + return str_replace("\n", $this->lineEnding, $comment.$indent.' */'); |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | private function removeTrailingSpaces(string $indent, string $text): string |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | |
125 | 125 | private function getSummaryAndDescriptionTextBlock(DocBlock $docblock, ?int $wrapLength): string |
126 | 126 | { |
127 | - $text = $docblock->getSummary() . ((string) $docblock->getDescription() ? "\n\n" . $docblock->getDescription() |
|
127 | + $text = $docblock->getSummary().((string) $docblock->getDescription() ? "\n\n".$docblock->getDescription() |
|
128 | 128 | : ''); |
129 | 129 | if ($wrapLength !== null) { |
130 | 130 | $text = wordwrap($text, $wrapLength); |
@@ -27,63 +27,63 @@ |
||
27 | 27 | */ |
28 | 28 | final class Generic extends BaseTag implements Factory\StaticMethod |
29 | 29 | { |
30 | - /** |
|
31 | - * Parses a tag and populates the member variables. |
|
32 | - * |
|
33 | - * @param string $name Name of the tag. |
|
34 | - * @param Description $description The contents of the given tag. |
|
35 | - */ |
|
36 | - public function __construct(string $name, ?Description $description = null) |
|
37 | - { |
|
38 | - $this->validateTagName($name); |
|
30 | + /** |
|
31 | + * Parses a tag and populates the member variables. |
|
32 | + * |
|
33 | + * @param string $name Name of the tag. |
|
34 | + * @param Description $description The contents of the given tag. |
|
35 | + */ |
|
36 | + public function __construct(string $name, ?Description $description = null) |
|
37 | + { |
|
38 | + $this->validateTagName($name); |
|
39 | 39 | |
40 | - $this->name = $name; |
|
41 | - $this->description = $description; |
|
42 | - } |
|
40 | + $this->name = $name; |
|
41 | + $this->description = $description; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Creates a new tag that represents any unknown tag type. |
|
46 | - * |
|
47 | - * @return static |
|
48 | - */ |
|
49 | - public static function create( |
|
50 | - string $body, |
|
51 | - string $name = '', |
|
52 | - ?DescriptionFactory $descriptionFactory = null, |
|
53 | - ?TypeContext $context = null |
|
54 | - ): self { |
|
55 | - Assert::stringNotEmpty($name); |
|
56 | - Assert::notNull($descriptionFactory); |
|
44 | + /** |
|
45 | + * Creates a new tag that represents any unknown tag type. |
|
46 | + * |
|
47 | + * @return static |
|
48 | + */ |
|
49 | + public static function create( |
|
50 | + string $body, |
|
51 | + string $name = '', |
|
52 | + ?DescriptionFactory $descriptionFactory = null, |
|
53 | + ?TypeContext $context = null |
|
54 | + ): self { |
|
55 | + Assert::stringNotEmpty($name); |
|
56 | + Assert::notNull($descriptionFactory); |
|
57 | 57 | |
58 | - $description = $body !== '' ? $descriptionFactory->create($body, $context) : null; |
|
58 | + $description = $body !== '' ? $descriptionFactory->create($body, $context) : null; |
|
59 | 59 | |
60 | - return new static($name, $description); |
|
61 | - } |
|
60 | + return new static($name, $description); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Returns the tag as a serialized string |
|
65 | - */ |
|
66 | - public function __toString(): string |
|
67 | - { |
|
68 | - if ($this->description) { |
|
69 | - $description = $this->description->render(); |
|
70 | - } else { |
|
71 | - $description = ''; |
|
72 | - } |
|
63 | + /** |
|
64 | + * Returns the tag as a serialized string |
|
65 | + */ |
|
66 | + public function __toString(): string |
|
67 | + { |
|
68 | + if ($this->description) { |
|
69 | + $description = $this->description->render(); |
|
70 | + } else { |
|
71 | + $description = ''; |
|
72 | + } |
|
73 | 73 | |
74 | - return $description; |
|
75 | - } |
|
74 | + return $description; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Validates if the tag name matches the expected format, otherwise throws an exception. |
|
79 | - */ |
|
80 | - private function validateTagName(string $name): void |
|
81 | - { |
|
82 | - if (!preg_match('/^' . StandardTagFactory::REGEX_TAGNAME . '$/u', $name)) { |
|
83 | - throw new InvalidArgumentException( |
|
84 | - 'The tag name "' . $name . '" is not wellformed. Tags may only consist of letters, underscores, ' |
|
85 | - . 'hyphens and backslashes.' |
|
86 | - ); |
|
87 | - } |
|
88 | - } |
|
77 | + /** |
|
78 | + * Validates if the tag name matches the expected format, otherwise throws an exception. |
|
79 | + */ |
|
80 | + private function validateTagName(string $name): void |
|
81 | + { |
|
82 | + if (!preg_match('/^' . StandardTagFactory::REGEX_TAGNAME . '$/u', $name)) { |
|
83 | + throw new InvalidArgumentException( |
|
84 | + 'The tag name "' . $name . '" is not wellformed. Tags may only consist of letters, underscores, ' |
|
85 | + . 'hyphens and backslashes.' |
|
86 | + ); |
|
87 | + } |
|
88 | + } |
|
89 | 89 | } |
@@ -79,9 +79,9 @@ |
||
79 | 79 | */ |
80 | 80 | private function validateTagName(string $name): void |
81 | 81 | { |
82 | - if (!preg_match('/^' . StandardTagFactory::REGEX_TAGNAME . '$/u', $name)) { |
|
82 | + if ( ! preg_match('/^'.StandardTagFactory::REGEX_TAGNAME.'$/u', $name)) { |
|
83 | 83 | throw new InvalidArgumentException( |
84 | - 'The tag name "' . $name . '" is not wellformed. Tags may only consist of letters, underscores, ' |
|
84 | + 'The tag name "'.$name.'" is not wellformed. Tags may only consist of letters, underscores, ' |
|
85 | 85 | . 'hyphens and backslashes.' |
86 | 86 | ); |
87 | 87 | } |
@@ -26,77 +26,77 @@ |
||
26 | 26 | */ |
27 | 27 | final class Author extends BaseTag implements Factory\StaticMethod |
28 | 28 | { |
29 | - /** @var string register that this is the author tag. */ |
|
30 | - protected $name = 'author'; |
|
31 | - |
|
32 | - /** @var string The name of the author */ |
|
33 | - private $authorName; |
|
34 | - |
|
35 | - /** @var string The email of the author */ |
|
36 | - private $authorEmail; |
|
37 | - |
|
38 | - /** |
|
39 | - * Initializes this tag with the author name and e-mail. |
|
40 | - */ |
|
41 | - public function __construct(string $authorName, string $authorEmail) |
|
42 | - { |
|
43 | - if ($authorEmail && !filter_var($authorEmail, FILTER_VALIDATE_EMAIL)) { |
|
44 | - throw new InvalidArgumentException('The author tag does not have a valid e-mail address'); |
|
45 | - } |
|
46 | - |
|
47 | - $this->authorName = $authorName; |
|
48 | - $this->authorEmail = $authorEmail; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Gets the author's name. |
|
53 | - * |
|
54 | - * @return string The author's name. |
|
55 | - */ |
|
56 | - public function getAuthorName(): string |
|
57 | - { |
|
58 | - return $this->authorName; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Returns the author's email. |
|
63 | - * |
|
64 | - * @return string The author's email. |
|
65 | - */ |
|
66 | - public function getEmail(): string |
|
67 | - { |
|
68 | - return $this->authorEmail; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Returns this tag in string form. |
|
73 | - */ |
|
74 | - public function __toString(): string |
|
75 | - { |
|
76 | - if ($this->authorEmail) { |
|
77 | - $authorEmail = '<' . $this->authorEmail . '>'; |
|
78 | - } else { |
|
79 | - $authorEmail = ''; |
|
80 | - } |
|
81 | - |
|
82 | - $authorName = $this->authorName; |
|
83 | - |
|
84 | - return $authorName . ($authorEmail !== '' ? ($authorName !== '' ? ' ' : '') . $authorEmail : ''); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Attempts to create a new Author object based on the tag body. |
|
89 | - */ |
|
90 | - public static function create(string $body): ?self |
|
91 | - { |
|
92 | - $splitTagContent = preg_match('/^([^\<]*)(?:\<([^\>]*)\>)?$/u', $body, $matches); |
|
93 | - if (!$splitTagContent) { |
|
94 | - return null; |
|
95 | - } |
|
96 | - |
|
97 | - $authorName = trim($matches[1]); |
|
98 | - $email = isset($matches[2]) ? trim($matches[2]) : ''; |
|
99 | - |
|
100 | - return new static($authorName, $email); |
|
101 | - } |
|
29 | + /** @var string register that this is the author tag. */ |
|
30 | + protected $name = 'author'; |
|
31 | + |
|
32 | + /** @var string The name of the author */ |
|
33 | + private $authorName; |
|
34 | + |
|
35 | + /** @var string The email of the author */ |
|
36 | + private $authorEmail; |
|
37 | + |
|
38 | + /** |
|
39 | + * Initializes this tag with the author name and e-mail. |
|
40 | + */ |
|
41 | + public function __construct(string $authorName, string $authorEmail) |
|
42 | + { |
|
43 | + if ($authorEmail && !filter_var($authorEmail, FILTER_VALIDATE_EMAIL)) { |
|
44 | + throw new InvalidArgumentException('The author tag does not have a valid e-mail address'); |
|
45 | + } |
|
46 | + |
|
47 | + $this->authorName = $authorName; |
|
48 | + $this->authorEmail = $authorEmail; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Gets the author's name. |
|
53 | + * |
|
54 | + * @return string The author's name. |
|
55 | + */ |
|
56 | + public function getAuthorName(): string |
|
57 | + { |
|
58 | + return $this->authorName; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Returns the author's email. |
|
63 | + * |
|
64 | + * @return string The author's email. |
|
65 | + */ |
|
66 | + public function getEmail(): string |
|
67 | + { |
|
68 | + return $this->authorEmail; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Returns this tag in string form. |
|
73 | + */ |
|
74 | + public function __toString(): string |
|
75 | + { |
|
76 | + if ($this->authorEmail) { |
|
77 | + $authorEmail = '<' . $this->authorEmail . '>'; |
|
78 | + } else { |
|
79 | + $authorEmail = ''; |
|
80 | + } |
|
81 | + |
|
82 | + $authorName = $this->authorName; |
|
83 | + |
|
84 | + return $authorName . ($authorEmail !== '' ? ($authorName !== '' ? ' ' : '') . $authorEmail : ''); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Attempts to create a new Author object based on the tag body. |
|
89 | + */ |
|
90 | + public static function create(string $body): ?self |
|
91 | + { |
|
92 | + $splitTagContent = preg_match('/^([^\<]*)(?:\<([^\>]*)\>)?$/u', $body, $matches); |
|
93 | + if (!$splitTagContent) { |
|
94 | + return null; |
|
95 | + } |
|
96 | + |
|
97 | + $authorName = trim($matches[1]); |
|
98 | + $email = isset($matches[2]) ? trim($matches[2]) : ''; |
|
99 | + |
|
100 | + return new static($authorName, $email); |
|
101 | + } |
|
102 | 102 | } |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | */ |
41 | 41 | public function __construct(string $authorName, string $authorEmail) |
42 | 42 | { |
43 | - if ($authorEmail && !filter_var($authorEmail, FILTER_VALIDATE_EMAIL)) { |
|
43 | + if ($authorEmail && ! filter_var($authorEmail, FILTER_VALIDATE_EMAIL)) { |
|
44 | 44 | throw new InvalidArgumentException('The author tag does not have a valid e-mail address'); |
45 | 45 | } |
46 | 46 | |
@@ -74,14 +74,14 @@ discard block |
||
74 | 74 | public function __toString(): string |
75 | 75 | { |
76 | 76 | if ($this->authorEmail) { |
77 | - $authorEmail = '<' . $this->authorEmail . '>'; |
|
77 | + $authorEmail = '<'.$this->authorEmail.'>'; |
|
78 | 78 | } else { |
79 | 79 | $authorEmail = ''; |
80 | 80 | } |
81 | 81 | |
82 | 82 | $authorName = $this->authorName; |
83 | 83 | |
84 | - return $authorName . ($authorEmail !== '' ? ($authorName !== '' ? ' ' : '') . $authorEmail : ''); |
|
84 | + return $authorName.($authorEmail !== '' ? ($authorName !== '' ? ' ' : '').$authorEmail : ''); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | public static function create(string $body): ?self |
91 | 91 | { |
92 | 92 | $splitTagContent = preg_match('/^([^\<]*)(?:\<([^\>]*)\>)?$/u', $body, $matches); |
93 | - if (!$splitTagContent) { |
|
93 | + if ( ! $splitTagContent) { |
|
94 | 94 | return null; |
95 | 95 | } |
96 | 96 |
@@ -22,45 +22,45 @@ |
||
22 | 22 | |
23 | 23 | abstract class TagWithType extends BaseTag |
24 | 24 | { |
25 | - /** @var ?Type */ |
|
26 | - protected $type; |
|
25 | + /** @var ?Type */ |
|
26 | + protected $type; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Returns the type section of the variable. |
|
30 | - */ |
|
31 | - public function getType(): ?Type |
|
32 | - { |
|
33 | - return $this->type; |
|
34 | - } |
|
28 | + /** |
|
29 | + * Returns the type section of the variable. |
|
30 | + */ |
|
31 | + public function getType(): ?Type |
|
32 | + { |
|
33 | + return $this->type; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return string[] |
|
38 | - */ |
|
39 | - protected static function extractTypeFromBody(string $body): array |
|
40 | - { |
|
41 | - $type = ''; |
|
42 | - $nestingLevel = 0; |
|
43 | - for ($i = 0, $iMax = strlen($body); $i < $iMax; $i++) { |
|
44 | - $character = $body[$i]; |
|
36 | + /** |
|
37 | + * @return string[] |
|
38 | + */ |
|
39 | + protected static function extractTypeFromBody(string $body): array |
|
40 | + { |
|
41 | + $type = ''; |
|
42 | + $nestingLevel = 0; |
|
43 | + for ($i = 0, $iMax = strlen($body); $i < $iMax; $i++) { |
|
44 | + $character = $body[$i]; |
|
45 | 45 | |
46 | - if ($nestingLevel === 0 && trim($character) === '') { |
|
47 | - break; |
|
48 | - } |
|
46 | + if ($nestingLevel === 0 && trim($character) === '') { |
|
47 | + break; |
|
48 | + } |
|
49 | 49 | |
50 | - $type .= $character; |
|
51 | - if (in_array($character, ['<', '(', '[', '{'])) { |
|
52 | - $nestingLevel++; |
|
53 | - continue; |
|
54 | - } |
|
50 | + $type .= $character; |
|
51 | + if (in_array($character, ['<', '(', '[', '{'])) { |
|
52 | + $nestingLevel++; |
|
53 | + continue; |
|
54 | + } |
|
55 | 55 | |
56 | - if (in_array($character, ['>', ')', ']', '}'])) { |
|
57 | - $nestingLevel--; |
|
58 | - continue; |
|
59 | - } |
|
60 | - } |
|
56 | + if (in_array($character, ['>', ')', ']', '}'])) { |
|
57 | + $nestingLevel--; |
|
58 | + continue; |
|
59 | + } |
|
60 | + } |
|
61 | 61 | |
62 | - $description = trim(substr($body, strlen($type))); |
|
62 | + $description = trim(substr($body, strlen($type))); |
|
63 | 63 | |
64 | - return [$type, $description]; |
|
65 | - } |
|
64 | + return [$type, $description]; |
|
65 | + } |
|
66 | 66 | } |
@@ -20,17 +20,17 @@ |
||
20 | 20 | */ |
21 | 21 | final class Url implements Reference |
22 | 22 | { |
23 | - /** @var string */ |
|
24 | - private $uri; |
|
23 | + /** @var string */ |
|
24 | + private $uri; |
|
25 | 25 | |
26 | - public function __construct(string $uri) |
|
27 | - { |
|
28 | - Assert::stringNotEmpty($uri); |
|
29 | - $this->uri = $uri; |
|
30 | - } |
|
26 | + public function __construct(string $uri) |
|
27 | + { |
|
28 | + Assert::stringNotEmpty($uri); |
|
29 | + $this->uri = $uri; |
|
30 | + } |
|
31 | 31 | |
32 | - public function __toString(): string |
|
33 | - { |
|
34 | - return $this->uri; |
|
35 | - } |
|
32 | + public function __toString(): string |
|
33 | + { |
|
34 | + return $this->uri; |
|
35 | + } |
|
36 | 36 | } |
@@ -20,19 +20,19 @@ |
||
20 | 20 | */ |
21 | 21 | final class Fqsen implements Reference |
22 | 22 | { |
23 | - /** @var RealFqsen */ |
|
24 | - private $fqsen; |
|
23 | + /** @var RealFqsen */ |
|
24 | + private $fqsen; |
|
25 | 25 | |
26 | - public function __construct(RealFqsen $fqsen) |
|
27 | - { |
|
28 | - $this->fqsen = $fqsen; |
|
29 | - } |
|
26 | + public function __construct(RealFqsen $fqsen) |
|
27 | + { |
|
28 | + $this->fqsen = $fqsen; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @return string string representation of the referenced fqsen |
|
33 | - */ |
|
34 | - public function __toString(): string |
|
35 | - { |
|
36 | - return (string) $this->fqsen; |
|
37 | - } |
|
31 | + /** |
|
32 | + * @return string string representation of the referenced fqsen |
|
33 | + */ |
|
34 | + public function __toString(): string |
|
35 | + { |
|
36 | + return (string) $this->fqsen; |
|
37 | + } |
|
38 | 38 | } |