Passed
Push — develop ( ded98c...5fca67 )
by Mathieu
02:55
created

MissingAnnotationArgumentException   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 25
c 1
b 0
f 0
dl 0
loc 54
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 44 7
1
<?php
2
3
namespace KunicMarko\SonataAnnotationBundle\Exception;
4
5
use InvalidArgumentException;
6
use KunicMarko\SonataAnnotationBundle\Annotation\AnnotationInterface;
7
use ReflectionClass;
8
9
/**
10
 * A mandatory argument is missing on an annotation.
11
 */
12
class MissingAnnotationArgumentException extends InvalidArgumentException
13
{
14
15
    /**
16
     * @param AnnotationInterface|string  $annotation   Annotation object or
17
     *                                                  class.
18
     * @param string                      $argumentName Argument name.
19
     * @param ReflectionClass|string|null $class        Class using the
20
     *                                                  annotation.
21
     */
22
    public function __construct(
23
      $annotation,
24
      string $argumentName,
25
      $class = null
26
    ) {
27
        if ($annotation instanceof AnnotationInterface) {
28
            $annotation = get_class($annotation);
29
        }
30
31
        if ($class instanceof ReflectionClass) {
32
            $class = $class->getName();
33
        }
34
35
        if (!is_string($annotation)) {
0 ignored issues
show
introduced by
The condition is_string($annotation) is always true.
Loading history...
36
            throw new InvalidArgumentException(
37
              sprintf(
38
                'Invalid annotation (string|%s) attribute.',
39
                AnnotationInterface::class,
40
              )
41
            );
42
        }
43
44
        if ($class !== null && !is_string($class)) {
45
            throw new InvalidArgumentException(
46
              'Invalid class (string|ReflectionClass|null) attribute.',
47
            );
48
        }
49
50
        if ($class) {
51
            $message = sprintf(
52
              'Argument "%s" is mandatory for annotation %s on %s.',
53
              $argumentName,
54
              $annotation,
55
              $class
56
            );
57
        } else {
58
            $message = sprintf(
59
              'Argument "%s" is mandatory for annotation %s.',
60
              $argumentName,
61
              $annotation,
62
            );
63
        }
64
65
        parent::__construct($message);
66
    }
67
68
}