Overview

Namespaces

  • TokenReflection
    • Broker
      • Backend
    • Dummy
    • Exception
    • Invalid
    • Php
    • Stream

Classes

  • ReflectionClass
  • ReflectionConstant
  • ReflectionExtension
  • ReflectionFunction
  • ReflectionMethod
  • ReflectionParameter
  • ReflectionProperty

Interfaces

  • IReflection
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: /**
  3:  * PHP Token Reflection
  4:  *
  5:  * Version 1.3.1
  6:  *
  7:  * LICENSE
  8:  *
  9:  * This source file is subject to the new BSD license that is bundled
 10:  * with this library in the file LICENSE.
 11:  *
 12:  * @author Ondřej Nešpor
 13:  * @author Jaroslav Hanslík
 14:  */
 15: 
 16: namespace TokenReflection\Php;
 17: 
 18: use TokenReflection;
 19: use TokenReflection\Broker, TokenReflection\Exception;
 20: use Reflector, ReflectionFunction as InternalReflectionFunction, ReflectionParameter as InternalReflectionParameter;
 21: 
 22: /**
 23:  * Reflection of a not tokenized but defined function.
 24:  *
 25:  * Descendant of the internal reflection with additional features.
 26:  */
 27: class ReflectionFunction extends InternalReflectionFunction implements IReflection, TokenReflection\IReflectionFunction
 28: {
 29:     /**
 30:      * Function parameter reflections.
 31:      *
 32:      * @var array
 33:      */
 34:     private $parameters;
 35: 
 36:     /**
 37:      * Reflection broker.
 38:      *
 39:      * @var \TokenReflection\Broker
 40:      */
 41:     private $broker;
 42: 
 43:     /**
 44:      * Constructor.
 45:      *
 46:      * @param string $functionName Function name
 47:      * @param \TokenReflection\Broker $broker Reflection broker
 48:      */
 49:     public function __construct($functionName, Broker $broker)
 50:     {
 51:         parent::__construct($functionName);
 52:         $this->broker = $broker;
 53:     }
 54: 
 55:     /**
 56:      * Returns the PHP extension reflection.
 57:      *
 58:      * @return \TokenReflection\IReflectionExtension
 59:      */
 60:     public function getExtension()
 61:     {
 62:         return ReflectionExtension::create(parent::getExtension(), $this->broker);
 63:     }
 64: 
 65:     /**
 66:      * Checks if there is a particular annotation.
 67:      *
 68:      * @param string $name Annotation name
 69:      * @return boolean
 70:      */
 71:     public function hasAnnotation($name)
 72:     {
 73:         return false;
 74:     }
 75: 
 76:     /**
 77:      * Returns a particular annotation value.
 78:      *
 79:      * @param string $name Annotation name
 80:      * @return null
 81:      */
 82:     public function getAnnotation($name)
 83:     {
 84:         return null;
 85:     }
 86: 
 87:     /**
 88:      * Returns parsed docblock.
 89:      *
 90:      * @return array
 91:      */
 92:     public function getAnnotations()
 93:     {
 94:         return array();
 95:     }
 96: 
 97:     /**
 98:      * Returns if the current reflection comes from a tokenized source.
 99:      *
100:      * @return boolean
101:      */
102:     public function isTokenized()
103:     {
104:         return false;
105:     }
106: 
107:     /**
108:      * Returns a particular parameter.
109:      *
110:      * @param integer|string $parameter Parameter name or position
111:      * @return \TokenReflection\Php\ReflectionParameter
112:      * @throws \TokenReflection\Exception\RuntimeException If there is no parameter of the given name.
113:      * @throws \TokenReflection\Exception\RuntimeException If there is no parameter at the given position.
114:      */
115:     public function getParameter($parameter)
116:     {
117:         $parameters = $this->getParameters();
118: 
119:         if (is_numeric($parameter)) {
120:             if (!isset($parameters[$parameter])) {
121:                 throw new Exception\RuntimeException(sprintf('There is no parameter at position "%d".', $parameter), Exception\RuntimeException::DOES_NOT_EXIST, $this);
122:             }
123: 
124:             return $parameters[$parameter];
125:         } else {
126:             foreach ($parameters as $reflection) {
127:                 if ($reflection->getName() === $parameter) {
128:                     return $reflection;
129:                 }
130:             }
131: 
132:             throw new Exception\RuntimeException(sprintf('There is no parameter "%s".', $parameter), Exception\RuntimeException::DOES_NOT_EXIST, $this);
133:         }
134:     }
135: 
136:     /**
137:      * Returns function parameters.
138:      *
139:      * @return array
140:      */
141:     public function getParameters()
142:     {
143:         if (null === $this->parameters) {
144:             $broker = $this->broker;
145:             $parent = $this;
146:             $this->parameters = array_map(function(InternalReflectionParameter $parameter) use ($broker, $parent) {
147:                 return ReflectionParameter::create($parameter, $broker, $parent);
148:             }, parent::getParameters());
149:         }
150: 
151:         return $this->parameters;
152:     }
153: 
154:     /**
155:      * Returns the reflection broker used by this reflection object.
156:      *
157:      * @return \TokenReflection\Broker
158:      */
159:     public function getBroker()
160:     {
161:         return $this->broker;
162:     }
163: 
164:     /**
165:      * Returns imported namespaces and aliases from the declaring namespace.
166:      *
167:      * @return array
168:      */
169:     public function getNamespaceAliases()
170:     {
171:         return array();
172:     }
173: 
174:     /**
175:      * Magic __get method.
176:      *
177:      * @param string $key Variable name
178:      * @return mixed
179:      */
180:     final public function __get($key)
181:     {
182:         return TokenReflection\ReflectionElement::get($this, $key);
183:     }
184: 
185:     /**
186:      * Magic __isset method.
187:      *
188:      * @param string $key Variable name
189:      * @return boolean
190:      */
191:     final public function __isset($key)
192:     {
193:         return TokenReflection\ReflectionElement::exists($this, $key);
194:     }
195: 
196:     /**
197:      * Returns the function/method as closure.
198:      *
199:      * @return \Closure
200:      */
201:     public function getClosure()
202:     {
203:         if (PHP_VERSION >= 50400) {
204:             return parent::getClosure();
205:         } else {
206:             $that = $this;
207:             return function() use ($that) {
208:                 return $that->invokeArgs(func_get_args());
209:             };
210:         }
211:     }
212: 
213:     /**
214:      * Returns the closure scope class.
215:      *
216:      * @return string|null
217:      */
218:     public function getClosureScopeClass()
219:     {
220:         return PHP_VERSION >= 50400 ? parent::getClosureScopeClass() : null;
221:     }
222: 
223:     /**
224:      * Returns this pointer bound to closure.
225:      *
226:      * @return null
227:      */
228:     public function getClosureThis()
229:     {
230:         return PHP_VERSION >= 50400 ? parent::getClosureThis() : null;
231:     }
232: 
233:     /**
234:      * Returns if the function definition is valid.
235:      *
236:      * Internal functions are always valid.
237:      *
238:      * @return boolean
239:      */
240:     public function isValid()
241:     {
242:         return true;
243:     }
244: 
245:     /**
246:      * Returns an element pretty (docblock compatible) name.
247:      *
248:      * @return string
249:      */
250:     public function getPrettyName()
251:     {
252:         return $this->getName() . '()';
253:     }
254: 
255:     /**
256:      * Creates a reflection instance.
257:      *
258:      * @param \ReflectionClass $internalReflection Internal reflection instance
259:      * @param \TokenReflection\Broker $broker Reflection broker instance
260:      * @return \TokenReflection\Php\ReflectionFunction
261:      * @throws \TokenReflection\Exception\RuntimeException If an invalid internal reflection object was provided.
262:      */
263:     public static function create(Reflector $internalReflection, Broker $broker)
264:     {
265:         if (!$internalReflection instanceof InternalReflectionFunction) {
266:             throw new Exception\RuntimeException('Invalid reflection instance provided, ReflectionFunction expected.', Exception\RuntimeException::INVALID_ARGUMENT);
267:         }
268: 
269:         return $broker->getFunction($internalReflection->getName());
270:     }
271: }
PHP Token Reflection API documentation generated by ApiGen 2.8.0