Failed Conditions
Pull Request — master (#264)
by
unknown
11:23
created

AnnotationTarget::method()   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 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_CONSTANT   = 16;
23
    public const TARGET_ALL        = self::TARGET_CLASS
24
        | self::TARGET_METHOD
25
        | self::TARGET_PROPERTY
26
        | self::TARGET_ANNOTATION
27
	    | self::TARGET_CONSTANT;
28
29
    private const LABELS = [
30
        self::TARGET_CLASS      => 'CLASS',
31
        self::TARGET_METHOD     => 'METHOD',
32
        self::TARGET_PROPERTY   => 'PROPERTY',
33
        self::TARGET_ANNOTATION => 'ANNOTATION',
34
        self::TARGET_CONSTANT   => 'CONSTANT',
35
        self::TARGET_ALL        => 'ALL',
36
    ];
37
38
    /** @var int */
39
    private $target;
40
41
    /**
42
     * @throws InvalidAnnotationTarget
43
     */
44 307
    public function __construct(int $target)
45
    {
46 307
        if ($target < 0 || $target > self::TARGET_ALL) {
47 2
            throw InvalidAnnotationTarget::fromInvalidBitmask($target);
48
        }
49
50 305
        $this->target = $target;
51 305
    }
52
53
    public static function class() : self
54
    {
55
        return new self(self::TARGET_CLASS);
56
    }
57
58 1
    public static function method() : self
59
    {
60 1
        return new self(self::TARGET_METHOD);
61
    }
62
63 297
    public static function property() : self
64
    {
65 297
        return new self(self::TARGET_PROPERTY);
66
    }
67
68 297
    public static function annotation() : self
69
    {
70 297
        return new self(self::TARGET_ANNOTATION);
71
    }
72
73
    public static function constant() : self
74
    {
75
        return new self(self::TARGET_CONSTANT);
76
    }
77
78 270
    public static function all() : self
79
    {
80 270
        return new self(self::TARGET_ALL);
81
    }
82
83 203
    public static function fromAnnotation(Target $annotation) : self
84
    {
85 203
        return new self($annotation->targets);
86
    }
87
88 266
    public function unwrap() : int
89
    {
90 266
        return $this->target;
91
    }
92
93 5
    public function targetsClass() : bool
94
    {
95 5
        return ($this->target & self::TARGET_CLASS) === self::TARGET_CLASS;
96
    }
97
98 5
    public function targetsMethod() : bool
99
    {
100 5
        return ($this->target & self::TARGET_METHOD) === self::TARGET_METHOD;
101
    }
102
103 5
    public function targetsProperty() : bool
104
    {
105 5
        return ($this->target & self::TARGET_PROPERTY) === self::TARGET_PROPERTY;
106
    }
107
108 5
    public function targetsAnnotation() : bool
109
    {
110 5
        return ($this->target & self::TARGET_ANNOTATION) === self::TARGET_ANNOTATION;
111
    }
112
113
    public function targetsConstant() : bool
114
    {
115
        return ($this->target & self::TARGET_CONSTANT) === self::TARGET_CONSTANT;
116
    }
117
118 11
    public function describe() : string
119
    {
120 11
        if ($this->target === self::TARGET_ALL) {
121 1
            return self::LABELS[self::TARGET_ALL];
122
        }
123
124 10
        return implode(
125 10
            ', ',
126 10
            array_filter(
127 10
                self::LABELS,
128
                function (int $target) : bool {
129 10
                    return ($this->target & $target) === $target;
130 10
                },
131 10
                ARRAY_FILTER_USE_KEY
132
            )
133
        );
134
    }
135
}
136