Passed
Push — master ( 0756fe...706dc7 )
by Arthur
14:47
created

TypeResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 18
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

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

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