2424use Webmozart \Assert \Assert ;
2525
2626use function array_keys ;
27+ use function array_map ;
2728use function explode ;
2829use function implode ;
2930use function is_string ;
@@ -63,6 +64,9 @@ final class Method extends BaseTag implements Factory\StaticMethod
6364 /** @var bool */
6465 private $ returnsReference ;
6566
67+ /** @var MethodParameter[] */
68+ private array $ parameters ;
69+
6670 /**
6771 * @param array<int, array<string, Type|string>> $arguments
6872 * @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
@@ -73,20 +77,24 @@ public function __construct(
7377 ?Type $ returnType = null ,
7478 bool $ static = false ,
7579 ?Description $ description = null ,
76- bool $ returnsReference = false
80+ bool $ returnsReference = false ,
81+ ?array $ parameters = null
7782 ) {
7883 Assert::stringNotEmpty ($ methodName );
7984
8085 if ($ returnType === null ) {
8186 $ returnType = new Void_ ();
8287 }
8388
89+ $ arguments = $ this ->filterArguments ($ arguments );
90+
8491 $ this ->methodName = $ methodName ;
85- $ this ->arguments = $ this -> filterArguments ( $ arguments) ;
92+ $ this ->arguments = $ arguments ;
8693 $ this ->returnType = $ returnType ;
8794 $ this ->isStatic = $ static ;
8895 $ this ->description = $ description ;
8996 $ this ->returnsReference = $ returnsReference ;
97+ $ this ->parameters = $ parameters ?? $ this ->fromLegacyArguments ($ arguments );
9098 }
9199
92100 public static function create (
@@ -190,7 +198,14 @@ public static function create(
190198 }
191199 }
192200
193- return new static ($ methodName , $ arguments , $ returnType , $ static , $ description , $ returnsReference );
201+ return new static (
202+ $ methodName ,
203+ $ arguments ,
204+ $ returnType ,
205+ $ static ,
206+ $ description ,
207+ $ returnsReference
208+ );
194209 }
195210
196211 /**
@@ -210,6 +225,12 @@ public function getArguments(): array
210225 return $ this ->arguments ;
211226 }
212227
228+ /** @return MethodParameter[] */
229+ public function getParameters (): array
230+ {
231+ return $ this ->parameters ;
232+ }
233+
213234 /**
214235 * Checks whether the method tag describes a static method or not.
215236 *
@@ -233,8 +254,11 @@ public function returnsReference(): bool
233254 public function __toString (): string
234255 {
235256 $ arguments = [];
236- foreach ($ this ->arguments as $ argument ) {
237- $ arguments [] = $ argument ['type ' ] . ' $ ' . $ argument ['name ' ];
257+ foreach ($ this ->parameters as $ parameter ) {
258+ $ arguments [] = ($ parameter ->getType () ?? new Mixed_ ()) . ' ' .
259+ ($ parameter ->isReference () ? '& ' : '' ) .
260+ ($ parameter ->isVariadic () ? '... ' : '' ) .
261+ '$ ' . $ parameter ->getName ();
238262 }
239263
240264 $ argumentStr = '( ' . implode (', ' , $ arguments ) . ') ' ;
@@ -301,4 +325,26 @@ private static function stripRestArg(string $argument): string
301325
302326 return $ argument ;
303327 }
328+
329+ /**
330+ * @param array{name: string, type: Type} $arguments
331+ * @return MethodParameter[]
332+ */
333+ private function fromLegacyArguments (array $ arguments ): array
334+ {
335+ trigger_error (
336+ 'Create method parameters via legacy format is deprecated add parameters via the constructor ' ,
337+ E_USER_DEPRECATED
338+ );
339+
340+ return array_map (
341+ static function ($ arg ) {
342+ return new MethodParameter (
343+ $ arg ['name ' ],
344+ $ arg ['type ' ]
345+ );
346+ },
347+ $ arguments
348+ );
349+ }
304350}
0 commit comments