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, ReflectionMethod as InternalReflectionMethod, ReflectionParameter as InternalReflectionParameter;
 21: 
 22: /**
 23:  * Reflection of a not tokenized but defined class method.
 24:  *
 25:  * Descendant of the internal reflection with additional features.
 26:  */
 27: class ReflectionMethod extends InternalReflectionMethod implements IReflection, TokenReflection\IReflectionMethod
 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:      * Is the property accessible despite its access level.
 45:      *
 46:      * @var boolean
 47:      */
 48:     private $accessible = false;
 49: 
 50:     /**
 51:      * Constructor.
 52:      *
 53:      * @param string|\TokenReflection\Php\ReflectionClass|\ReflectionClass $class Defining class
 54:      * @param string $methodName Method name
 55:      * @param \TokenReflection\Broker $broker Reflection broker
 56:      */
 57:     public function __construct($class, $methodName, Broker $broker)
 58:     {
 59:         parent::__construct($class, $methodName);
 60:         $this->broker = $broker;
 61:     }
 62: 
 63:     /**
 64:      * Returns the declaring class reflection.
 65:      *
 66:      * @return \TokenReflection\IReflectionClass
 67:      */
 68:     public function getDeclaringClass()
 69:     {
 70:         return ReflectionClass::create(parent::getDeclaringClass(), $this->broker);
 71:     }
 72: 
 73:     /**
 74:      * Returns the declaring class name.
 75:      *
 76:      * @return string
 77:      */
 78:     public function getDeclaringClassName()
 79:     {
 80:         return $this->getDeclaringClass()->getName();
 81:     }
 82: 
 83:     /**
 84:      * Returns imported namespaces and aliases from the declaring namespace.
 85:      *
 86:      * @return array
 87:      */
 88:     public function getNamespaceAliases()
 89:     {
 90:         return $this->getDeclaringClass()->getNamespaceAliases();
 91:     }
 92: 
 93:     /**
 94:      * Checks if there is a particular annotation.
 95:      *
 96:      * @param string $name Annotation name
 97:      * @return boolean
 98:      */
 99:     public function hasAnnotation($name)
