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

AnnotationMetadata::getTarget()   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\Metadata\Exception\TooManyDefaultProperties;
8
use function array_combine;
9
use function array_filter;
10
use function array_map;
11
use function array_values;
12
use function count;
13
14
/**
15
 * Annotation metadata represents information about the definition a single annotation - its name, allowed targets,
16
 * construction strategy and properties.
17
 */
18
final class AnnotationMetadata
19
{
20
    /** @var string */
21
    private $name;
22
23
    /** @var AnnotationTarget */
24
    private $target;
25
26
    /** @var bool */
27
    private $usesConstructor;
28
29
    /** @var array<string, PropertyMetadata> */
30
    private $properties;
31
32
    /** @var PropertyMetadata|null */
33
    private $defaultProperty;
34
35
    /**
36
     * @param PropertyMetadata[] $properties
37
     */
38 297
    public function __construct(
39
        string $name,
40
        AnnotationTarget $target,
41
        bool $hasConstructor,
42
        PropertyMetadata ...$properties
43
    ) {
44 297
        $this->name            = $name;
45 297
        $this->target          = $target;
46 297
        $this->usesConstructor = $hasConstructor;
47 297
        $this->properties      = array_combine(
48 297
            array_map(
49
                static function (PropertyMetadata $property) : string {
50 292
                    return $property->getName();
51 297
                },
52 297
                $properties
53
            ),
54 297
            $properties
55
        );
56
57 297
        $defaultProperties = array_filter(
58 297
            $properties,
59
            static function (PropertyMetadata $property) : bool {
60 292
                return $property->isDefault();
61 297
            }
62
        );
63
64 297
        if (count($defaultProperties) > 1) {
65 1
            throw TooManyDefaultProperties::new($name, ...$defaultProperties);
66
        }
67
68 296
        $this->defaultProperty = array_values($defaultProperties)[0] ?? null;
69 296
    }
70
71 296
    public function getName() : string
72
    {
73 296
        return $this->name;
74
    }
75
76 261
    public function getTarget() : AnnotationTarget
77
    {
78 261
        return $this->target;
79
    }
80
81 250
    public function usesConstructor() : bool
82
    {
83 250
        return $this->usesConstructor;
84
    }
85
86
    /**
87
     * @return array<string, PropertyMetadata>
88
     */
89 250
    public function getProperties() : array
90
    {
91 250
        return $this->properties;
92
    }
93
94 94
    public function getDefaultProperty() : ?PropertyMetadata
95
    {
96 94
        return $this->defaultProperty;
97
    }
98
}
99