Failed Conditions
Pull Request — new-parser-ast-metadata (#3)
by
unknown
01:53
created

Constructor::__construct()   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 1
crap 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