示例#1
0
 public function testIfWillProperlyNameMethod()
 {
     $model = new ModelWithTargetMethod();
     $reflection = new ReflectionAnnotatedMethod($model, 'test');
     $name = ReflectionName::createName($reflection);
     $this->assertSame(ModelWithTargetMethod::class . '@test()', $name);
 }
示例#2
0
 /**
  * Check target constraints
  * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|bool $target Target entity
  * @param AnnotationsCollection $annotations
  * @return void
  * @throws ConflictException
  */
 public static function check($target, AnnotationsCollection $annotations)
 {
     if (!self::$_conflicts) {
         return;
     }
     foreach ($annotations->getAllAnnotations() as $annotation) {
         $name = AnnotationName::createName($annotation);
         if (!isset(self::$_conflicts[$name])) {
             continue;
         }
         $second = self::$_conflicts[$name];
         if ($annotations->hasAnnotation($second)) {
             throw new ConflictException(sprintf('Annotation `%s` cannot be used together with `%s` in `%s`', $name, $second, ReflectionName::createName($target)));
         }
     }
 }
示例#3
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));
     }
 }
示例#4
0
 /**
  * Get doc comment
  * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $reflection
  * @return mixed[]
  */
 private function parse($reflection)
 {
     $key = sprintf('%s@%s', $this->addendum->getInstanceId(), ReflectionName::createName($reflection));
     if (!isset(self::$cache[$key])) {
         //
         if (!CoarseChecker::mightHaveAnnotations($reflection)) {
             self::$cache[$key] = [];
             return self::$cache[$key];
         }
         $parser = new AnnotationsMatcher();
         $data = [];
         $parser->setPlugins(new MatcherConfig(['addendum' => $this->addendum, 'reflection' => $reflection]));
         $parser->matches($this->getDocComment($reflection), $data);
         self::$cache[$key] = $data;
     }
     return self::$cache[$key];
 }