PropertyTypeResolver   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 13
eloc 37
dl 0
loc 89
ccs 36
cts 39
cp 0.9231
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolvePossibleParentType() 0 7 4
A resolve() 0 38 5
A getParentType() 0 11 3
A __construct() 0 4 1
1
<?php
2
3
namespace Larapie\DataTransferObject\Resolvers;
4
5
use Throwable;
6
use ReflectionProperty;
7
use Larapie\DataTransferObject\Annotations\Inherit;
8
use Larapie\DataTransferObject\Property\PropertyType;
9
use Larapie\DataTransferObject\Exceptions\ConstraintInheritanceException;
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
     * TypeResolver constructor.
25
     * @param ReflectionProperty $reflection
26
     * @param array $annotations
27
     */
28 36
    final public function __construct(ReflectionProperty $reflection, array $annotations)
29
    {
30 36
        $this->reflection = $reflection;
31 36
        $this->annotations = $annotations;
32 36
    }
33
34
    /**
35
     * @return PropertyType
36
     */
37 36
    public function resolve(): PropertyType
38
    {
39 36
        $type = new PropertyType();
40
41 36
        $docComment = $this->reflection->getDocComment();
42
43 36
        if (! $docComment) {
44 1
            $type->setNullable(true);
45
46 1
            if (($parentType = $this->resolvePossibleParentType()) !== null) {
47
                return $parentType;
48
            }
49
50 1
            return $type;
51
        }
52
53 36
        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

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