Passed
Pull Request — master (#247)
by Michael
02:00
created

AnnotationTarget::class()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Doctrine\Annotations\Metadata\Exception\InvalidAnnotationTarget;
9
use const ARRAY_FILTER_USE_KEY;
10
use function array_filter;
11
use function implode;
12
13
/**
14
 * Annotation targets represents possible targets at which an annotation could be declared.
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 301
    public function __construct(int $target)
42
    {
43 301
        if ($target < 0 || $target > self::TARGET_ALL) {
44 2
            throw InvalidAnnotationTarget::fromInvalidBitmask($target);
45
        }
46
47 299
        $this->target = $target;
48 299
    }
49
50
    public static function class() : self
51
    {
52
        return new self(self::TARGET_CLASS);
53
    }
54
55 1
    public static function method() : self
56
    {
57 1
        return new self(self::TARGET_METHOD);
58
    }
59
60 291
    public static function property() : self
61
    {
62 291
        return new self(self::TARGET_PROPERTY);
63
    }
64
65 291
    public static function annotation() : self
66
    {
67 291
        return new self(self::TARGET_ANNOTATION);
68
    }
69
70 267
    public static function all() : self
71
    {
72 267
        return new self(self::TARGET_ALL);
73
    }
74
75 203
    public static function fromAnnotation(Target $annotation) : self
76
    {
77 203
        return new self($annotation->targets);
78
    }
79
80 263
    public function unwrap() : int
81
    {
82 263
        return $this->target;
83
    }
84
85 5
    public function targetsClass() : bool
86
    {
87 5
        return ($this->target & self::TARGET_CLASS) === self::TARGET_CLASS;
88
    }
89
90 5
    public function targetsMethod() : bool
91
    {
92 5
        return ($this->target & self::TARGET_METHOD) === self::TARGET_METHOD;
93
    }
94
95 5
    public function targetsProperty() : bool
96
    {
97 5
        return ($this->target & self::TARGET_PROPERTY) === self::TARGET_PROPERTY;
98
    }
99
100 5
    public function targetsAnnotation() : bool
101
    {
102 5
        return ($this->target & self::TARGET_ANNOTATION) === self::TARGET_ANNOTATION;
103
    }
104
105 11
    public function describe() : string
106
    {
107 11
        if ($this->target === self::TARGET_ALL) {
108 1
            return self::LABELS[self::TARGET_ALL];
109
        }
110
111 10
        return implode(
112 10
            ', ',
113 10
            array_filter(
114 10
                self::LABELS,
115
                function (int $target) : bool {
116 10
                    return ($this->target & $target) === $target;
117 10
                },
118 10
                ARRAY_FILTER_USE_KEY
119
            )
120
        );
121
    }
122
}
123