1515
1616use InvalidArgumentException ;
1717use phpDocumentor \Reflection \DocBlock \Description ;
18- use phpDocumentor \Reflection \DocBlock \ DescriptionFactory ;
18+ use phpDocumentor \Reflection \Exception \ CannotCreateTag ;
1919use phpDocumentor \Reflection \Type ;
20- use phpDocumentor \Reflection \TypeResolver ;
21- use phpDocumentor \Reflection \Types \Context as TypeContext ;
2220use phpDocumentor \Reflection \Types \Mixed_ ;
2321use phpDocumentor \Reflection \Types \Void_ ;
2422use Webmozart \Assert \Assert ;
2523
26- use function array_keys ;
27- use function array_map ;
28- use function explode ;
2924use 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-
4125/**
4226 * Reflection class for an {@}method in a Docblock.
4327 */
@@ -57,152 +41,28 @@ final class Method extends BaseTag
5741 private array $ parameters ;
5842
5943 /**
60- * @param array<int, array<string, Type|string>> $arguments
6144 * @param MethodParameter[] $parameters
62- * @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
6345 */
6446 public function __construct (
6547 string $ methodName ,
66- array $ arguments = [],
48+ array $ parameters = [],
6749 ?Type $ returnType = null ,
6850 bool $ static = false ,
6951 ?Description $ description = null ,
70- bool $ returnsReference = false ,
71- ?array $ parameters = null
52+ bool $ returnsReference = false
7253 ) {
7354 Assert::stringNotEmpty ($ methodName );
7455
7556 if ($ returnType === null ) {
7657 $ returnType = new Void_ ();
7758 }
7859
79- $ arguments = $ this ->filterArguments ($ arguments );
80-
8160 $ this ->methodName = $ methodName ;
8261 $ this ->returnType = $ returnType ;
8362 $ this ->isStatic = $ static ;
8463 $ this ->description = $ description ;
8564 $ 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- );
65+ $ this ->parameters = $ parameters ;
20666 }
20767
20868 /**
@@ -213,24 +73,6 @@ public function getMethodName(): string
21373 return $ this ->methodName ;
21474 }
21575
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-
23476 /** @return MethodParameter[] */
23577 public function getParameters (): array
23678 {
@@ -287,69 +129,8 @@ public function __toString(): string
287129 . ($ description !== '' ? ' ' . $ description : '' );
288130 }
289131
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
132+ public static function create (string $ body )
298133 {
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
339- {
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- );
134+ throw new CannotCreateTag ('Method tag cannot be created ' );
354135 }
355136}
0 commit comments