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, ReflectionParameter as InternalReflectionParameter, ReflectionFunctionAbstract as InternalReflectionFunctionAbstract;
 21: 
 22: /**
 23:  * Reflection of a not tokenized but defined method/function parameter.
 24:  *
 25:  * Descendant of the internal reflection with additional features.
 26:  */
 27: class ReflectionParameter extends InternalReflectionParameter implements IReflection, TokenReflection\IReflectionParameter
 28: {
 29:     /**
 30:      * Determined if the parameter (along with the function/method) is user defined.
 31:      *
 32:      * @var boolean
 33:      */
 34:     private $userDefined;
 35: 
 36:     /**
 37:      * Reflection broker.
 38:      *
 39:      * @var \TokenReflection\Broker
 40:      */
 41:     private $broker;
 42: 
 43:     /**
 44:      * Constructor.
 45:      *
 46:      * @param string|array $function Defining function/method
 47:      * @param string $paramName Parameter name
 48:      * @param \TokenReflection\Broker $broker Reflection broker
 49:      * @param \ReflectionFunctionAbstract $parent Parent reflection object
 50:      */
 51:     public function __construct($function, $paramName, Broker $broker, InternalReflectionFunctionAbstract $parent)
 52:     {
 53:         parent::__construct($function, $paramName);
 54:         $this->broker = $broker;
 55:         $this->userDefined = $parent->isUserDefined();
 56:     }
 57: 
 58:     /**
 59:      * Returns the declaring class reflection.
 60:      *
 61:      * @return \TokenReflection\IReflectionClass
 62:      */
 63:     public function getDeclaringClass()
 64:     {
 65:         $class = parent::getDeclaringClass();
 66:         return $class ? ReflectionClass::create($class, $this->broker) : null;
 67:     }
 68: 
 69:     /**
 70:      * Returns the declaring class name.
 71:      *
 72:      * @return string|null
 73:      */
 74:     public function getDeclaringClassName()
 75:     {
 76:         $class = parent::getDeclaringClass();
 77:         return $class ? $class->getName() : null;
 78:     }
 79: 
 80:     /**
 81:      * Returns imported namespaces and aliases from the declaring namespace.
 82:      *
 83:      * @return array
 84:      */
 85:     public function getNamespaceAliases()
 86:     {
 87:         return $this->getDeclaringFunction()->getNamespaceAliases();
 88:     }
 89: 
 90:     /**
 91:      * Returns the file name the reflection object is defined in.
 92:      *
 93:      * @return string
 94:      */
 95:     public function getFileName()
 96:     {
 97:         return $this->getDeclaringFunction()->getFileName();
 98:     }
 99: 
100:     /**
101:      * Returns the PHP extension reflection.
102:      *
103:      * @return \TokenReflection\Php\ReflectionExtension
104:      */
105:     public function getExtension()
106:     {
107:         return $this->getDeclaringFunction()->getExtension();
108:     }
109: 
110:     /**
111:      * Returns the PHP extension name.
112:      *
113:      * @return string|boolean
114:      */
115:     public function getExtensionName()
116:     {
117:         $extension = $this->getExtension();
118:         return $extension ? $extension->getName() : false;
119:     }
120: 
121:     /**
122:      * Checks if there is a particular annotation.
123:      *
124:      * @param string $name Annotation name
125:      * @return boolean
126:      */
127:     public function hasAnnotation($name)
128:     {
129:         return false;
130:     }
131: 
132:     /**
133:      * Returns a particular annotation value.
134:      *
135:      * @param string $name Annotation name
136:      * @return null
137:      */
138:     public function getAnnotation($name)
139:     {
140:         return null;
141:     }
142: 
143:     /**
144:      * Returns parsed docblock.
145:      *
146:      * @return array
147:      */
148:     public function getAnnotations()
149:     {
150:         return array();
151:     }
152: 
153:     /**
154:      * Returns the declaring function reflection.
155:      *
156:      * @return \TokenReflection\Php\ReflectionFunction|\TokenReflection\Php\ReflectionMethod
157:      */
158:     public function getDeclaringFunction()
159:     {
160:         $class = $this->getDeclaringClass();
161:         $function = parent::getDeclaringFunction();
162: 
163:         return $class ? $class->getMethod($function->getName()) : ReflectionFunction::create($function, $this->broker);
164:     }
165: 
166:     /**
167:      * Returns the declaring function name.
168:      *
169:      * @return string|null
170:      */
171:     public function getDeclaringFunctionName()
172:     {
173:         $function = parent::getDeclaringFunction();
174:         return $function ? $function->getName() : $function;
175:     }
176: 
177:     /**
178:      * Returns the definition start line number in the file.
179:      *
180:      * @return null
181:      */
182:     public function getStartLine()
183:     {
184:         return null;
185:     }
186: 
187:     /**
188:      * Returns the definition end line number in the file.
189:      *
190:      * @return null
191:      */
192:     public function getEndLine()
193:     {
194:         return null;
195:     }
196: 
197:     /**
198:      * Returns the appropriate docblock definition.
199:      *
200:      * @return boolean
201:      */
202:     public function getDocComment()
203:     {
204:         return false;
205:     }
206: 
207:     /**
208:      * Returns the part of the source code defining the paramter default value.
209:      *
210:      * @return string|null
211:      */
212:     public function getDefaultValueDefinition()
213:     {
214:         $value = $this->getDefaultValue();
215:         return null === $value ? null : var_export($value, true);
216:     }
217: 
218:     /**
219:      * Returns if the parameter expects a callback.
220:      *
221:      * @return boolean
222:      */
223:     public function isCallable()
224:     {
225:         return PHP_VERSION >= 50400 && parent::isCallable();
226:     }
227: 
228:     /**
229:      * Returns the original type hint as defined in the source code.
230:      *
231:      * @return string|null
232:      */
233:     public function getOriginalTypeHint()
234:     {
235:         return !$this->isArray() && !$this->isCallable() ? $this->getClass() : null;
236:     }
237: 
238:     /**
239:      * Returns the required class name of the value.
240:      *
241:      * @return string|null
242:      */
243:     public function getClassName()
244:     {
245:         return $this->getClass() ? $this->getClass()->getName() : null;
246:     }
247: 
248:     /**
249:      * Returns if the parameter is internal.
250:      *
251:      * @return boolean
252:      */
253:     public function isInternal()
254:     {
255:         return !$this->userDefined;
256:     }
257: 
258:     /**
259:      * Returns if the parameter is user defined.
260:      *
261:      * @return boolean
262:      */
263:     public function isUserDefined()
264:     {
265:         return $this->userDefined;
266:     }
267: 
268:     /**
269:      * Returns if the current reflection comes from a tokenized source.
270:      *
271:      * @return boolean
272:      */
273:     public function isTokenized()
274:     {
275:         return false;
276:     }
277: 
278:     /**
279:      * Returns if the reflection subject is deprecated.
280:      *
281:      * @return boolean
282:      */
283:     public function isDeprecated()
284:     {
285:         return false;
286:     }
287: 
288:     /**
289:      * Returns the reflection broker used by this reflection object.
290:      *
291:      * @return \TokenReflection\Broker
292:      */
293:     public function getBroker()
294:     {
295:         return $this->broker;
296:     }
297: 
298:     /**
299:      * Returns if the paramter value can be passed by value.
300:      *
301:      * @return boolean
302:      */
303:     public function canBePassedByValue()
304:     {
305:         return method_exists($this, 'canBePassedByValue') ? parent::canBePassedByValue() : !$this->isPassedByReference();
306:     }
307: 
308:     /**
309:      * Returns an element pretty (docblock compatible) name.
310:      *
311:      * @return string
312:      */
313:     public function getPrettyName()
314:     {
315:         return str_replace('()', '($' . $this->getName() . ')', $this->getDeclaringFunction()->getPrettyName());
316:     }
317: 
318:     /**
319:      * Magic __get method.
320:      *
321:      * @param string $key Variable name
322:      * @return mixed
323:      */
324:     final public function __get($key)
325:     {
326:         return TokenReflection\ReflectionElement::get($this, $key);
327:     }
328: 
329:     /**
330:      * Magic __isset method.
331:      *
332:      * @param string $key Variable name
333:      * @return boolean
334:      */
335:     final public function __isset($key)
336:     {
337:         return TokenReflection\ReflectionElement::exists($this, $key);
338:     }
339: 
340:     /**
341:      * Creates a reflection instance.
342:      *
343:      * @param \ReflectionClass $internalReflection Internal reflection instance
344:      * @param \TokenReflection\Broker $broker Reflection broker instance
345:      * @return \TokenReflection\Php\ReflectionParameter
346:      * @throws \TokenReflection\Exception\RuntimeException If an invalid internal reflection object was provided.
347:      */
348:     public static function create(Reflector $internalReflection, Broker $broker)
349:     {
350:         static $cache = array();
351: 
352:         if (!$internalReflection instanceof InternalReflectionParameter) {
353:             throw new Exception\RuntimeException('Invalid reflection instance provided, ReflectionParameter expected.', Exception\RuntimeException::INVALID_ARGUMENT);
354:         }
355: 
356:         $class = $internalReflection->getDeclaringClass();
357:         $function = $internalReflection->getDeclaringFunction();
358: 
359:         $key = $class ? $class->getName() . '::' : '';
360:         $key .= $function->getName() . '(' . $internalReflection->getName() . ')';
361: 
362:         if (!isset($cache[$key])) {
363:             $cache[$key] = new self($class ? array($class->getName(), $function->getName()) : $function->getName(), $internalReflection->getName(), $broker, $function);
364:         }
365: 
366:         return $cache[$key];
367:     }
368: }
369: 
PHP Token Reflection API documentation generated by ApiGen 2.8.0