Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • 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

Classes

  • ClassType
  • Helpers
  • Method
  • Parameter
  • PhpLiteral
  • Property
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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:  * @method Method setName(string)
 19:  * @method string getName()
 20:  * @method Method setParameters(Parameter[])
 21:  * @method Parameter[] getParameters()
 22:  * @method Method setUses(array)
 23:  * @method array getUses()
 24:  * @method string getBody()
 25:  * @method Method setStatic(bool)
 26:  * @method bool isStatic()
 27:  * @method Method setVisibility(string)
 28:  * @method string getVisibility()
 29:  * @method Method setFinal(bool)
 30:  * @method bool isFinal()
 31:  * @method Method setAbstract(bool)
 32:  * @method bool isAbstract()
 33:  * @method Method setReturnReference(bool)
 34:  * @method bool getReturnReference()
 35:  * @method Method setVariadic(bool)
 36:  * @method bool isVariadic()
 37:  * @method Method setDocuments(string[])
 38:  * @method string[] getDocuments()
 39:  * @method Method addDocument(string)
 40:  */
 41: class Method extends Nette\Object
 42: {
 43:     /** @var string */
 44:     private $name;
 45: 
 46:     /** @var array of name => Parameter */
 47:     private $parameters = array();
 48: 
 49:     /** @var array of name => bool */
 50:     private $uses = array();
 51: 
 52:     /** @var string|FALSE */
 53:     private $body;
 54: 
 55:     /** @var bool */
 56:     private $static;
 57: 
 58:     /** @var string  public|protected|private or none */
 59:     private $visibility;
 60: 
 61:     /** @var bool */
 62:     private $final;
 63: 
 64:     /** @var bool */
 65:     private $abstract;
 66: 
 67:     /** @var bool */
 68:     private $returnReference;
 69: 
 70:     /** @var bool */
 71:     private $variadic;
 72: 
 73:     /** @var array of string */
 74:     private $documents = array();
 75: 
 76: 
 77:     /** @return Method */
 78:     public static function from($from)
 79:     {
 80:         $from = $from instanceof \ReflectionMethod ? $from : new \ReflectionMethod($from);
 81:         $method = new static;
 82:         $method->name = $from->getName();
 83:         foreach ($from->getParameters() as $param) {
 84:             $method->parameters[$param->getName()] = Parameter::from($param);
 85:         }
 86:         $method->static = $from->isStatic();
 87:         $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : '');
 88:         $method->final = $from->isFinal();
 89:         $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface();
 90:         $method->body = $from->isAbstract() ? FALSE : '';
 91:         $method->returnReference = $from->returnsReference();
 92:         $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic();
 93:         $method->documents = preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"));
 94:         return $method;
 95:     }
 96: 
 97: 
 98:     /** @return Parameter */
 99:     public function addParameter($name, $defaultValue = NULL)
100:     {
101:         $param = new Parameter;
102:         if (func_num_args() > 1) {
103:             $param->setOptional(TRUE)->setDefaultValue($defaultValue);
104:         }
105:         return $this->parameters[$name] = $param->setName($name);
106:     }
107: 
108: 
109:     /** @return Parameter */
110:     public function addUse($name)
111:     {
112:         $param = new Parameter;
113:         return $this->uses[] = $param->setName($name);
114:     }
115: 
116: 
117:     /** @return Method */
118:     public function setBody($statement, array $args = NULL)
119:     {
120:         $this->body = func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement;
121:         return $this;
122:     }
123: 
124: 
125:     /** @return Method */
126:     public function addBody($statement, array $args = NULL)
127:     {
128:         $this->body .= (func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement) . "\n";
129:         return $this;
130:     }
131: 
132: 
133:     /** @return string  PHP code */
134:     public function __toString()
135:     {
136:         $parameters = array();
137:         foreach ($this->parameters as $param) {
138:             $variadic = $this->variadic && $param === end($this->parameters);
139:             $parameters[] = ($param->typeHint ? $param->typeHint . ' ' : '')
140:                 . ($param->reference ? '&' : '')
141:                 . ($variadic ? '...' : '')
142:                 . '$' . $param->name
143:                 . ($param->optional && !$variadic ? ' = ' . Helpers::dump($param->defaultValue) : '');
144:         }
145:         $uses = array();
146:         foreach ($this->uses as $param) {
147:             $uses[] = ($param->reference ? '&' : '') . '$' . $param->name;
148:         }
149:         return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
150:             . ($this->abstract ? 'abstract ' : '')
151:             . ($this->final ? 'final ' : '')
152:             . ($this->visibility ? $this->visibility . ' ' : '')
153:             . ($this->static ? 'static ' : '')
154:             . 'function'
155:             . ($this->returnReference ? ' &' : '')
156:             . ($this->name ? ' ' . $this->name : '')
157:             . '(' . implode(', ', $parameters) . ')'
158:             . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
159:             . ($this->abstract || $this->body === FALSE ? ';'
160:                 : ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(trim($this->body), 1) . "\n}");
161:     }
162: 
163: }
164: 
Nette 2.2 API documentation generated by ApiGen 2.8.0