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