Passed
Push — master ( cbecdb...34e2d9 )
by Arthur
07:30
created

ConstraintsResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
4
namespace Larapie\DataTransferObject\Resolvers;
5
6
7
use Larapie\DataTransferObject\Annotations\Inherit;
8
use Larapie\DataTransferObject\Exceptions\ConstraintInheritanceException;
9
use ReflectionProperty;
10
use Symfony\Component\Validator\Constraint;
11
use Throwable;
12
13
class ConstraintsResolver
14
{
15
    /**
16
     * @var ReflectionProperty
17
     */
18
    protected $reflection;
19
20
    /**
21
     * @var array
22
     */
23
    protected $annotations;
24
25
26
    /**
27
     * TypeResolver constructor.
28
     * @param ReflectionProperty $reflection
29
     * @param array $annotations
30
     */
31
    public final function __construct(ReflectionProperty $reflection, array $annotations)
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
32
    {
33
        $this->reflection = $reflection;
34
        $this->annotations = $annotations;
35
    }
36
37
    public function resolve(): array
38
    {
39
        $constraints = [];
40
        foreach ($this->annotations as $annotation) {
41
            if ($annotation instanceof Inherit) {
42
                $constraints = array_merge($constraints, $this->getParentConstraints());
43
            } else if ($annotation instanceof Constraint) {
44
                $constraints[] = $annotation;
45
            }
46
        }
47
48
        return $constraints;
49
    }
50
51
    protected function getParentConstraints()
52
    {
53
        try {
54
            if ($parentClass = $this->reflection->getDeclaringClass()->getParentClass()) {
55
                $parentProperty = $parentClass->getProperty($this->reflection->getName());
56
            }
57
        } catch (Throwable $exception) {
58
            throw new ConstraintInheritanceException("There is no parent property to inherit from");
59
        }
60
61
        return (new static($parentProperty, (new AnnotationResolver($parentProperty))->resolve()))->resolve();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parentProperty does not seem to be defined for all execution paths leading up to this point.
Loading history...
62
    }
63
64
65
}