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 (#140)
by
unknown
01:23
created

ValueCaster   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 86
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A cast() 0 6 2
A castValue() 0 22 5
B castCollection() 0 34 6
A shouldBeCastToCollection() 0 18 5
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 (method_exists($castTo, 'fromRequest'))
33
            ? $castTo::fromRequest($value)
34
            : new $castTo($value);
35
    }
36
37
    public function castCollection($values, array $allowedArrayTypes)
38
    {
39
        $castTo = null;
40
41
        foreach ($allowedArrayTypes as $type) {
42
            if (! is_subclass_of($type, DataTransferObject::class)) {
43
                continue;
44
            }
45
46
            $castTo = $type;
47
48
            break;
49
        }
50
51
        if (! $castTo) {
52
            return $values;
53
        }
54
55
        $casts = [];
56
57
        $use_fromRequest = method_exists($castTo, 'fromRequest');
58
59
        foreach ($values as $value) {
60
            if ($use_fromRequest) {
61
                $casts[] = $castTo::fromRequest($value);
62
63
                continue;
64
            }
65
66
            $casts[] = new $castTo($value);
67
        }
68
69
        return $casts;
70
    }
71
72
    public function shouldBeCastToCollection(array $values): bool
73
    {
74
        if (empty($values)) {
75
            return false;
76
        }
77
78
        foreach ($values as $key => $value) {
79
            if (is_string($key)) {
80
                return false;
81
            }
82
83
            if (! is_array($value)) {
84
                return false;
85
            }
86
        }
87
88
        return true;
89
    }
90
}
91