public function testIfHasAnnotationFromImplementingInterface()
 {
     // Check interface first
     $info = new ReflectionAnnotatedClass(BaseInterfaceWithLabel::class);
     $has = $info->hasAnnotation('Label');
     $this->assertTrue($has);
     // Check inherited model model
     $model = new ModelWithInheritedLabelFromInterface();
     $info = new ReflectionAnnotatedClass($model);
     $has = $info->hasAnnotation('Label');
     $this->assertTrue($has);
 }
Example #2
0
 public function testIfHasAnnotationFromUsedTrait()
 {
     // Check trait first
     $info = new ReflectionAnnotatedClass(BaseTraitWithLabel::class);
     $has = $info->hasAnnotation('Label');
     $this->assertTrue($has);
     // Check inherited model model
     $model = new ModelWithInheritedLabelFromTrait();
     $info = new ReflectionAnnotatedClass($model);
     $has = $info->hasAnnotation('Label');
     $this->assertTrue($has);
 }
Example #3
0
 /**
  * Register annotation for later check
  * @param AnnotationInterface $annotation Annotation
  * @return void
  */
 public static function register(AnnotationInterface $annotation)
 {
     $name = AnnotationName::createName($annotation);
     $reflection = new ReflectionAnnotatedClass($annotation);
     if ($reflection->hasAnnotation('Conflicts')) {
         $value = $reflection->getAnnotation('Conflicts')->value;
         $values = is_array($value) ? $value : [$value];
         foreach ($values as $secondName) {
             self::$_conflicts[$name] = $secondName;
             self::$_conflicts[$secondName] = $name;
         }
     }
 }
Example #4
0
 /**
  * Get reflection class.
  * @param ReflectionAnnotatedClass|ReflectionAnnotatedProperty|ReflectionAnnotatedMethod $reflection Reflection
  * @return ReflectionAnnotatedClass
  * @throws Exception
  */
 public static function getReflectionClass($reflection)
 {
     if (null === $reflection) {
         throw new Exception(sprintf('No reflection class for matcher `%s`', get_class($this)));
     }
     if ($reflection instanceof ReflectionAnnotatedMethod) {
         return $reflection->getDeclaringClass();
     }
     if ($reflection instanceof ReflectionAnnotatedProperty) {
         return $reflection->getDeclaringClass();
     }
     return $reflection;
 }
Example #5
0
 public function testIfHasAnnotationFromParentClassParent()
 {
     // Check base model first
     $model = new BaseModelWithLabel();
     $info = new ReflectionAnnotatedClass($model);
     $has = $info->hasAnnotation('Label');
     $this->assertTrue($has);
     // Check inherited model model
     $model = new ModelWithInheritedLabelDeep();
     $info = new ReflectionAnnotatedClass($model);
     $has = $info->hasAnnotation('Label');
     $this->assertTrue($has);
 }
Example #6
0
 /**
  * Check target constraints
  * @param AnnotationInterface $annotation Annotation
  * @param ReflectionClass|ReflectionMethod|ReflectionProperty|bool $target
  * @return type
  * @throws TargetException
  */
 public static function check($annotation, $target)
 {
     $reflection = new ReflectionAnnotatedClass($annotation);
     if (!$reflection->hasAnnotation('Target')) {
         return;
     }
     $value = $reflection->getAnnotation('Target')->value;
     $values = is_array($value) ? $value : [$value];
     foreach ($values as $value) {
         if ($value == TargetAnnotation::TargetClass && $target instanceof ReflectionClass) {
             return;
         }
         if ($value == TargetAnnotation::TargetMethod && $target instanceof ReflectionMethod) {
             return;
         }
         if ($value == TargetAnnotation::TargetProperty && $target instanceof ReflectionProperty) {
             return;
         }
         if ($value == TargetAnnotation::TargetNested && $target === false) {
             return;
         }
     }
     if ($target !== false && $value && !in_array($value, [TargetAnnotation::TargetClass, TargetAnnotation::TargetMethod, TargetAnnotation::TargetProperty, TargetAnnotation::TargetNested])) {
         if ($target instanceof ReflectionClass) {
             $interfaceTarget = $target;
         } else {
             /* @var $target ReflectionProperty */
             $interfaceTarget = new ReflectionClass($target->class);
         }
         if (!ClassChecker::exists($value)) {
             throw new TargetException(sprintf('Annotation "%s" used in "%s" is only allowed on instances of "%s", but this class does not exists (see @Target)', basename($reflection->name), $interfaceTarget->name, $value));
         }
         if (!$interfaceTarget->implementsInterface($value)) {
             throw new TargetException(sprintf('Annotation "%s" used in "%s" is only allowed on instances of "%s" (see @Target)', basename($reflection->name), $interfaceTarget->name, $value));
         }
     }
     if ($target === false && $value == TargetAnnotation::TargetNested) {
         throw new TargetException("Annotation '" . get_class($annotation) . "' nesting not allowed");
     } elseif (in_array($value, TargetAnnotation::getTargets())) {
         throw new TargetException(sprintf("Annotation '%s' not allowed on %s, it's target is %s  (see @Target)", get_class($annotation), ReflectionName::createName($target), $value));
     }
 }