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, ReflectionClass as InternalReflectionClass, ReflectionProperty as InternalReflectionProperty, ReflectionMethod as InternalReflectionMethod;
 21: 
 22: /**
 23:  * Reflection of a not tokenized but defined class.
 24:  *
 25:  * Descendant of the internal reflection with additional features.
 26:  */
 27: class ReflectionClass extends InternalReflectionClass implements IReflection, TokenReflection\IReflectionClass
 28: {
 29:     /**
 30:      * Reflection broker.
 31:      *
 32:      * @var \TokenReflection\Broker
 33:      */
 34:     private $broker;
 35: 
 36:     /**
 37:      * Implemented interface reflections.
 38:      *
 39:      * @var array
 40:      */
 41:     private $interfaces;
 42: 
 43:     /**
 44:      * Metod reflections.
 45:      *
 46:      * @var array
 47:      */
 48:     private $methods;
 49: 
 50:     /**
 51:      * Constant reflections.
 52:      *
 53:      * @var array
 54:      */
 55:     private $constants;
 56: 
 57:     /**
 58:      * Property reflections.
 59:      *
 60:      * @var array
 61:      */
 62:     private $properties;
 63: 
 64:     /**
 65:      * Constructor.
 66:      *
 67:      * @param string $className Class name
 68:      * @param \TokenReflection\Broker $broker Reflection broker
 69:      */
 70:     public function __construct($className, Broker $broker)
 71:     {
 72:         parent::__construct($className);
 73:         $this->broker = $broker;
 74:     }
 75: 
 76:     /**
 77:      * Returns the PHP extension reflection.
 78:      *
 79:      * @return \TokenReflection\Php\ReflectionExtension
 80:      */
 81:     public function getExtension()
 82:     {
 83:         return ReflectionExtension::create(parent::getExtension(), $this->broker);
 84:     }
 85: 
 86:     /**
 87:      * Checks if there is a particular annotation.
 88:      *
 89:      * @param string $name Annotation name
 90:      * @return boolean
 91:      */
 92:     public function hasAnnotation($name)
 93:     {
 94:         return false;
 95:     }
 96: 
 97:     /**
 98:      * Returns a particular annotation value.
 99:      *
100:      * @param string $name Annotation name
101:      * @return null
102:      */
103:     public function getAnnotation($name)
104:     {
105:         return null;
106:     }
107: 
108:     /**
109:      * Returns parsed docblock.
110:      *
111:      * @return array
112:      */
113:     public function getAnnotations()
114:     {
115:         return array();
116:     }
117: 
118:     /**
119:      * Returns if the class is an exception or its descendant.
120:      *
121:      * @return boolean
122:      */
123:     public function isException()
124:     {
125:         return 'Exception' === $this->getName() || $this->isSubclassOf('Exception');
126:     }
127: 
128:     /**
129:      * Returns if objects of this class are cloneable.
130:      *
131:      * Introduced in PHP 5.4.
132:      *
133:      * @return boolean
134:      * @see http://svn.php.net/viewvc/php/php-src/trunk/ext/reflection/php_reflection.c?revision=307971&view=markup#l4059
135:      */
136:     public function isCloneable()
137:     {
138:         if ($this->isInterface() || $this->isAbstract()) {
139:             return false;
140:         }
141: 
142:         $methods = $this->getMethods();
143:         return isset($methods['__clone']) ? $methods['__clone']->isPublic() : true;
144:     }
145: 
146:     /**
147:      * Returns if the current reflection comes from a tokenized source.
148:      *
149:      * @return boolean
150:      */
151:     public function isTokenized()
152:     {
153:         return false;
154:     }
155: 
156:     /**
157:      * Returns if the reflection subject is deprecated.
158:      *
159:      * @return boolean
160:      */
161:     public function isDeprecated()
162:     {
163:         return false;
164:     }
165: 
166:     /**
167:      * Returns if the current class is a subclass of the given class.
168:      *
169:      * @param string|object $class Class name or reflection object
170:      * @return boolean
171:      * @throws \TokenReflection\Exception\RuntimeException If an invalid parameter was provided.
172:      */
173:     public function isSubclassOf($class)
174:     {
175:         if (is_object($class)) {
176:             if (!$class instanceof InternalReflectionClass && !$class instanceof IReflectionClass) {
177:                 throw new Exception\RuntimeException('Parameter must be a string or an instance of class reflection.', Exception\RuntimeException::INVALID_ARGUMENT, $this);
178:             }
179: 
180:             $class = $class->getName();
181:         }
182: 
183:         return in_array($class, $this->getParentClassNameList());
184:     }
185: 
186:     /**
187:      * Returns parent class reflection.
188:      *
189:      * @return \TokenReflection\Php\ReflectionClass
190:      */
191:     public function getParentClass()
192:     {
193:         $parent = parent::getParentClass();
194:         return $parent ? self::create($parent, $this->broker) : null;
195:     }
196: 
197:     /**
198:      * Returns the parent class name.
199:      *
200:      * @return string
201:      */
202:     public function getParentClassName()
203:     {
204:         $parent = $this->getParentClass();
205:         return $parent ? $parent->getName() : null;
206:     }
207: 
208:     /**
209:      * Returns the parent classes reflections.
210:      *
211:      * @return array
212:      */
213:     public function getParentClasses()
214:     {
215:         $broker = $this->broker;
216:         return array_map(function($className) use ($broker) {
217:             return $broker->getClass($className);
218:         }, $this->getParentClassNameList());
219:     }
220: 
221:     /**
222:      * Returns the parent classes names.
223:      *
224:      * @return array
225:      */
226:     public function getParentClassNameList()
227:     {
228:         return class_parents($this->getName());
229:     }
230: 
231:     /**
232:      * Returns if the class implements the given interface.
233:      *
234:      * @param string|object $interface Interface name or reflection object
235:      * @return boolean
236:      * @throws \TokenReflection\Exception\RuntimeException If the provided parameter is not an interface.
237:      */
238:     public function implementsInterface($interface)
239:     {
240:         if (is_object($interface)) {
241:             if (!$interface instanceof InternalReflectionClass && !$interface instanceof IReflectionClass) {
242:                 throw new Exception\RuntimeException('Parameter must be a string or an instance of class reflection.', Exception\RuntimeException::INVALID_ARGUMENT, $this);
243:             }
244: 
245:             $interfaceName = $interface->getName();
246: 
247:             if (!$interface->isInterface()) {
248:                 throw new Exception\RuntimeException(sprintf('"%s" is not an interface.', $interfaceName), Exception\RuntimeException::INVALID_ARGUMENT, $this);
249:             }
250:         } else {
251:             $reflection = $this->getBroker()->getClass($interface);
252:             if (!$reflection->isInterface()) {
253:                 throw new Exception\RuntimeException(sprintf('"%s" is not an interface.', $interface), Exception\RuntimeException::INVALID_ARGUMENT, $this);
254:             }
255: 
256:             $interfaceName = $interface;
257:         }
258: 
259:         $interfaces = $this->getInterfaces();
260:         return isset($interfaces[$interfaceName]);
261:     }
262: 
263:     /**
264:      * Returns an array of interface reflections.
265:      *
266:      * @return array
267:      */
268:     public function getInterfaces()
269:     {
270:         if (null === $this->interfaces) {
271:             $broker = $this->broker;
272:             $interfaceNames = $this->getInterfaceNames();
273: 
274:             if (empty($interfaceNames)) {
275:                 $this->interfaces = array();
276:             } else {
277:                 $this->interfaces = array_combine($interfaceNames, array_map(function($interfaceName) use ($broker) {
278:                     return $broker->getClass($interfaceName);
279:                 }, $interfaceNames));
280:             }
281:         }
282: 
283:         return $this->interfaces;
284:     }
285: 
286:     /**
287:      * Returns interfaces implemented by this class, not its parents.
288:      *
289:      * @return array
290:      */
291:     public function getOwnInterfaces()
292:     {
293:         $parent = $this->getParentClass();
294:         return $parent ? array_diff_key($this->getInterfaces(), $parent->getInterfaces()) : $this->getInterfaces();
295:     }
296: 
297:     /**
298:      * Returns names of interfaces implemented by this class, not its parents.
299:      *
300:      * @return array
301:      */
302:     public function getOwnInterfaceNames()
303:     {
304:         return array_keys($this->getOwnInterfaces());
305:     }
306: 
307:     /**
308:      * Returns class constructor reflection.
309:      *
310:      * @return \TokenReflection\Php\ReflectionClass|null
311:      */
312:     public function getConstructor()
313:     {
314:         return ReflectionMethod::create(parent::getConstructor(), $this->broker);
315:     }
316: 
317:     /**
318:      * Returns class desctructor reflection.
319:      *
320:      * @return \TokenReflection\Php\ReflectionClass|null
321:      */
322:     public function getDestructor()
323:     {
324:         foreach ($this->getMethods() as $method) {
325:             if ($method->isDestructor()) {
326:                 return $method;
327:             }
328:         }
329: 
330:         return null;
331:     }
332: 
333:     /**
334:      * Returns a particular method reflection.
335:      *
336:      * @param string $name Method name
337:      * @return \TokenReflection\Php\ReflectionMethod
338:      * @throws \TokenReflection\Exception\RuntimeException If the requested method does not exist.
339:      */
340:     public function getMethod($name)
341:     {
342:         foreach ($this->getMethods() as $method) {
343:             if ($method->getName() === $name) {
344:                 return $method;
345:             }
346:         }
347: 
348:         throw new Exception\RuntimeException(sprintf('Method %s does not exist.', $name), Exception\RuntimeException::DOES_NOT_EXIST, $this);
349:     }
350: 
351:     /**
352:      * Returns class methods.
353:      *
354:      * @param integer $filter Methods filter
355:      * @return array
356:      */
357:     public function getMethods($filter = null)
358:     {
359:         if (null === $this->methods) {
360:             $broker = $this->broker;
361:             $this->methods = array_map(function(InternalReflectionMethod $method) use ($broker) {
362:                 return ReflectionMethod::create($method, $broker);
363:             }, parent::getMethods());
364:         }
365: 
366:         if (null === $filter) {
367:             return $this->methods;
368:         }
369: 
370:         return array_filter($this->methods, function(ReflectionMethod $method) use ($filter) {
371:             return (bool) ($method->getModifiers() & $filter);
372:         });
373:     }
374: 
375:     /**
376:      * Returns if the class implements (and not its parents) the given method.
377:      *
378:      * @param string $name Method name
379:      * @return boolean
380:      */
381:     public function hasOwnMethod($name)
382:     {
383:         foreach ($this->getOwnMethods() as $method) {
384:             if ($name === $method->getName()) {
385:                 return true;
386:             }
387:         }
388: 
389:         return false;
390:     }
391: 
392:     /**
393:      * Returns methods declared by this class, not its parents.
394:      *
395:      * @param integer $filter
396:      * @return array
397:      */
398:     public function getOwnMethods($filter = null)
399:     {
400:         $me = $this->getName();
401:         return array_filter($this->getMethods($filter), function(ReflectionMethod $method) use ($me) {
402:             return $method->getDeclaringClass()->getName() === $me;
403:         });
404:     }
405: 
406:     /**
407:      * Returns if the class imports the given method from traits.
408:      *
409:      * @param string $name Method name
410:      * @return boolean
411:      * @todo Impossible with the current status of reflection
412:      */
413:     public function hasTraitMethod($name)
414:     {
415:         return false;
416:     }
417: 
418:     /**
419:      * Returns method reflections imported from traits.
420:      *
421:      * @param integer $filter Methods filter
422:      * @return array
423:      * @todo Impossible with the current status of reflection
424:      */
425:     public function getTraitMethods($filter = null)
426:     {
427:         return array();
428:     }
429: 
430:     /**
431:      * Returns a constant reflection.
432:      *
433:      * @param string $name Constant name
434:      * @return \TokenReflection\ReflectionConstant
435:      * @throws \TokenReflection\Exception\RuntimeException If the requested constant does not exist.
436:      */
437:     public function getConstantReflection($name)
438:     {
439:         if ($this->hasConstant($name)) {
440:             return new ReflectionConstant($name, $this->getConstant($name), $this->broker, $this);
441:         }
442: 
443:         throw new Exception\RuntimeException(sprintf('Constant "%s" does not exist.', $name), Exception\RuntimeException::DOES_NOT_EXIST, $this);
444:     }
445: 
446:     /**
447:      * Returns an array of constant reflections.
448:      *
449:      * @return array
450:      */
451:     public function getConstantReflections()
452:     {
453:         if (null === $this->constants) {
454:             $this->constants = array();
455:             foreach ($this->getConstants() as $name => $value) {
456:                 $this->constants[$name] = $this->getConstantReflection($name);
457:             }
458:         }
459: 
460:         return array_values($this->constants);
461:     }
462: 
463:     /**
464:      * Returns if the class (and not its parents) defines the given constant.
465:      *
466:      * @param string $name Constant name.
467:      * @return boolean
468:      */
469:     public function hasOwnConstant($name)
470:     {
471:         $constants = $this->getOwnConstants();
472:         return isset($constants[$name]);
473:     }
474: 
475:     /**
476:      * Returns constants declared by this class, not its parents.
477:      *
478:      * @return array
479:      */
480:     public function getOwnConstants()
481:     {
482:         return array_diff_assoc($this->getConstants(), $this->getParentClass() ? $this->getParentClass()->getConstants() : array());
483:     }
484: 
485:     /**
486:      * Returns an array of constant reflections defined by this class and not its parents.
487:      *
488:      * @return array
489:      */
490:     public function getOwnConstantReflections()
491:     {
492:         $constants = array();
493:         foreach ($this->getOwnConstants() as $name => $value) {
494:             $constants[] = $this->getConstantReflection($name);
495:         }
496:         return $constants;
497:     }
498: 
499:     /**
500:      * Returns a particular property reflection.
501:      *
502:      * @param string $name Property name
503:      * @return \TokenReflection\Php\ReflectionProperty
504:      * @throws \TokenReflection\Exception\RuntimeException If the requested property does not exist.
505:      */
506:     public function getProperty($name)
507:     {
508:         foreach ($this->getProperties() as $property) {
509:             if ($name === $property->getName()) {
510:                 return $property;
511:             }
512:         }
513: 
514:         throw new Exception\RuntimeException(sprintf('Property %s does not exist.', $name), Exception\RuntimeException::DOES_NOT_EXIST, $this);
515:     }
516: 
517:     /**
518:      * Returns class properties.
519:      *
520:      * @param integer $filter Properties filter
521:      * @return array
522:      */
523:     public function getProperties($filter = null)
524:     {
525:         if (null === $this->properties) {
526:             $broker = $this->broker;
527:             $this->properties = array_map(function(InternalReflectionProperty $property) use ($broker) {
528:                 return ReflectionProperty::create($property, $broker);
529:             }, parent::getProperties());
530:         }
531: 
532:         if (null === $filter) {
533:             return $this->properties;
534:         }
535: 
536:         return array_filter($this->properties, function(ReflectionProperty $property) use ($filter) {
537:             return (bool) ($property->getModifiers() & $filter);
538:         });
539:     }
540: 
541:     /**
542:      * Returns if the class has (and not its parents) the given property.
543:      *
544:      * @param string $name Property name
545:      * @return boolean
546:      */
547:     public function hasOwnProperty($name)
548:     {
549:         foreach ($this->getOwnProperties() as $property) {
550:             if ($name === $property->getName()) {
551:                 return true;
552:             }
553:         }
554: 
555:         return false;
556:     }
557: 
558:     /**
559:      * Returns properties declared by this class, not its parents.
560:      *
561:      * @param integer $filter
562:      * @return array
563:      */
564:     public function getOwnProperties($filter = null)
565:     {
566:         $me = $this->getName();
567:         return array_filter($this->getProperties($filter), function(ReflectionProperty $property) use ($me) {
568:             return $property->getDeclaringClass()->getName() === $me;
569:         });
570:     }
571: 
572:     /**
573:      * Returns if the class imports the given property from traits.
574:      *
575:      * @param string $name Property name
576:      * @return boolean
577:      * @todo Impossible with the current status of reflection
578:      */
579:     public function hasTraitProperty($name)
580:     {
581:         return false;
582:     }
583: 
584:     /**
585:      * Returns property reflections imported from traits.
586:      *
587:      * @param integer $filter Properties filter
588:      * @return array
589:      * @todo Impossible with the current status of reflection
590:      */
591:     public function getTraitProperties($filter = null)
592:     {
593:         return array();
594:     }
595: 
596:     /**
597:      * Returns static properties reflections.
598:      *
599:      * @return array
600:      */
601:     public function getStaticProperties()
602:     {
603:         return $this->getProperties(InternalReflectionProperty::IS_STATIC);
604:     }
605: 
606:     /**
607:      * Returns reflections of direct subclasses.
608:      *
609:      * @return array
610:      */
611:     public function getDirectSubclasses()
612:     {
613:         $that = $this->name;
614:         return array_filter($this->getBroker()->getClasses(Broker\Backend::INTERNAL_CLASSES | Broker\Backend::TOKENIZED_CLASSES), function(IReflectionClass $class) use ($that) {
615:             if (!$class->isSubclassOf($that)) {
616:                 return false;
617:             }
618: 
619:             return null === $class->getParentClassName() || !$class->getParentClass()->isSubClassOf($that);
620:         });
621:     }
622: 
623:     /**
624:      * Returns names of direct subclasses.
625:      *
626:      * @return array
627:      */
628:     public function getDirectSubclassNames()
629:     {
630:         return array_keys($this->getDirectSubclasses());
631:     }
632: 
633:     /**
634:      * Returns reflections of indirect subclasses.
635:      *
636:      * @return array
637:      */
638:     public function getIndirectSubclasses()
639:     {
640:         $that = $this->name;
641:         return array_filter($this->getBroker()->getClasses(Broker\Backend::INTERNAL_CLASSES | Broker\Backend::TOKENIZED_CLASSES), function(IReflectionClass $class) use ($that) {
642:             if (!$class->isSubclassOf($that)) {
643:                 return false;
644:             }
645: 
646:             return null !== $class->getParentClassName() && $class->getParentClass()->isSubClassOf($that);
647:         });
648:     }
649: 
650:     /**
651:      * Returns names of indirect subclasses.
652:      *
653:      * @return array
654:      */
655:     public function getIndirectSubclassNames()
656:     {
657:         return array_keys($this->getIndirectSubclasses());
658:     }
659: 
660:     /**
661:      * Returns reflections of classes directly implementing this interface.
662:      *
663:      * @return array
664:      */
665:     public function getDirectImplementers()
666:     {
667:         if (!$this->isInterface()) {
668:             return array();
669:         }
670: 
671:         $that = $this->name;
672:         return array_filter($this->getBroker()->getClasses(Broker\Backend::INTERNAL_CLASSES | Broker\Backend::TOKENIZED_CLASSES), function(IReflectionClass $class) use ($that) {
673:             if (!$class->implementsInterface($that)) {
674:                 return false;
675:             }
676: 
677:             return null === $class->getParentClassName() || !$class->getParentClass()->implementsInterface($that);
678:         });
679:     }
680: 
681:     /**
682:      * Returns names of classes directly implementing this interface.
683:      *
684:      * @return array
685:      */
686:     public function getDirectImplementerNames()
687:     {
688:         return array_keys($this->getDirectImplementers());
689:     }
690: 
691:     /**
692:      * Returns reflections of classes indirectly implementing this interface.
693:      *
694:      * @return array
695:      */
696:     public function getIndirectImplementers()
697:     {
698:         if (!$this->isInterface()) {
699:             return array();
700:         }
701: 
702:         $that = $this->name;
703:         return array_filter($this->getBroker()->getClasses(Broker\Backend::INTERNAL_CLASSES | Broker\Backend::TOKENIZED_CLASSES), function(IReflectionClass $class) use ($that) {
704:             if (!$class->implementsInterface($that)) {
705:                 return false;
706:             }
707: 
708:             return null !== $class->getParentClassName() && $class->getParentClass()->implementsInterface($that);
709:         });
710:     }
711: 
712:     /**
713:      * Returns names of classes indirectly implementing this interface.
714:      *
715:      * @return array
716:      */
717:     public function getIndirectImplementerNames()
718:     {
719:         return array_keys($this->getIndirectImplementers());
720:     }
721: 
722:     /**
723:      * Returns if the class definition is complete.
724:      *
725:      * Internal classes always have the definition complete.
726:      *
727:      * @return boolean
728:      */
729:     public function isComplete()
730:     {
731:         return true;
732:     }
733: 
734:     /**
735:      * Returns if the class definition is valid.
736:      *
737:      * Internal classes are always valid.
738:      *
739:      * @return boolean
740:      */
741:     public function isValid()
742:     {
743:         return true;
744:     }
745: 
746:     /**
747:      * Returns imported namespaces and aliases from the declaring namespace.
748:      *
749:      * @return array
750:      */
751:     public function getNamespaceAliases()
752:     {
753:         return array();
754:     }
755: 
756:     /**
757:      * Returns the reflection broker used by this reflection object.
758:      *
759:      * @return \TokenReflection\Broker
760:      */
761:     public function getBroker()
762:     {
763:         return $this->broker;
764:     }
765: 
766:     /**
767:      * Magic __get method.
768:      *
769:      * @param string $key Variable name
770:      * @return mixed
771:      */
772:     final public function __get($key)
773:     {
774:         return TokenReflection\ReflectionElement::get($this, $key);
775:     }
776: 
777:     /**
778:      * Magic __isset method.
779:      *
780:      * @param string $key Variable name
781:      * @return boolean
782:      */
783:     final public function __isset($key)
784:     {
785:         return TokenReflection\ReflectionElement::exists($this, $key);
786:     }
787: 
788:     /**
789:      * Returns traits used by this class.
790:      *
791:      * @return array
792:      */
793:     public function getTraits()
794:     {
795:         return NATIVE_TRAITS ? parent::getTraits() : array();
796:     }
797: 
798:     /**
799:      * Returns traits used by this class and not its parents.
800:      *
801:      * @return array
802:      */
803:     public function getOwnTraits()
804:     {
805:         if (!NATIVE_TRAITS) {
806:             return array();
807:         }
808: 
809:         $parent = $this->getParentClass();
810:         return $parent ? array_diff_key($this->getTraits(), $parent->getTraits()) : $this->getTraits();
811:     }
812: 
813:     /**
814:      * Returns names of used traits.
815:      *
816:      * @return array
817:      */
818:     public function getTraitNames()
819:     {
820:         return NATIVE_TRAITS ? parent::getTraitNames() : array();
821:     }
822: 
823:     /**
824:      * Returns traits used by this class and not its parents.
825:      *
826:      * @return array
827:      */
828:     public function getOwnTraitNames()
829:     {
830:         return array_keys($this->getOwnTraits());
831:     }
832: 
833:     /**
834:      * Returns method aliases from traits.
835:      *
836:      * @return array
837:      */
838:     public function getTraitAliases()
839:     {
840:         return NATIVE_TRAITS ? parent::getTraitAliases() : array();
841:     }
842: 
843:     /**
844:      * Returns if the class is a trait.
845:      *
846:      * @return boolean
847:      */
848:     public function isTrait()
849:     {
850:         return NATIVE_TRAITS && parent::isTrait();
851:     }
852: 
853:     /**
854:      * Returns if the class uses a particular trait.
855:      *
856:      * @param \ReflectionClass|\TokenReflection\IReflectionClass|string $trait Trait reflection or name
857:      * @return boolean
858:      * @throws \TokenReflection\Exception\RuntimeException If an invalid parameter was provided.
859:      */
860:     public function usesTrait($trait)
861:     {
862:         if (is_object($trait)) {
863:             if (!$trait instanceof InternalReflectionClass && !$trait instanceof TokenReflection\IReflectionClass) {
864:                 throw new Exception\RuntimeException('Parameter must be a string or an instance of trait reflection.', Exception\RuntimeException::INVALID_ARGUMENT, $this);
865:             }
866: 
867:             $traitName = $trait->getName();
868: 
869:             if (!$trait->isTrait()) {
870:                 throw new Exception\RuntimeException(sprintf('"%s" is not a trait.', $traitName), Exception\RuntimeException::INVALID_ARGUMENT, $this);
871:             }
872:         } else {
873:             $reflection = $this->getBroker()->getClass($trait);
874:             if (!$reflection->isTrait()) {
875:                 throw new Exception\RuntimeException(sprintf('"%s" is not a trait.', $trait), Exception\RuntimeException::INVALID_ARGUMENT, $this);
876:             }
877: 
878:             $traitName = $trait;
879:         }
880: 
881:         return in_array($traitName, $this->getTraitNames());
882:     }
883: 
884:     /**
885:      * Creates a new class instance without using a constructor.
886:      *
887:      * @return object
888:      * @throws \TokenReflection\Exception\RuntimeException If the class inherits from an internal class.
889:      */
890:     public function newInstanceWithoutConstructor()
891:     {
892:         if ($this->isInternal()) {
893:             throw new Exception\RuntimeException('Could not create an instance; only user defined classes can be instantiated.', Exception\RuntimeException::UNSUPPORTED, $this);
894:         }
895: 
896:         foreach ($this->getParentClasses() as $parent) {
897:             if ($parent->isInternal()) {
898:                 throw new Exception\RuntimeException('Could not create an instance; only user defined classes can be instantiated.', Exception\RuntimeException::UNSUPPORTED, $this);
899:             }
900:         }
901: 
902:         if (PHP_VERSION_ID >= 50400) {
903:             return parent::newInstanceWithoutConstructor();
904:         }
905: 
906:         return unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->getName()), $this->getName()));
907:     }
908: 
909:     /**
910:      * Returns an element pretty (docblock compatible) name.
911:      *
912:      * @return string
913:      */
914:     public function getPrettyName()
915:     {
916:         return $this->getName();
917:     }
918: 
919:     /**
920:      * Creates a reflection instance.
921:      *
922:      * @param \ReflectionClass $internalReflection Internal reflection instance
923:      * @param \TokenReflection\Broker $broker Reflection broker instance
924:      * @return \TokenReflection\Php\ReflectionClass
925:      * @throws \TokenReflection\Exception\RuntimeException If an invalid internal reflection object was provided.
926:      */
927:     public static function create(Reflector $internalReflection, Broker $broker)
928:     {
929:         if (!$internalReflection instanceof InternalReflectionClass) {
930:             throw new Exception\RuntimeException('Invalid reflection instance provided, ReflectionClass expected.', Exception\RuntimeException::INVALID_ARGUMENT);
931:         }
932: 
933:         return $broker->getClass($internalReflection->getName());
934:     }
935: }
936: 
PHP Token Reflection API documentation generated by ApiGen 2.8.0