1313
1414namespace phpDocumentor \Reflection \DocBlock \Tags ;
1515
16- use InvalidArgumentException ;
1716use phpDocumentor \Reflection \DocBlock \Description ;
18- use phpDocumentor \Reflection \DocBlock \ DescriptionFactory ;
17+ use phpDocumentor \Reflection \Exception \ CannotCreateTag ;
1918use phpDocumentor \Reflection \Type ;
20- use phpDocumentor \Reflection \TypeResolver ;
21- use phpDocumentor \Reflection \Types \Context as TypeContext ;
22- use phpDocumentor \Reflection \Types \Mixed_ ;
2319use phpDocumentor \Reflection \Types \Void_ ;
2420use Webmozart \Assert \Assert ;
2521
26- use function array_keys ;
27- use function array_map ;
28- use function explode ;
2922use function implode ;
30- use function is_string ;
31- use function preg_match ;
32- use function sort ;
33- use function strpos ;
34- use function substr ;
35- use function trigger_error ;
36- use function trim ;
37- use function var_export ;
38-
39- use const E_USER_DEPRECATED ;
40-
4123/**
4224 * Reflection class for an {@}method in a Docblock.
4325 */
26+
4427final class Method extends BaseTag
4528{
4629 protected string $ name = 'method ' ;
@@ -57,152 +40,28 @@ final class Method extends BaseTag
5740 private array $ parameters ;
5841
5942 /**
60- * @param array<int, array<string, Type|string>> $arguments
6143 * @param MethodParameter[] $parameters
62- * @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
6344 */
6445 public function __construct (
6546 string $ methodName ,
66- array $ arguments = [],
47+ array $ parameters = [],
6748 ?Type $ returnType = null ,
6849 bool $ static = false ,
6950 ?Description $ description = null ,
70- bool $ returnsReference = false ,
71- ?array $ parameters = null
51+ bool $ returnsReference = false
7252 ) {
7353 Assert::stringNotEmpty ($ methodName );
7454
7555 if ($ returnType === null ) {
7656 $ returnType = new Void_ ();
7757 }
7858
79- $ arguments = $ this ->filterArguments ($ arguments );
80-
8159 $ this ->methodName = $ methodName ;
8260 $ this ->returnType = $ returnType ;
8361 $ this ->isStatic = $ static ;
8462 $ this ->description = $ description ;
8563 $ this ->returnsReference = $ returnsReference ;
86- $ this ->parameters = $ parameters ?? $ this ->fromLegacyArguments ($ arguments );
87- }
88-
89- /**
90- * @deprecated Create using static factory is deprecated,
91- * this method should not be called directly by library consumers
92- */
93- public static function create (
94- string $ body ,
95- ?TypeResolver $ typeResolver = null ,
96- ?DescriptionFactory $ descriptionFactory = null ,
97- ?TypeContext $ context = null
98- ): ?self {
99- trigger_error (
100- 'Create using static factory is deprecated, this method should not be called directly
101- by library consumers ' ,
102- E_USER_DEPRECATED
103- );
104- Assert::stringNotEmpty ($ body );
105- Assert::notNull ($ typeResolver );
106- Assert::notNull ($ descriptionFactory );
107-
108- // 1. none or more whitespace
109- // 2. optionally the keyword "static" followed by whitespace
110- // 3. optionally a word with underscores followed by whitespace : as
111- // type for the return value
112- // 4. optionally an ampersand followed or not by whitespace : as
113- // a reference
114- // 5. then optionally a word with underscores followed by () and
115- // whitespace : as method name as used by phpDocumentor
116- // 6. then a word with underscores, followed by ( and any character
117- // until a ) and whitespace : as method name with signature
118- // 7. any remaining text : as description
119- if (
120- !preg_match (
121- '/^
122- # Static keyword
123- # Declares a static method ONLY if type is also present
124- (?:
125- (static)
126- \s+
127- )?
128- # Return type
129- (?:
130- (
131- (?:[\w\|_ \\\\]*\$this[\w\|_ \\\\]*)
132- |
133- (?:
134- (?:[\w\|_ \\\\]+)
135- # array notation
136- (?:\[\])*
137- )*+
138- )
139- \s+
140- )?
141- # Returns reference
142- (?:
143- (&)
144- \s*
145- )?
146- # Method name
147- ([\w_]+)
148- # Arguments
149- (?:
150- \(([^\)]*)\)
151- )?
152- \s*
153- # Description
154- (.*)
155- $/sux ' ,
156- $ body ,
157- $ matches
158- )
159- ) {
160- return null ;
161- }
162-
163- [, $ static , $ returnType , $ returnsReference , $ methodName , $ argumentLines , $ description ] = $ matches ;
164-
165- $ static = $ static === 'static ' ;
166-
167- if ($ returnType === '' ) {
168- $ returnType = 'void ' ;
169- }
170-
171- $ returnsReference = $ returnsReference === '& ' ;
172-
173- $ returnType = $ typeResolver ->resolve ($ returnType , $ context );
174- $ description = $ descriptionFactory ->create ($ description , $ context );
175-
176- /** @phpstan-var array<int, array{name: string, type: Type}> $arguments */
177- $ arguments = [];
178- if ($ argumentLines !== '' ) {
179- $ argumentsExploded = explode (', ' , $ argumentLines );
180- foreach ($ argumentsExploded as $ argument ) {
181- $ argument = explode (' ' , self ::stripRestArg (trim ($ argument )), 2 );
182- if (strpos ($ argument [0 ], '$ ' ) === 0 ) {
183- $ argumentName = substr ($ argument [0 ], 1 );
184- $ argumentType = new Mixed_ ();
185- } else {
186- $ argumentType = $ typeResolver ->resolve ($ argument [0 ], $ context );
187- $ argumentName = '' ;
188- if (isset ($ argument [1 ])) {
189- $ argument [1 ] = self ::stripRestArg ($ argument [1 ]);
190- $ argumentName = substr ($ argument [1 ], 1 );
191- }
192- }
193-
194- $ arguments [] = ['name ' => $ argumentName , 'type ' => $ argumentType ];
195- }
196- }
197-
198- return new static (
199- $ methodName ,
200- $ arguments ,
201- $ returnType ,
202- $ static ,
203- $ description ,
204- $ returnsReference
205- );
64+ $ this ->parameters = $ parameters ;
20665 }
20766
20867 /**
@@ -213,24 +72,6 @@ public function getMethodName(): string
21372 return $ this ->methodName ;
21473 }
21574
216- /**
217- * @deprecated Method deprecated, use {@see self::getParameters()}
218- *
219- * @return array<int, array<string, Type|string>>
220- * @phpstan-return array<int, array{name: string, type: Type}>
221- */
222- public function getArguments (): array
223- {
224- trigger_error ('Method deprecated, use ::getParameters() ' , E_USER_DEPRECATED );
225-
226- return array_map (
227- static function (MethodParameter $ methodParameter ) {
228- return ['name ' => $ methodParameter ->getName (), 'type ' => $ methodParameter ->getType ()];
229- },
230- $ this ->parameters
231- );
232- }
233-
23475 /** @return MethodParameter[] */
23576 public function getParameters (): array
23677 {
@@ -287,69 +128,8 @@ public function __toString(): string
287128 . ($ description !== '' ? ' ' . $ description : '' );
288129 }
289130
290- /**
291- * @param mixed[][]|string[] $arguments
292- * @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
293- *
294- * @return mixed[][]
295- * @phpstan-return array<int, array{name: string, type: Type}>
296- */
297- private function filterArguments (array $ arguments = []): array
298- {
299- $ result = [];
300- foreach ($ arguments as $ argument ) {
301- if (is_string ($ argument )) {
302- $ argument = ['name ' => $ argument ];
303- }
304-
305- if (!isset ($ argument ['type ' ])) {
306- $ argument ['type ' ] = new Mixed_ ();
307- }
308-
309- $ keys = array_keys ($ argument );
310- sort ($ keys );
311- if ($ keys !== ['name ' , 'type ' ]) {
312- throw new InvalidArgumentException (
313- 'Arguments can only have the "name" and "type" fields, found: ' . var_export ($ keys , true )
314- );
315- }
316-
317- $ result [] = $ argument ;
318- }
319-
320- return $ result ;
321- }
322-
323- private static function stripRestArg (string $ argument ): string
324- {
325- if (strpos ($ argument , '... ' ) === 0 ) {
326- $ argument = trim (substr ($ argument , 3 ));
327- }
328-
329- return $ argument ;
330- }
331-
332- /**
333- * @param array{name: string, type: Type} $arguments
334- * @phpstan-param array<int, array{name: string, type: Type}> $arguments
335- *
336- * @return MethodParameter[]
337- */
338- private function fromLegacyArguments (array $ arguments ): array
131+ public static function create (string $ body ): void
339132 {
340- trigger_error (
341- 'Create method parameters via legacy format is deprecated add parameters via the constructor ' ,
342- E_USER_DEPRECATED
343- );
344-
345- return array_map (
346- static function ($ arg ) {
347- return new MethodParameter (
348- $ arg ['name ' ],
349- $ arg ['type ' ]
350- );
351- },
352- $ arguments
353- );
133+ throw new CannotCreateTag ('Method tag cannot be created ' );
354134 }
355135}
0 commit comments