1515 *
1616 * @property string|FALSE $body
1717 */
18- class Method extends Member
18+ class Method
1919{
20- /** @var array of name => Parameter */
21- private $ parameters = [];
20+ use Nette \SmartObject;
21+ use Traits \FunctionLike;
22+ use Traits \NameAware;
23+ use Traits \VisibilityAware;
24+ use Traits \CommentAware;
2225
2326 /** @var Parameter[] */
2427 private $ uses = [];
@@ -35,21 +38,6 @@ class Method extends Member
3538 /** @var bool */
3639 private $ abstract = FALSE ;
3740
38- /** @var bool */
39- private $ returnReference = FALSE ;
40-
41- /** @var bool */
42- private $ variadic = FALSE ;
43-
44- /** @var PhpNamespace|NULL */
45- private $ namespace ;
46-
47- /** @var string|NULL */
48- private $ returnType ;
49-
50- /** @var bool */
51- private $ returnNullable ;
52-
5341
5442 /**
5543 * @param callable
@@ -63,89 +51,28 @@ public static function from($method)
6351 }
6452
6553
66- /**
67- * @param string|NULL
68- */
69- public function __construct ($ name = NULL )
70- {
71- $ this ->setName ($ name );
72- }
73-
74-
7554 /**
7655 * @return string PHP code
7756 */
7857 public function __toString ()
7958 {
80- $ parameters = [];
81- foreach ($ this ->parameters as $ param ) {
82- $ variadic = $ this ->variadic && $ param === end ($ this ->parameters );
83- $ hint = $ param ->getTypeHint ();
84- $ parameters [] = ($ hint ? ($ param ->isNullable () ? '? ' : '' ) . ($ this ->namespace ? $ this ->namespace ->unresolveName ($ hint ) : $ hint ) . ' ' : '' )
85- . ($ param ->isReference () ? '& ' : '' )
86- . ($ variadic ? '... ' : '' )
87- . '$ ' . $ param ->getName ()
88- . ($ param ->hasDefaultValue () && !$ variadic ? ' = ' . Helpers::dump ($ param ->defaultValue ) : '' );
89- }
9059 $ uses = [];
9160 foreach ($ this ->uses as $ param ) {
9261 $ uses [] = ($ param ->isReference () ? '& ' : '' ) . '$ ' . $ param ->getName ();
9362 }
94-
95- return Helpers::formatDocComment ($ this ->getComment () . "\n" )
63+ return Helpers::formatDocComment ($ this ->comment . "\n" )
9664 . ($ this ->abstract ? 'abstract ' : '' )
9765 . ($ this ->final ? 'final ' : '' )
98- . ($ this ->getVisibility () ? $ this ->getVisibility () . ' ' : '' )
66+ . ($ this ->visibility ? $ this ->visibility . ' ' : '' )
9967 . ($ this ->static ? 'static ' : '' )
10068 . 'function '
10169 . ($ this ->returnReference ? '& ' : '' )
102- . $ this ->getName ()
103- . ' ( ' . implode ( ' , ' , $ parameters ) . ' ) '
70+ . $ this ->name
71+ . $ this -> parametersToString ()
10472 . ($ this ->uses ? ' use ( ' . implode (', ' , $ uses ) . ') ' : '' )
105- . ($ this ->returnType ? ': ' . ($ this ->returnNullable ? '? ' : '' )
106- . ($ this ->namespace ? $ this ->namespace ->unresolveName ($ this ->returnType ) : $ this ->returnType ) : '' )
73+ . $ this ->returnTypeToString ()
10774 . ($ this ->abstract || $ this ->body === FALSE ? '; '
108- : ($ this ->getName () ? "\n" : ' ' ) . "{ \n" . Nette \Utils \Strings::indent (ltrim (rtrim ($ this ->body ) . "\n" ), 1 ) . '} ' );
109- }
110-
111-
112- /**
113- * @param Parameter[]
114- * @return static
115- */
116- public function setParameters (array $ val )
117- {
118- $ this ->parameters = [];
119- foreach ($ val as $ v ) {
120- if (!$ v instanceof Parameter) {
121- throw new Nette \InvalidArgumentException ('Argument must be Nette\PhpGenerator\Parameter[]. ' );
122- }
123- $ this ->parameters [$ v ->getName ()] = $ v ;
124- }
125- return $ this ;
126- }
127-
128-
129- /**
130- * @return Parameter[]
131- */
132- public function getParameters ()
133- {
134- return $ this ->parameters ;
135- }
136-
137-
138- /**
139- * @param string without $
140- * @return Parameter
141- */
142- public function addParameter ($ name , $ defaultValue = NULL )
143- {
144- $ param = new Parameter ($ name );
145- if (func_num_args () > 1 ) {
146- $ param ->setOptional (TRUE )->setDefaultValue ($ defaultValue );
147- }
148- return $ this ->parameters [$ name ] = $ param ;
75+ : ($ this ->name ? "\n" : ' ' ) . "{ \n" . Nette \Utils \Strings::indent (ltrim (rtrim ($ this ->body ) . "\n" ), 1 ) . '} ' );
14976 }
15077
15178
@@ -267,94 +194,4 @@ public function isAbstract()
267194 return $ this ->abstract ;
268195 }
269196
270-
271- /**
272- * @param bool
273- * @return static
274- */
275- public function setReturnReference ($ val )
276- {
277- $ this ->returnReference = (bool ) $ val ;
278- return $ this ;
279- }
280-
281-
282- /**
283- * @return bool
284- */
285- public function getReturnReference ()
286- {
287- return $ this ->returnReference ;
288- }
289-
290-
291- /**
292- * @param bool
293- * @return static
294- */
295- public function setReturnNullable ($ val )
296- {
297- $ this ->returnNullable = (bool ) $ val ;
298- return $ this ;
299- }
300-
301-
302- /**
303- * @return bool
304- */
305- public function getReturnNullable ()
306- {
307- return $ this ->returnNullable ;
308- }
309-
310-
311- /**
312- * @param bool
313- * @return static
314- */
315- public function setVariadic ($ val )
316- {
317- $ this ->variadic = (bool ) $ val ;
318- return $ this ;
319- }
320-
321-
322- /**
323- * @return bool
324- */
325- public function isVariadic ()
326- {
327- return $ this ->variadic ;
328- }
329-
330-
331- /**
332- * @return static
333- */
334- public function setNamespace (PhpNamespace $ val = NULL )
335- {
336- $ this ->namespace = $ val ;
337- return $ this ;
338- }
339-
340-
341- /**
342- * @param string|NULL
343- * @return static
344- */
345- public function setReturnType ($ val )
346- {
347- $ this ->returnType = $ val ? (string ) $ val : NULL ;
348- return $ this ;
349- }
350-
351-
352- /**
353- * @return string|NULL
354- */
355- public function getReturnType ()
356- {
357- return $ this ->returnType ;
358- }
359-
360197}
0 commit comments