Failed Conditions
Pull Request — master (#247)
by Michael
02:46
created

Target   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 7
eloc 31
dl 0
loc 67
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 29 7
1
<?php
2
3
namespace Doctrine\Annotations\Annotation;
4
5
use Doctrine\Annotations\Metadata\AnnotationTarget;
6
7
/**
8
 * Annotation that can be used to signal to the parser
9
 * to check the annotation target during the parsing process.
10
 *
11
 * @author Fabio B. Silva <[email protected]>
12
 *
13
 * @Annotation
14
 */
15
final class Target
16
{
17
    public const TARGET_CLASS      = AnnotationTarget::TARGET_CLASS;
18
    public const TARGET_METHOD     = AnnotationTarget::TARGET_METHOD;
19
    public const TARGET_PROPERTY   = AnnotationTarget::TARGET_PROPERTY;
20
    public const TARGET_ANNOTATION = AnnotationTarget::TARGET_ANNOTATION;
21
    public const TARGET_ALL        = AnnotationTarget::TARGET_ALL;
22
23
    /**
24
     * @var array
25
     */
26
    private static $map = [
27
        'ALL'        => self::TARGET_ALL,
28
        'CLASS'      => self::TARGET_CLASS,
29
        'METHOD'     => self::TARGET_METHOD,
30
        'PROPERTY'   => self::TARGET_PROPERTY,
31
        'ANNOTATION' => self::TARGET_ANNOTATION,
32
    ];
33
34
    /**
35
     * @var array
36
     */
37
    public $value;
38
39
    /**
40
     * Targets as bitmask.
41
     *
42
     * @var integer
43
     */
44
    public $targets;
45
46
    /**
47
     * Annotation constructor.
48
     *
49
     * @param array $values
50
     *
51
     * @throws \InvalidArgumentException
52
     */
53 217
    public function __construct(array $values)
54
    {
55 217
        if (!isset($values['value'])){
56 1
            $values['value'] = null;
57
        }
58 217
        if (is_string($values['value'])){
59
            $values['value'] = [$values['value']];
60
        }
61 217
        if (!is_array($values['value'])){
62 1
            throw new \InvalidArgumentException(
63 1
                sprintf('@Target expects either a string value, or an array of strings, "%s" given.',
64 1
                    is_object($values['value']) ? get_class($values['value']) : gettype($values['value'])
65
                )
66
            );
67
        }
68
69 216
        $bitmask = 0;
70 216
        foreach ($values['value'] as $literal) {
71 216
            if(!isset(self::$map[$literal])){
72 1
                throw new \InvalidArgumentException(
73 1
                    sprintf('Invalid Target "%s". Available targets: [%s]',
74 1
                            $literal,  implode(', ', array_keys(self::$map)))
75
                );
76
            }
77 215
            $bitmask |= self::$map[$literal];
78
        }
79
80 215
        $this->targets  = $bitmask;
81 215
        $this->value    = $values['value'];
82 215
    }
83
}
84