Failed Conditions
Pull Request — master (#247)
by Michael
13:16
created

AnnotationTarget::targetsAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    public function __construct(int $target)
38
    {
39
        assert($target >= 0 && $target <= self::TARGET_ALL, 'Target out of bounds.');
40
41
        $this->target = $target;
42
    }
43
44
    public static function class() : self
45
    {
46
        return new self(self::TARGET_CLASS);
47
    }
48
49
    public static function method() : self
50
    {
51
        return new self(self::TARGET_METHOD);
52
    }
53
54
    public static function property() : self
55
    {
56
        return new self(self::TARGET_PROPERTY);
57
    }
58
59
    public static function annotation() : self
60
    {
61
        return new self(self::TARGET_ANNOTATION);
62
    }
63
64
    public static function all() : self
65
    {
66
        return new self(self::TARGET_ALL);
67
    }
68
69
    public static function fromAnnotation(Target $annotation) : self
70
    {
71
        return new self($annotation->targets);
72
    }
73
74
    public function unwrap() : int
75
    {
76
        return $this->target;
77
    }
78
79
    public function targetsClass() : bool
80
    {
81
        return ($this->target & self::TARGET_CLASS) === self::TARGET_CLASS;
82
    }
83
84
    public function targetsMethod() : bool
85
    {
86
        return ($this->target & self::TARGET_METHOD) === self::TARGET_METHOD;
87
    }
88
89
    public function targetsProperty() : bool
90
    {
91
        return ($this->target & self::TARGET_PROPERTY) === self::TARGET_PROPERTY;
92
    }
93
94
    public function targetsAnnotation() : bool
95
    {
96
        return ($this->target & self::TARGET_ANNOTATION) === self::TARGET_ANNOTATION;
97
    }
98
99
    public function describe() : string
100
    {
101
        return implode(
102
            ', ',
103
            array_filter(
104
                self::LABELS,
105
                function (int $target) : bool {
106
                    return ($this->target & $target) === $target;
107
                },
108
                ARRAY_FILTER_USE_KEY
109
            )
110
        );
111
    }
112
}
113