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
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

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