Completed
Pull Request — master (#247)
by Michael
02:36
created

AnnotationTarget::targetsMethod()   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 Doctrine\Annotations\Metadata\Exception\InvalidAnnotationTarget;
9
use const ARRAY_FILTER_USE_KEY;
10
use function array_filter;
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
    /**
39
     * @throws InvalidAnnotationTarget
40
     */
41
    public function __construct(int $target)
42
    {
43
        if ($target < 0 || $target > self::TARGET_ALL) {
44
            throw InvalidAnnotationTarget::fromInvalidBitmask($target);
45
        }
46
47
        $this->target = $target;
48
    }
49
50
    public static function class() : self
51
    {
52
        return new self(self::TARGET_CLASS);
53
    }
54
55
    public static function method() : self
56
    {
57
        return new self(self::TARGET_METHOD);
58
    }
59
60
    public static function property() : self
61
    {
62
        return new self(self::TARGET_PROPERTY);
63
    }
64
65
    public static function annotation() : self
66
    {
67
        return new self(self::TARGET_ANNOTATION);
68
    }
69
70
    public static function all() : self
71
    {
72
        return new self(self::TARGET_ALL);
73
    }
74
75
    public static function fromAnnotation(Target $annotation) : self
76
    {
77
        return new self($annotation->targets);
78
    }
79
80
    public function unwrap() : int
81
    {
82
        return $this->target;
83
    }
84
85
    public function targetsClass() : bool
86
    {
87
        return ($this->target & self::TARGET_CLASS) === self::TARGET_CLASS;
88
    }
89
90
    public function targetsMethod() : bool
91
    {
92
        return ($this->target & self::TARGET_METHOD) === self::TARGET_METHOD;
93
    }
94
95
    public function targetsProperty() : bool
96
    {
97
        return ($this->target & self::TARGET_PROPERTY) === self::TARGET_PROPERTY;
98
    }
99
100
    public function targetsAnnotation() : bool
101
    {
102
        return ($this->target & self::TARGET_ANNOTATION) === self::TARGET_ANNOTATION;
103
    }
104
105
    public function describe() : string
106
    {
107
        if ($this->target === self::TARGET_ALL) {
108
            return self::LABELS[self::TARGET_ALL];
109
        }
110
111
        return implode(
112
            ', ',
113
            array_filter(
114
                self::LABELS,
115
                function (int $target) : bool {
116
                    return ($this->target & $target) === $target;
117
                },
118
                ARRAY_FILTER_USE_KEY
119
            )
120
        );
121
    }
122
}
123