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.

ValueCaster::shouldBeCastToCollection()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.3554
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
3
namespace Spatie\DataTransferObject;
4
5
class ValueCaster
6
{
7
    public function cast($value, FieldValidator $validator)
8
    {
9
        return $this->shouldBeCastToCollection($value)
10
            ? $this->castCollection($value, $validator->allowedArrayTypes)
11
            : $this->castValue($value, $validator->allowedTypes);
12
    }
13
14
    public function castValue($value, array $allowedTypes)
15
    {
16
        $castTo = null;
17
18
        foreach ($allowedTypes as $type) {
19
            if (! is_subclass_of($type, DataTransferObject::class)) {
20
                continue;
21
            }
22
23
            $castTo = $type;
24
25
            break;
26
        }
27
28
        if (! $castTo) {
29
            return $value;
30
        }
31
32
        return new $castTo($value);
33
    }
34
35
    public function castCollection($values, array $allowedArrayTypes)
36
    {
37
        $castTo = null;
38
39
        foreach ($allowedArrayTypes as $type) {
40
            if (! is_subclass_of($type, DataTransferObject::class)) {
41
                continue;
42
            }
43
44
            $castTo = $type;
45
46
            break;
47
        }
48
49
        if (! $castTo) {
50
            return $values;
51
        }
52
53
        $casts = [];
54
55
        foreach ($values as $value) {
56
            $casts[] = new $castTo($value);
57
        }
58
59
        return $casts;
60
    }
61
62
    public function shouldBeCastToCollection(array $values): bool
63
    {
64
        if (empty($values)) {
65
            return false;
66
        }
67
68
        foreach ($values as $key => $value) {
69
            if (is_string($key)) {
70
                return false;
71
            }
72
73
            if (! is_array($value)) {
74
                return false;
75
            }
76
        }
77
78
        return true;
79
    }
80
}
81