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

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