Passed
Push — master ( 0efa8f...09a34a )
by Arthur
12:45
created

PropertyTypeResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 20
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 30 3
A __construct() 0 3 1
1
<?php
2
3
namespace Larapie\DataTransferObject\Resolvers;
4
5
use Larapie\DataTransferObject\Exceptions\TypeDoesNotExistException;
6
use phpDocumentor\Reflection\Types\Compound;
7
use ReflectionProperty;
8
use Larapie\DataTransferObject\PropertyType;
9
10
class PropertyTypeResolver
11
{
12
    /**
13
     * @var ReflectionProperty
14
     */
15
    protected $reflection;
16
17
    /**
18
     * TypeResolver constructor.
19
     * @param ReflectionProperty $reflection
20
     */
21
    public function __construct(ReflectionProperty $reflection)
22
    {
23
        $this->reflection = $reflection;
24
    }
25
26
    /**
27
     * @return PropertyType
28
     */
29
    public function resolve(): PropertyType
30
    {
31
        $type = new PropertyType();
32
33
        $docComment = $this->reflection->getDocComment();
34
35
        if (!$docComment) {
36
            $type->setNullable(true);
37
38
            return $type;
39
        }
40
41
        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

41
        preg_match('/\@var ((?:(?:[\w|\\\\])+(?:\[\])?)+)/', /** @scrutinizer ignore-type */ $docComment, $matches);
Loading history...
42
43
        if (!count($matches)) {
44
            $type->setNullable(true);
45
46
            return $type;
47
        }
48
49
        $varDocComment = end($matches);
50
51
        $resolver = new VarTypeResolver($this->reflection);
52
        $types = $resolver->resolve($varDocComment);
53
        $type->setTypes($types);
54
        $type->setArrayTypes(str_replace('[]', '', $types));
55
        $type->setHasType(true);
56
        $type->setNullable(strpos($varDocComment, 'null') !== false);
57
58
        return $type;
59
    }
60
61
}
62