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

AnnotationTarget::targetsAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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