GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#66)
by Tom
01:12
created

Property::resolveTypeDefinition()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 42

Duplication

Lines 12
Ratio 28.57 %

Importance

Changes 0
Metric Value
dl 12
loc 42
rs 8.9368
c 0
b 0
f 0
cc 5
nc 5
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spatie\DataTransferObject;
6
7
use ReflectionProperty;
8
9
class Property extends ReflectionProperty
10
{
11
    /** @var array */
12
    protected static $typeMapping = [
13
        'int' => 'integer',
14
        'bool' => 'boolean',
15
        'float' => 'double',
16
    ];
17
18
    /** @var \Spatie\DataTransferObject\DataTransferObject */
19
    protected $valueObject;
20
21
    /** @var bool */
22
    protected $hasTypeDeclaration = false;
23
24
    /** @var bool */
25
    protected $isNullable = false;
26
27
    /** @var bool */
28
    protected $isInitialised = false;
29
30
    /** @var array */
31
    protected $types = [];
32
33
    /** @var array */
34
    protected $arrayTypes = [];
35
36
    /** @var array[] */
37
    protected static $cache = [];
38
39
    public static function fromReflection(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty)
40
    {
41
        return new self($valueObject, $reflectionProperty);
42
    }
43
44
    public function __construct(DataTransferObject $valueObject, ReflectionProperty $reflectionProperty)
45
    {
46
        parent::__construct($reflectionProperty->class, $reflectionProperty->getName());
47
48
        $this->valueObject = $valueObject;
49
50
        $this->resolveTypeDefinition();
51
    }
52
53
    public function set($value)
54
    {
55
        if (is_array($value)) {
56
            $value = $this->shouldBeCastToCollection($value) ? $this->castCollection($value) : $this->cast($value);
57
        }
58
59
        if (! $this->isValidType($value)) {
60
            throw DataTransferObjectError::invalidType($this, $value);
61
        }
62
63
        $this->isInitialised = true;
64
65
        $this->valueObject->{$this->getName()} = $value;
66
    }
67
68
    public function getTypes(): array
69
    {
70
        return $this->types;
71
    }
72
73
    public function getFqn(): string
74
    {
75
        return "{$this->getDeclaringClass()->getName()}::{$this->getName()}";
76
    }
77
78
    public function isNullable(): bool
79
    {
80
        return $this->isNullable;
81
    }
82
83
    protected function resolveTypeDefinition()
84
    {
85
        if (isset(self::$cache[$this->class][$this->getName()])) {
86
            $this->isNullable = self::$cache[$this->class][$this->getName()]['is_nullable'];
87
            $this->hasTypeDeclaration = (!empty(self::$cache[$this->class][$this->getName()]['types']) || !empty(self::$cache[$this->class][$this->getName()]['array_types']));
88
            $this->types = self::$cache[$this->class][$this->getName()]['types'] ?? [];
89
            $this->arrayTypes = self::$cache[$this->class][$this->getName()]['array_types'] ?? [];
90
91
            return;
92
        }
93
94
        $docComment = $this->getDocComment();
95
96 View Code Duplication
        if (! $docComment) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
            $this->isNullable = true;
98
            self::$cache[$this->class][$this->getName()]['is_nullable'] = $this->isNullable;
99
100
            return;
101
        }
102
103
        preg_match('/\@var ((?:(?:[\w|\\\\])+(?:\[\])?)+)/', $docComment, $matches);
104
105 View Code Duplication
        if (! count($matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            $this->isNullable = true;
107
            self::$cache[$this->class][$this->getName()]['is_nullable'] = $this->isNullable;
108
109
            return;
110
        }
111
112
        $this->hasTypeDeclaration = true;
113
114
        $varDocComment = end($matches);
115
116
        $this->types = explode('|', $varDocComment);
117
        self::$cache[$this->class][$this->getName()]['types'] = $this->types;
118
119
        $this->arrayTypes = str_replace('[]', '', $this->types);
120
        self::$cache[$this->class][$this->getName()]['array_types'] = $this->arrayTypes;
121
122
        $this->isNullable = strpos($varDocComment, 'null') !== false;
123
        self::$cache[$this->class][$this->getName()]['is_nullable'] = $this->isNullable;
124
    }
125
126
    protected function isValidType($value): bool
127
    {
128
        if (! $this->hasTypeDeclaration) {
129
            return true;
130
        }
131
132
        if ($this->isNullable && $value === null) {
133
            return true;
134
        }
135
136
        foreach ($this->types as $currentType) {
137
            $isValidType = $this->assertTypeEquals($currentType, $value);
138
139
            if ($isValidType) {
140
                return true;
141
            }
142
        }
143
144
        return false;
145
    }
146
147
    protected function cast($value)
148
    {
149
        $castTo = null;
150
151
        foreach ($this->types as $type) {
152
            if (! is_subclass_of($type, DataTransferObject::class)) {
153
                continue;
154
            }
155
156
            $castTo = $type;
157
158
            break;
159
        }
160
161
        if (! $castTo) {
162
            return $value;
163
        }
164
165
        return new $castTo($value);
166
    }
167
168
    protected function castCollection(array $values)
169
    {
170
        $castTo = null;
171
172
        foreach ($this->arrayTypes as $type) {
173
            if (! is_subclass_of($type, DataTransferObject::class)) {
174
                continue;
175
            }
176
177
            $castTo = $type;
178
179
            break;
180
        }
181
182
        if (! $castTo) {
183
            return $values;
184
        }
185
186
        $casts = [];
187
188
        foreach ($values as $value) {
189
            $casts[] = new $castTo($value);
190
        }
191
192
        return $casts;
193
    }
194
195
    protected function shouldBeCastToCollection(array $values): bool
196
    {
197
        if (empty($values)) {
198
            return false;
199
        }
200
201
        foreach ($values as $key => $value) {
202
            if (is_string($key)) {
203
                return false;
204
            }
205
206
            if (! is_array($value)) {
207
                return false;
208
            }
209
        }
210
211
        return true;
212
    }
213
214
    protected function assertTypeEquals(string $type, $value): bool
215
    {
216
        if (strpos($type, '[]') !== false) {
217
            return $this->isValidGenericCollection($type, $value);
218
        }
219
220
        if ($type === 'mixed' && $value !== null) {
221
            return true;
222
        }
223
224
        return $value instanceof $type
225
            || gettype($value) === (self::$typeMapping[$type] ?? $type);
226
    }
227
228
    protected function isValidGenericCollection(string $type, $collection): bool
229
    {
230
        if (! is_array($collection)) {
231
            return false;
232
        }
233
234
        $valueType = str_replace('[]', '', $type);
235
236
        foreach ($collection as $value) {
237
            if (! $this->assertTypeEquals($valueType, $value)) {
238
                return false;
239
            }
240
        }
241
242
        return true;
243
    }
244
}
245