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

PropertyTypeResolver::resolvePossibleParentType()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
c 0
b 0
f 0
rs 10
cc 4
nc 4
nop 0
1
<?php
2
3
namespace Larapie\DataTransferObject\Resolvers;
4
5
use Larapie\DataTransferObject\Annotations\Inherit;
6
use Larapie\DataTransferObject\Exceptions\ConstraintInheritanceException;
7
use Larapie\DataTransferObject\Property\PropertyType;
8
use ReflectionProperty;
9
use Throwable;
10
11
class PropertyTypeResolver
12
{
13
    /**
14
     * @var ReflectionProperty
15
     */
16
    protected $reflection;
17
18
    /**
19
     * @var array
20
     */
21
    protected $annotations;
22
23
24
    /**
25
     * TypeResolver constructor.
26
     * @param ReflectionProperty $reflection
27
     * @param array $annotations
28
     */
29
    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...
30
    {
31
        $this->reflection = $reflection;
32
        $this->annotations = $annotations;
33
    }
34
35
    /**
36
     * @return PropertyType
37
     */
38
    public function resolve(): PropertyType
39
    {
40
        $type = new PropertyType();
41
42
        $docComment = $this->reflection->getDocComment();
43
44
        if (!$docComment) {
45
            $type->setNullable(true);
46
47
            if (($parentType = $this->resolvePossibleParentType()) !== null)
48
                return $parentType;
49
            return $type;
50
        }
51
52
        preg_match('/\@var ((?:(?:[\w|\\\\])+(?:\[\])?)+)/', $docComment, $matches);
0 ignored issues
show
Bug introduced by
It seems like $docComment can also be of type true; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        preg_match('/\@var ((?:(?:[\w|\\\\])+(?:\[\])?)+)/', /** @scrutinizer ignore-type */ $docComment, $matches);
Loading history...
53
54
        if (!count($matches)) {
55
            $type->setNullable(true);
56
57
            if (($parentType = $this->resolvePossibleParentType()) !== null)
58
                return $parentType;
59
60
            return $type;
61
        }
62
63
        $varDocComment = end($matches);
64
65
        $resolver = new VarTypeResolver($this->reflection);
66
        $types = $resolver->resolve($varDocComment);
67
        $type->setTypes($types);
68
        $type->setArrayTypes(str_replace('[]', '', $types));
69
        $type->setInitialized(true);
70
        $type->setNullable(strpos($varDocComment, 'null') !== false);
71
72
        return $type;
73
    }
74
75
    protected function resolvePossibleParentType()
76
    {
77
        foreach ($this->annotations as $annotation) {
78
            if ($annotation instanceof Inherit) {
79
                $type = $this->getParentType();
80
                if ($type->isInitialized())
81
                    return $type;
82
            }
83
        }
84
        return null;
85
    }
86
87
    protected function getParentType(): PropertyType
88
    {
89
        try {
90
            if ($parentClass = $this->reflection->getDeclaringClass()->getParentClass()) {
91
                $parentProperty = $parentClass->getProperty($this->reflection->getName());
92
            }
93
        } catch (Throwable $exception) {
94
            throw new ConstraintInheritanceException("There is no parent property to inherit from");
95
        }
96
97
        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...
98
    }
99
}
100