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 (#77)
by Brent
01:19
created

DataTransferObject   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 139
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spatie\DataTransferObject;
6
7
use ReflectionClass;
8
use ReflectionProperty;
9
10
abstract class DataTransferObject
11
{
12
    protected bool $ignoreMissing = false;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
13
14
    protected array $exceptKeys = [];
15
16
    protected array $onlyKeys = [];
17
18
    /**
19
     * @param array $parameters
20
     *
21
     * @return \Spatie\DataTransferObject\ImmutableDataTransferObject|static
22
     */
23
    public static function immutable(array $parameters = []): ImmutableDataTransferObject
24
    {
25
        return new ImmutableDataTransferObject(new static($parameters));
26
    }
27
28
    public function __construct(array $parameters = [])
29
    {
30
        $class = new ReflectionClass(static::class);
31
32
        $validators = $this->getFieldValidators($class);
33
34
        $valueCaster = new ValueCaster();
35
36
        foreach ($validators as $field => $validator) {
37
            if (
38
                ! isset($parameters[$field])
39
                && ! $validator->hasDefaultValue
40
                && ! $validator->isNullable
41
            ) {
42
                throw DataTransferObjectError::uninitialized(
43
                    static::class,
44
                    $field
45
                );
46
            }
47
48
            $value = $parameters[$field] ?? $this->{$field} ?? null;
49
50
            if (is_array($value)) {
51
                $value = $valueCaster->cast($value, $validator);
52
            }
53
54
            if (! $validator->isValidType($value)) {
55
                throw DataTransferObjectError::invalidType(
56
                    static::class,
57
                    $field,
58
                    $validator->allowedTypes,
59
                    $value
60
                );
61
            }
62
63
            $this->{$field} = $value;
64
65
            unset($parameters[$field]);
66
        }
67
68
        if (! $this->ignoreMissing && count($parameters)) {
69
            throw DataTransferObjectError::unknownProperties(array_keys($parameters), $class->getName());
70
        }
71
    }
72
73
    public function all(): array
74
    {
75
        $data = [];
76
77
        $class = new ReflectionClass(static::class);
78
79
        $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
80
81
        foreach ($properties as $reflectionProperty) {
82
            $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this);
83
        }
84
85
        return $data;
86
    }
87
88
    /**
89
     * @param string ...$keys
90
     *
91
     * @return static
92
     */
93
    public function only(string ...$keys): DataTransferObject
94
    {
95
        $dataTransferObject = clone $this;
96
97
        $dataTransferObject->onlyKeys = [...$this->onlyKeys, ...$keys];
98
99
        return $dataTransferObject;
100
    }
101
102
    /**
103
     * @param string ...$keys
104
     *
105
     * @return static
106
     */
107
    public function except(string ...$keys): DataTransferObject
108
    {
109
        $dataTransferObject = clone $this;
110
111
        $dataTransferObject->exceptKeys = [...$this->exceptKeys, ...$keys];
112
113
        return $dataTransferObject;
114
    }
115
116
    public function toArray(): array
117
    {
118
        if (count($this->onlyKeys)) {
119
            $array = Arr::only($this->all(), $this->onlyKeys);
120
        } else {
121
            $array = Arr::except($this->all(), $this->exceptKeys);
122
        }
123
124
        $array = $this->parseArray($array);
125
126
        return $array;
127
    }
128
129
    protected function parseArray(array $array): array
130
    {
131
        foreach ($array as $key => $value) {
132
            if (
133
                $value instanceof DataTransferObject
134
                || $value instanceof DataTransferObjectCollection
135
            ) {
136
                $array[$key] = $value->toArray();
137
138
                continue;
139
            }
140
141
            if (! is_array($value)) {
142
                continue;
143
            }
144
145
            $array[$key] = $this->parseArray($value);
146
        }
147
148
        return $array;
149
    }
150
151
    /**
152
     * @param \ReflectionClass $class
153
     *
154
     * @return \Spatie\DataTransferObject\FieldValidator[]
155
     */
156
    private function getFieldValidators(ReflectionClass $class): array
157
    {
158
        return DTOCache::resolve(static::class, function () use ($class) {
159
            $properties = [];
160
161
            foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
162
                $field = $reflectionProperty->getName();
163
164
                $properties[$field] = FieldValidator::fromReflection($reflectionProperty);
165
            }
166
167
            return $properties;
168
        });
169
    }
170
}
171