15
15
*
16
16
* @property string|FALSE $body
17
17
*/
18
- class Method extends Member
18
+ class Method
19
19
{
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;
22
25
23
26
/** @var Parameter[] */
24
27
private $ uses = [];
@@ -35,21 +38,6 @@ class Method extends Member
35
38
/** @var bool */
36
39
private $ abstract = FALSE ;
37
40
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
-
53
41
54
42
/**
55
43
* @param callable
@@ -63,89 +51,28 @@ public static function from($method)
63
51
}
64
52
65
53
66
- /**
67
- * @param string|NULL
68
- */
69
- public function __construct ($ name = NULL )
70
- {
71
- $ this ->setName ($ name );
72
- }
73
-
74
-
75
54
/**
76
55
* @return string PHP code
77
56
*/
78
57
public function __toString ()
79
58
{
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
- }
90
59
$ uses = [];
91
60
foreach ($ this ->uses as $ param ) {
92
61
$ uses [] = ($ param ->isReference () ? '& ' : '' ) . '$ ' . $ param ->getName ();
93
62
}
94
-
95
- return Helpers::formatDocComment ($ this ->getComment () . "\n" )
63
+ return Helpers::formatDocComment ($ this ->comment . "\n" )
96
64
. ($ this ->abstract ? 'abstract ' : '' )
97
65
. ($ this ->final ? 'final ' : '' )
98
- . ($ this ->getVisibility () ? $ this ->getVisibility () . ' ' : '' )
66
+ . ($ this ->visibility ? $ this ->visibility . ' ' : '' )
99
67
. ($ this ->static ? 'static ' : '' )
100
68
. 'function '
101
69
. ($ this ->returnReference ? '& ' : '' )
102
- . $ this ->getName ()
103
- . ' ( ' . implode ( ' , ' , $ parameters ) . ' ) '
70
+ . $ this ->name
71
+ . $ this -> parametersToString ()
104
72
. ($ this ->uses ? ' use ( ' . implode (', ' , $ uses ) . ') ' : '' )
105
- . ($ this ->returnType ? ': ' . ($ this ->returnNullable ? '? ' : '' )
106
- . ($ this ->namespace ? $ this ->namespace ->unresolveName ($ this ->returnType ) : $ this ->returnType ) : '' )
73
+ . $ this ->returnTypeToString ()
107
74
. ($ 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 ) . '} ' );
149
76
}
150
77
151
78
@@ -267,94 +194,4 @@ public function isAbstract()
267
194
return $ this ->abstract ;
268
195
}
269
196
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
-
360
197
}
0 commit comments