Completed
Push — master ( faad46...c87772 )
by Brent
02:23
created

Property::getTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\ValueObject;
4
5
use ReflectionProperty;
6
7
class Property extends ReflectionProperty
8
{
9
    /** @var array */
10
    protected static $typeMapping = [
11
        'int' => 'integer',
12
        'bool' => 'boolean',
13
    ];
14
15
    /** @var \Spatie\ValueObject\ValueObject */
16
    protected $valueObject;
17
18
    /** @var bool */
19
    protected $hasTypeDeclaration = false;
20
21
    /** @var bool */
22
    protected $isNullable = false;
23
24
    /** @var bool */
25
    protected $isInitialised = false;
26
27
    /** @var array */
28
    protected $types = [];
29
30
    public static function fromReflection(ValueObject $valueObject, ReflectionProperty $reflectionProperty)
31
    {
32
        return new self($valueObject, $reflectionProperty);
33
    }
34
35
    public function __construct(ValueObject $valueObject, ReflectionProperty $reflectionProperty)
36
    {
37
        parent::__construct($reflectionProperty->class, $reflectionProperty->getName());
38
39
        $this->valueObject = $valueObject;
40
41
        $this->resolveTypeDefinition();
42
    }
43
44
    public function set($value)
45
    {
46
        if (! $this->isValidType($value)) {
47
48
            throw ValueObjectError::invalidType(
49
                $this,
50
                $this->getDeclaringClass()->getName(),
51
                $value
0 ignored issues
show
Unused Code introduced by
The call to ValueObjectError::invalidType() has too many arguments starting with $value.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
52
            );
53
        }
54
55
        $this->isInitialised = true;
56
57
        $this->valueObject->{$this->getName()} = $value;
58
    }
59
60
    public function getTypes(): array
61
    {
62
        return $this->types;
63
    }
64
65
    public function getFqn(): string
66
    {
67
        return "{$this->getDeclaringClass()->getName()}::{$this->getName()}";
68
    }
69
70
    protected function resolveTypeDefinition()
71
    {
72
        $docComment = $this->getDocComment();
73
74
        if (! $docComment) {
75
            return;
76
        }
77
78
        preg_match('/\@var ([\w|\\\\]+)/', $docComment, $matches);
79
80
        if (! count($matches)) {
81
            return;
82
        }
83
84
        $this->hasTypeDeclaration = true;
85
86
        $varDocComment = end($matches);
87
88
        $this->types = explode('|', $varDocComment);
89
90
        $this->isNullable = strpos($varDocComment, 'null') !== false;
91
    }
92
93
    protected function isValidType($value): bool
94
    {
95
        if (! $this->hasTypeDeclaration) {
96
            return true;
97
        }
98
99
        if ($this->isNullable && $value === null) {
100
            return true;
101
        }
102
103
        foreach ($this->types as $currentType) {
104
            $isValidType = $this->assertTypeEquals($currentType, $value);
105
106
            if ($isValidType) {
107
                return true;
108
            }
109
        }
110
111
        return false;
112
    }
113
114
    protected function assertTypeEquals(string $type, $value): bool
115
    {
116
        if ($type === 'mixed' && $value !== null) {
117
            return true;
118
        }
119
120
        if (class_exists($type)) {
121
            return $value instanceof $type;
122
        }
123
124
        return gettype($value) === (self::$typeMapping[$type] ?? $type);
125
    }
126
}
127