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
unknown
02:44 queued 42s
created

DataTransferObject::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 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
    /**
29
     * @param array $arrayOfParameters
30
     *
31
     * @return \Spatie\DataTransferObject\ImmutableDataTransferObject[]|static[]
32
     */
33
    public static function arrayOf(array $arrayOfParameters): array
34
    {
35
        return array_map(
36
            function ($parameters) {
37
                return new static($parameters);
38
            },
39
            $arrayOfParameters
40
        );
41
    }
42
43
    public function __construct(array $parameters = [])
44
    {
45
        $validators = $this->getFieldValidators();
46
47
        $valueCaster = new ValueCaster();
48
49
        foreach ($validators as $field => $validator) {
50
            if (
51
                ! isset($parameters[$field])
52
                && ! $validator->hasDefaultValue
53
                && ! $validator->isNullable
54
            ) {
55
                throw DataTransferObjectError::uninitialized(
56
                    static::class,
57
                    $field
58
                );
59
            }
60
61
            $value = $parameters[$field] ?? $this->{$field} ?? null;
62
63
            if (is_array($value)) {
64
                $value = $valueCaster->cast($value, $validator);
65
            }
66
67
            if (! $validator->isValidType($value)) {
68
                throw DataTransferObjectError::invalidType(
69
                    static::class,
70
                    $field,
71
                    $validator->allowedTypes,
72
                    $value
73
                );
74
            }
75
76
            $this->{$field} = $value;
77
78
            unset($parameters[$field]);
79
        }
80
81
        if (! $this->ignoreMissing && count($parameters)) {
82
            throw DataTransferObjectError::unknownProperties(array_keys($parameters), static::class);
83
        }
84
    }
85
86
    public function all(): array
87
    {
88
        $data = [];
89
90
        $class = new ReflectionClass(static::class);
91
92
        $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
93
94
        foreach ($properties as $reflectionProperty) {
95
            $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this);
96
        }
97
98
        return $data;
99
    }
100
101
    /**
102
     * @param string ...$keys
103
     *
104
     * @return static
105
     */
106
    public function only(string ...$keys): DataTransferObject
107
    {
108
        $dataTransferObject = clone $this;
109
110
        $dataTransferObject->onlyKeys = [...$this->onlyKeys, ...$keys];
111
112
        return $dataTransferObject;
113
    }
114
115
    /**
116
     * @param string ...$keys
117
     *
118
     * @return static
119
     */
120
    public function except(string ...$keys): DataTransferObject
121
    {
122
        $dataTransferObject = clone $this;
123
124
        $dataTransferObject->exceptKeys = [...$this->exceptKeys, ...$keys];
125
126
        return $dataTransferObject;
127
    }
128
129
    public function toArray(): array
130
    {
131
        if (count($this->onlyKeys)) {
132
            $array = Arr::only($this->all(), $this->onlyKeys);
133
        } else {
134
            $array = Arr::except($this->all(), $this->exceptKeys);
135
        }
136
137
        $array = $this->parseArray($array);
138
139
        return $array;
140
    }
141
142
    protected function parseArray(array $array): array
143
    {
144
        foreach ($array as $key => $value) {
145
            if (
146
                $value instanceof DataTransferObject
147
                || $value instanceof DataTransferObjectCollection
148
            ) {
149
                $array[$key] = $value->toArray();
150
151
                continue;
152
            }
153
154
            if (! is_array($value)) {
155
                continue;
156
            }
157
158
            $array[$key] = $this->parseArray($value);
159
        }
160
161
        return $array;
162
    }
163
164
    /**
165
     * @param \ReflectionClass $class
166
     *
167
     * @return \Spatie\DataTransferObject\FieldValidator[]
168
     */
169
    private function getFieldValidators(): array
170
    {
171
        return DTOCache::resolve(static::class, function () {
172
            $class = new ReflectionClass(static::class);
173
174
            $properties = [];
175
176
            foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
177
                // Skip static properties
178
                if ($reflectionProperty->isStatic()) {
179
                    continue;
180
                }
181
182
                $field = $reflectionProperty->getName();
183
184
                $properties[$field] = FieldValidator::fromReflection($reflectionProperty);
185
            }
186
187
            return $properties;
188
        });
189
    }
190
}
191