Passed
Branch fix-scrutinezer (372d64)
by Vitalik
08:38
created

PropertyMap::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace BitrixToolkit\BitrixEntityMapper\Map;
4
5
use Doctrine\Common\Annotations\AnnotationException;
6
use InvalidArgumentException;
7
use ReflectionProperty;
8
use BitrixToolkit\BitrixEntityMapper\Annotation\AnnotationReader;
9
use BitrixToolkit\BitrixEntityMapper\Annotation\Property\PropertyAnnotationInterface;
10
11
class PropertyMap
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $code;
17
18
    /**
19
     * @var PropertyAnnotationInterface
20
     */
21
    protected $annotation;
22
23
    /**
24
     * @var ReflectionProperty
25
     */
26
    protected $reflection;
27
28
    /**
29
     * PropertyMap constructor.
30
     * @param string $code
31
     * @param PropertyAnnotationInterface $annotation
32
     * @param ReflectionProperty $reflection
33
     */
34 26
    public function __construct($code, PropertyAnnotationInterface $annotation, ReflectionProperty $reflection)
35
    {
36 26
        $this->code = $code;
37 26
        $this->annotation = $annotation;
38 26
        $this->reflection = $reflection;
39
    }
40
41
    /**
42
     * @param ReflectionProperty $propRef
43
     * @return PropertyMap|null
44
     * @throws AnnotationException
45
     */
46 26
    public static function fromReflectionProperty(ReflectionProperty $propRef)
47
    {
48 26
        $annotationReader = new AnnotationReader();
49 26
        $propAnnotations = $annotationReader->getPropertyAnnotations($propRef);
50 26
        $propAnnotations = array_filter($propAnnotations, function ($propAnnotation) {
51 26
            return $propAnnotation instanceof PropertyAnnotationInterface;
52 26
        });
53
54 26
        if (empty($propAnnotations)) {
55 25
            return null;
56
        }
57
58 26
        if (count($propAnnotations) > 1) {
59 1
            $annotationClasses = array_map(function ($propAnnotation) {
60 1
                return get_class($propAnnotation);
61 1
            }, $propAnnotations);
62
63 1
            throw new InvalidArgumentException(
64 1
                'Аннотации ' . '@' . implode(', @', $annotationClasses) .
65 1
                ' свойства ' . $propRef->getName() . ' класса ' . $propRef->getDeclaringClass()->getName() .
66 1
                ' не могут быть применены одновременно.'
67 1
            );
68
        }
69
70
        /** @var PropertyAnnotationInterface $propAnnotation */
71 25
        $propAnnotation = reset($propAnnotations);
72 25
        return new PropertyMap($propRef->getName(), $propAnnotation, $propRef);
73
    }
74
75
    /**
76
     * @return string
77
     */
78 21
    public function getCode()
79
    {
80 21
        return $this->code;
81
    }
82
83
    /**
84
     * @return PropertyAnnotationInterface
85
     */
86 24
    public function getAnnotation()
87
    {
88 24
        return $this->annotation;
89
    }
90
91
    /**
92
     * @return ReflectionProperty
93
     */
94 6
    public function getReflection()
95
    {
96 6
        return $this->reflection;
97
    }
98
}