Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • ClassType
  • Constant
  • Factory
  • Helpers
  • Member
  • Method
  • Parameter
  • PhpFile
  • PhpLiteral
  • PhpNamespace
  • Property
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\PhpGenerator;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Method or function description.
 15:  *
 16:  * @property string $body
 17:  */
 18: class Method extends Member
 19: {
 20:     /** @var array of name => Parameter */
 21:     private $parameters = [];
 22: 
 23:     /** @var array of name => bool */
 24:     private $uses = [];
 25: 
 26:     /** @var string|FALSE */
 27:     private $body = '';
 28: 
 29:     /** @var bool */
 30:     private $static = FALSE;
 31: 
 32:     /** @var bool */
 33:     private $final = FALSE;
 34: 
 35:     /** @var bool */
 36:     private $abstract = FALSE;
 37: 
 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: 
 54:     /**
 55:      * @return static
 56:      */
 57:     public static function from($from)
 58:     {
 59:         return (new Factory)->fromFunctionReflection(
 60:             $from instanceof \ReflectionFunctionAbstract ? $from : Nette\Utils\Callback::toReflection($from)
 61:         );
 62:     }
 63: 
 64: 
 65:     /**
 66:      * @param  string|NULL
 67:      */
 68:     public function __construct($name = NULL)
 69:     {
 70:         $this->setName($name);
 71:     }
 72: 
 73: 
 74:     /**
 75:      * @return string  PHP code
 76:      */
 77:     public function __toString()
 78:     {
 79:         $parameters = [];
 80:         foreach ($this->parameters as $param) {
 81:             $variadic = $this->variadic && $param === end($this->parameters);
 82:             $hint = $param->getTypeHint();
 83:             $parameters[] = ($hint ? ($param->isNullable() ? '?' : '') . ($this->namespace ? $this->namespace->unresolveName($hint) : $hint) . ' ' : '')
 84:                 . ($param->isReference() ? '&' : '')
 85:                 . ($variadic ? '...' : '')
 86:                 . '$' . $param->getName()
 87:                 . ($param->hasDefaultValue() && !$variadic ? ' = ' . Helpers::dump($param->defaultValue) : '');
 88:         }
 89:         $uses = [];
 90:         foreach ($this->uses as $param) {
 91:             $uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
 92:         }
 93: 
 94:         return Helpers::formatDocComment($this->getComment() . "\n")
 95:             . ($this->abstract ? 'abstract ' : '')
 96:             . ($this->final ? 'final ' : '')
 97:             . ($this->getVisibility() ? $this->getVisibility() . ' ' : '')
 98:             . ($this->static ? 'static ' : '')
 99:             . 'function '
100:             . ($this->returnReference ? '&' : '')
101:             . $this->getName()
102:             . '(' . implode(', ', $parameters) . ')'
103:             . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
104:             . ($this->returnType ? ': ' . ($this->returnNullable ? '?' : '')
105:                 . ($this->namespace ? $this->namespace->unresolveName($this->returnType) : $this->returnType) : '')
106:             . ($this->abstract || $this->body === FALSE ? ';'
107:                 : ($this->getName() ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
108:     }
109: 
110: 
111:     /**
112:      * @param  Parameter[]
113:      * @return static
114:      */
115:     public function setParameters(array $val)
116:     {
117:         $this->parameters = [];
118:         foreach ($val as $v) {
119:             if (!$v instanceof Parameter) {
120:                 throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Parameter[].');
121:             }
122:             $this->parameters[$v->getName()] = $v;
123:         }
124:         return $this;
125:     }
126: 
127: 
128:     /**
129:      * @return Parameter[]
130:      */
131:     public function getParameters()
132:     {
133:         return $this->parameters;
134:     }
135: 
136: 
137:     /**
138:      * @param  string  without $
139:      * @return Parameter
140:      */
141:     public function addParameter($name, $defaultValue = NULL)
142:     {
143:         $param = new Parameter($name);
144:         if (func_num_args() > 1) {
145:             $param->setOptional(TRUE)->setDefaultValue($defaultValue);
146:         }
147:         return $this->parameters[$name] = $param;
148:     }
149: 
150: 
151:     /**
152:      * @return static
153:      */
154:     public function setUses(array $val)
155:     {
156:         $this->uses = $val;
157:         return $this;
158:     }
159: 
160: 
161:     /**
162:      * @return array
163:      */
164:     public function getUses()
165:     {
166:         return $this->uses;
167:     }
168: 
169: 
170:     /**
171:      * @return Parameter
172:      */
173:     public function addUse($name)
174:     {
175:         return $this->uses[] = new Parameter($name);
176:     }
177: 
178: 
179:     /**
180:      * @return static
181:      */
182:     public function setBody($code, array $args = NULL)
183:     {
184:         $this->body = $args === NULL ? $code : Helpers::formatArgs($code, $args);
185:         return $this;
186:     }
187: 
188: 
189:     /**
190:      * @return string
191:      */
192:     public function getBody()
193:     {
194:         return $this->body;
195:     }
196: 
197: 
198:     /**
199:      * @return static
200:      */
201:     public function addBody($code, array $args = NULL)
202:     {
203:         $this->body .= ($args === NULL ? $code : Helpers::formatArgs($code, $args)) . "\n";
204:         return $this;
205:     }
206: 
207: 
208:     /**
209:      * @param  bool
210:      * @return static
211:      */
212:     public function setStatic($val)
213:     {
214:         $this->static = (bool) $val;
215:         return $this;
216:     }
217: 
218: 
219:     /**
220:      * @return bool
221:      */
222:     public function isStatic()
223:     {
224:         return $this->static;
225:     }
226: 
227: 
228:     /**
229:      * @param  bool
230:      * @return static
231:      */
232:     public function setFinal($val)
233:     {
234:         $this->final = (bool) $val;
235:         return $this;
236:     }
237: 
238: 
239:     /**
240:      * @return bool
241:      */
242:     public function isFinal()
243:     {
244:         return $this->final;
245:     }
246: 
247: 
248:     /**
249:      * @param  bool
250:      * @return static
251:      */
252:     public function setAbstract($val)
253:     {
254:         $this->abstract = (bool) $val;
255:         return $this;
256:     }
257: 
258: 
259:     /**
260:      * @return bool
261:      */
262:     public function isAbstract()
263:     {
264:         return $this->abstract;
265:     }
266: 
267: 
268:     /**
269:      * @param  bool
270:      * @return static
271:      */
272:     public function setReturnReference($val)
273:     {
274:         $this->returnReference = (bool) $val;
275:         return $this;
276:     }
277: 
278: 
279:     /**
280:      * @return bool
281:      */
282:     public function getReturnReference()
283:     {
284:         return $this->returnReference;
285:     }
286: 
287: 
288:     /**
289:      * @param  bool
290:      * @return static
291:      */
292:     public function setReturnNullable($val)
293:     {
294:         $this->returnNullable = (bool) $val;
295:         return $this;
296:     }
297: 
298: 
299:     /**
300:      * @return bool
301:      */
302:     public function getReturnNullable()
303:     {
304:         return $this->returnNullable;
305:     }
306: 
307: 
308:     /**
309:      * @param  bool
310:      * @return static
311:      */
312:     public function setVariadic($val)
313:     {
314:         $this->variadic = (bool) $val;
315:         return $this;
316:     }
317: 
318: 
319:     /**
320:      * @return bool
321:      */
322:     public function isVariadic()
323:     {
324:         return $this->variadic;
325:     }
326: 
327: 
328:     /**
329:      * @return static
330:      */
331:     public function setNamespace(PhpNamespace $val = NULL)
332:     {
333:         $this->namespace = $val;
334:         return $this;
335:     }
336: 
337: 
338:     /**
339:      * @param  string|NULL
340:      * @return static
341:      */
342:     public function setReturnType($val)
343:     {
344:         $this->returnType = $val ? (string) $val : NULL;
345:         return $this;
346:     }
347: 
348: 
349:     /**
350:      * @return string|NULL
351:      */
352:     public function getReturnType()
353:     {
354:         return $this->returnType;
355:     }
356: 
357: }
358: 
Nette 2.4-20170221 API API documentation generated by ApiGen 2.8.0