100:     {
101:         return false;
102:     }
103: 
104:     /**
105:      * Returns a particular annotation value.
106:      *
107:      * @param string $name Annotation name
108:      * @return null
109:      */
110:     public function getAnnotation($name)
111:     {
112:         return null;
113:     }
114: 
115:     /**
116:      * Returns parsed docblock.
117:      *
118:      * @return array
119:      */
120:     public function getAnnotations()
121:     {
122:         return array();
123:     }
124: 
125:     /**
126:      * Returns if the current reflection comes from a tokenized source.
127:      *
128:      * @return boolean
129:      */
130:     public function isTokenized()
131:     {
132:         return false;
133:     }
134: 
135:     /**
136:      * Returns the method prototype.
137:      *
138:      * @return \TokenReflection\Php\ReflectionMethod
139:      */
140:     public function getPrototype()
141:     {
142:         return self::create(parent::getPrototype(), $this->broker);
143:     }
144: 
145:     /**
146:      * Returns a particular parameter.
147:      *
148:      * @param integer|string $parameter Parameter name or position
149:      * @return \TokenReflection\Php\ReflectionParameter
150:      * @throws \TokenReflection\Exception\RuntimeException If there is no parameter of the given name.
151:      * @throws \TokenReflection\Exception\RuntimeException If there is no parameter at the given position.
152:      */
153:     public function getParameter($parameter)
154:     {
155:         $parameters = $this->getParameters();
156: 
157:         if (is_numeric($parameter)) {
158:             if (!isset($parameters[$parameter])) {
159:                 throw new Exception\RuntimeException(sprintf('There is no parameter at position "%d".', $parameter), Exception\RuntimeException::DOES_NOT_EXIST, $this);
160:             }
161: 
162:             return $parameters[$parameter];
163:         } else {
164:             foreach ($parameters as $reflection) {
165:                 if ($reflection->getName() === $parameter) {
166:                     return $reflection;
167:                 }
168:             }
169: 
170:             throw new Exception\RuntimeException(sprintf('There is no parameter "%s".', $parameter), Exception\RuntimeException::DOES_NOT_EXIST, $this);
171:         }
172:     }
173: 
174:     /**
175:      * Returns function parameters.
176:      *
177:      * @return array
178:      */
179:     public function getParameters()
180:     {
181:         if (null === $this->parameters) {
182:             $broker = $this->broker;
183:             $parent = $this;
184:             $this->parameters = array_map(function(InternalReflectionParameter $parameter) use ($broker, $parent) {
185:                 return ReflectionParameter::create($parameter, $broker, $parent);
186:             }, parent::getParameters());
187:         }
188: 
189:         return $this->parameters;
190:     }
191: 
192:     /**
193:      * Returns if the method is set accessible.
194:      *
195:      * @return boolean
196:      */
197:     public function isAccessible()
198:     {
199:         return $this->accessible;
200:     }
201: 
202:     /**
203:      * Sets a method to be accessible or not.
204:      *
205:      * Introduced in PHP 5.3.2. Throws an exception if run on an older version.
206:      *
207:      * @param boolean $accessible
208:      * @throws \TokenReflection\Exception\RuntimeException If run on PHP version < 5.3.2.
209:      */
210:     public function setAccessible($accessible)
211:     {
212:         if (PHP_VERSION_ID < 50302) {
213:             throw new Exception\RuntimeException(sprintf('Method setAccessible was introduced the internal reflection in PHP 5.3.2, you are using %s.', PHP_VERSION), Exception\RuntimeException::UNSUPPORTED, $this);
214:         }
215: 
216:         $this->accessible = $accessible;
217: 
218:         parent::setAccessible($accessible);
219:     }
220: 
221:     /**
222:      * Shortcut for isPublic(), ... methods that allows or-ed modifiers.
223:      *
224:      * @param integer $filter Filter
225:      * @return boolean
226:      */
227:     public function is($filter = null)
228:     {
229:         return null === $filter || ($this->getModifiers() & $filter);
230:     }
231: 
232:     /**
233:      * Returns the reflection broker used by this reflection object.
234:      *
235:      * @return \TokenReflection\Broker
236:      */
237:     public function getBroker()
238:     {
239:         return $this->broker;
240:     }
241: 
242:     /**
243:      * Magic __get method.
244:      *
245:      * @param string $key Variable name
246:      * @return mixed
247:      */
248:     final public function __get($key)
249:     {
250:         return TokenReflection\ReflectionElement::get($this, $key);
251:     }
252: 
253:     /**
254:      * Magic __isset method.
255:      *
256:      * @param string $key Variable name
257:      * @return boolean
258:      */
259:     final public function __isset($key)
260:     {
261:         return TokenReflection\ReflectionElement::exists($this, $key);
262:     }
263: 
264:     /**
265:      * Returns the function/method as closure.
266:      *
267:      * @param object $object Object
268:      * @return \Closure
269:      */
270:     public function getClosure($object)
271:     {
272:         if (PHP_VERSION >= 50400) {
273:             return parent::getClosure();
274:         } else {
275:             $that = $this;
276:             return function() use ($object, $that) {
277:                 return $that->invokeArgs($object, func_get_args());
278:             };
279:         }
280:     }
281: 
282:     /**
283:      * Returns the closure scope class.
284:      *
285:      * @return string|null
286:      */
287:     public function getClosureScopeClass()
288:     {
289:         return PHP_VERSION >= 50400 ? parent::getClosureScopeClass() : null;
290:     }
291: 
292:     /**
293:      * Returns this pointer bound to closure.
294:      *
295:      * @return null
296:      */
297:     public function getClosureThis()
298:     {
299:         return PHP_VERSION >= 50400 ? parent::getClosureThis() : null;
300:     }
301: 
302:     /**
303:      * Returns the original name when importing from a trait.
304:      *
305:      * @return string
306:      */
307:     public function getOriginalName()
308:     {
309:         return null;
310:     }
311: 
312:     /**
313:      * Returns the original method when importing from a trait.
314:      *
315:      * @return null
316:      */
317:     public function getOriginal()
318:     {
319:         return null;
320:     }
321: 
322:     /**
323:      * Returns the original modifiers value when importing from a trait.
324:      *
325:      * @return null
326:      */
327:     public function getOriginalModifiers()
328:     {
329:         return null;
330:     }
331: 
332:     /**
333:      * Returns the defining trait.
334:      *
335:      * @return \TokenReflection\IReflectionClass|null
336:      */
337:     public function getDeclaringTrait()
338:     {
339:         return null;
340:     }
341: 
342:     /**
343:      * Returns the declaring trait name.
344:      *
345:      * @return string|null
346:      */
347:     public function getDeclaringTraitName()
348:     {
349:         return null;
350:     }
351: 
352:     /**
353:      * Returns an element pretty (docblock compatible) name.
354:      *
355:      * @return string
356:      */
357:     public function getPrettyName()
358:     {
359:         return sprintf('%s::%s()', $this->getDeclaringClassName(), $this->getName());
360:     }
361: 
362:     /**
363:      * Creates a reflection instance.
364:      *
365:      * @param \ReflectionClass $internalReflection Internal reflection instance
366:      * @param \TokenReflection\Broker $broker Reflection broker instance
367:      * @return \TokenReflection\Php\IReflection
368:      * @throws \TokenReflection\Exception\RuntimeException If an invalid internal reflection object was provided.
369:      */
370:     public static function create(Reflector $internalReflection, Broker $broker)
371:     {
372:         static $cache = array();
373: 
374:         if (!$internalReflection instanceof InternalReflectionMethod) {
375:             throw new Exception\RuntimeException('Invalid reflection instance provided, ReflectionMethod expected.', Exception\RuntimeException::INVALID_ARGUMENT);
376:         }
377: 
378:         $key = $internalReflection->getDeclaringClass()->getName() . '::' . $internalReflection->getName();
379:         if (!isset($cache[$key])) {
380:             $cache[$key] = new self($internalReflection->getDeclaringClass()->getName(), $internalReflection->getName(), $broker);
381:         }
382: 
383:         return $cache[$key];
384:     }
385: }
386: 
PHP Token Reflection API documentation generated by ApiGen 2.8.0