Passed
Pull Request — new-parser-ast-metadata (#3)
by
unknown
03:27
created

Constructor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
wmc 8
eloc 17
dl 0
loc 49
ccs 17
cts 20
cp 0.85
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A construct() 0 9 2
A getPropertyMetadata() 0 10 2
A validateProperties() 0 8 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Constructor;
6
7
use Doctrine\Annotations\Constructor\Instantiator\Instantiator;
8
use Doctrine\Annotations\Metadata\AnnotationMetadata;
9
use Doctrine\Annotations\Metadata\InvalidAnnotationValue;
10
use Doctrine\Annotations\Metadata\InvalidPropertyValue;
11
use Doctrine\Annotations\Metadata\PropertyMetadata;
12
use Doctrine\Annotations\Parser\Scope;
13
14
final class Constructor
15
{
16
    /** @var Instantiator */
17
    private $instantiator;
18
19 11
    public function __construct(Instantiator $instantiator)
20
    {
21 11
        $this->instantiator = $instantiator;
22 11
    }
23
24
    /**
25
     * @param mixed[] $parameters iterable<string, mixed>
26
     */
27 11
    public function construct(AnnotationMetadata $annotationMetadata, Scope $scope, iterable $parameters) : object
28
    {
29 11
        $annotationMetadata->validateTarget($scope);
30
31 11
        if (! $annotationMetadata->hasConstructor()) {
32 10
            $this->validateProperties($annotationMetadata, $parameters);
33
        }
34
35 11
        return $this->instantiator->instantiate($annotationMetadata, $parameters);
36
    }
37
38
    /**
39
     * @param mixed[] $parameters
40
     */
41 10
    private function validateProperties(AnnotationMetadata $annotationMetadata, iterable $parameters) : void
42
    {
43 10
        foreach ($parameters as $propertyName => $propertyValue) {
44 4
            $propertyMetadata = $this->getPropertyMetadata($annotationMetadata, $propertyName);
45
            try {
46 4
                $propertyMetadata->validateValue($propertyValue);
47
            } catch (InvalidPropertyValue $exception) {
48 4
                throw InvalidAnnotationValue::new($annotationMetadata, $exception);
49
            }
50
        }
51 10
    }
52
53 4
    private function getPropertyMetadata(AnnotationMetadata $annotationMetadata, string $propertyName) : PropertyMetadata
54
    {
55 4
        if ($propertyName === '') {
56
            /** @var PropertyMetadata $defaultProperty */
57
            $defaultProperty = $annotationMetadata->getDefaultProperty();
58
59
            $propertyName = $defaultProperty->getName();
60
        }
61
62 4
        return $annotationMetadata->getProperties()[$propertyName];
63
    }
64
}
65