Failed Conditions
Pull Request — master (#247)
by Michael
05:15
created

AnnotationTarget::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Metadata;
6
7
use Doctrine\Annotations\Annotation\Target;
8
use function array_filter;
9
use function assert;
10
use function implode;
11
12
/**
13
 * @internal
14
 */
15
final class AnnotationTarget
16
{
17
    public const TARGET_CLASS      = 1;
18
    public const TARGET_METHOD     = 2;
19
    public const TARGET_PROPERTY   = 4;
20
    public const TARGET_ANNOTATION = 8;
21
    public const TARGET_ALL        = self::TARGET_CLASS
22
        | self::TARGET_METHOD
23
        | self::TARGET_PROPERTY
24
        | self::TARGET_ANNOTATION;
25
26
    private const LABELS = [
27
        self::TARGET_CLASS      => 'CLASS',
28
        self::TARGET_METHOD     => 'METHOD',
29
        self::TARGET_PROPERTY   => 'PROPERTY',
30
        self::TARGET_ANNOTATION => 'ANNOTATION',
31
        self::TARGET_ALL        => 'ALL',
32
    ];
33
34
    /** @var int */
35
    private $target;
36
37 296
    public function __construct(int $target)
38
    {
39 296
        assert($target >= 0 && $target <= self::TARGET_ALL, 'Target out of bounds.');
40
41 296
        $this->target = $target;
42 296
    }
43
44
    public static function class() : self
45
    {
46
        return new self(self::TARGET_CLASS);
47
    }
48
49 1
    public static function method() : self
50
    {
51 1
        return new self(self::TARGET_METHOD);
52
    }
53
54 291
    public static function property() : self
55
    {
56 291
        return new self(self::TARGET_PROPERTY);
57
    }
58
59 291
    public static function annotation() : self
60
    {
61 291
        return new self(self::TARGET_ANNOTATION);
62
    }
63
64 264
    public static function all() : self
65
    {
66 264
        return new self(self::TARGET_ALL);
67
    }
68
69 203
    public static function fromAnnotation(Target $annotation) : self
70
    {
71 203
        return new self($annotation->targets);
72
    }
73
74 263
    public function unwrap() : int
75
    {
76 263
        return $this->target;
77
    }
78
79 5
    public function targetsClass() : bool
80
    {
81 5
        return ($this->target & self::TARGET_CLASS) === self::TARGET_CLASS;
82
    }
83
84 5
    public function targetsMethod() : bool
85
    {
86 5
        return ($this->target & self::TARGET_METHOD) === self::TARGET_METHOD;
87
    }
88
89 5
    public function targetsProperty() : bool
90
    {
91 5
        return ($this->target & self::TARGET_PROPERTY) === self::TARGET_PROPERTY;
92
    }
93
94 5
    public function targetsAnnotation() : bool
95
    {
96 5
        return ($this->target & self::TARGET_ANNOTATION) === self::TARGET_ANNOTATION;
97
    }
98
99 11
    public function describe() : string
100
    {
101 11
        if ($this->target === self::TARGET_ALL) {
102 1
            return self::LABELS[self::TARGET_ALL];
103
        }
104
105 10
        return implode(
106 10
            ', ',
107 10
            array_filter(
108 10
                self::LABELS,
109
                function (int $target) : bool {
110 10
                    return ($this->target & $target) === $target;
111 10
                },
112 10
                ARRAY_FILTER_USE_KEY
113
            )
114
        );
115
    }
116
}
117