Failed Conditions
Push — annotation-metadata ( 414255 )
by Michael
02:42
created

AnnotationTarget::describe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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