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 (#57)
by Brent
02:25 queued 01:05
created

DataTransferObject::__set()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 3
nop 2
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
    /** @var array */
13
    private $propertyValues = [];
14
15
    /** @var \Spatie\DataTransferObject\Property[] */
16
    private $propertyDefinitions = [];
17
18
    /** @var array */
19
    protected $exceptKeys = [];
20
21
    /** @var array */
22
    protected $onlyKeys = [];
23
24
    public function __construct(array $inputParameters)
25
    {
26
        $class = new ReflectionClass(static::class);
27
28
        $properties = $this->getPublicProperties($class);
29
30
        foreach ($properties as $property) {
31
            $propertyName = $property->getName();
32
33
            if (
34
                ! isset($inputParameters[$propertyName])
35
                && ! $property->isDefault()
36
                && ! $property->isNullable()
37
            ) {
38
                throw DataTransferObjectError::uninitialized($property);
39
            }
40
41
            $value = $inputParameters[$propertyName] ?? $property->getValue($this);
42
43
            if ($property->isImmutable()) {
44
                $this->makeImmutable($property);
45
            }
46
47
            unset($inputParameters[$propertyName]);
48
49
            $property->set($value);
50
        }
51
52
        if (count($inputParameters)) {
53
            throw DataTransferObjectError::unknownProperties(array_keys($inputParameters), $class->getName());
54
        }
55
    }
56
57
    public function __get($name)
58
    {
59
        if (! array_key_exists($name, $this->propertyValues)) {
60
            throw DataTransferObjectError::fieldNotFound($name);
61
        }
62
63
        return $this->propertyValues[$name] ?? $this->{$name};
64
    }
65
66
    public function __set($name, $value)
67
    {
68
        if (! array_key_exists($name, $this->propertyDefinitions)) {
69
            throw DataTransferObjectError::fieldNotFound($name);
70
        }
71
72
        if (
73
            $this->propertyDefinitions[$name]->isInitialised()
74
            && $this->propertyDefinitions[$name]->isImmutable()
75
        ) {
76
            throw DataTransferObjectError::immutable($name);
77
        }
78
79
        $this->propertyValues[$name] = $value;
80
    }
81
82
    public function all(): array
83
    {
84
        $data = [];
85
86
        $class = new ReflectionClass(static::class);
87
88
        $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
89
90
        foreach ($properties as $reflectionProperty) {
91
            $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this);
92
        }
93
94
        return $data;
95
    }
96
97
    /**
98
     * @param string ...$keys
99
     *
100
     * @return static
101
     */
102
    public function only(string ...$keys): DataTransferObject
103
    {
104
        $valueObject = clone $this;
105
106
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
107
108
        return $valueObject;
109
    }
110
111
    /**
112
     * @param string ...$keys
113
     *
114
     * @return static
115
     */
116
    public function except(string ...$keys): DataTransferObject
117
    {
118
        $valueObject = clone $this;
119
120
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
121
122
        return $valueObject;
123
    }
124
125
    public function toArray(): array
126
    {
127
        if (count($this->onlyKeys)) {
128
            $array = Arr::only($this->all(), $this->onlyKeys);
129
        } else {
130
            $array = Arr::except($this->all(), $this->exceptKeys);
131
        }
132
133
        $array = $this->parseArray($array);
134
135
        return $array;
136
    }
137
138
    protected function parseArray(array $array): array
139
    {
140
        foreach ($array as $key => $value) {
141
            if (
142
                $value instanceof DataTransferObject
143
                || $value instanceof DataTransferObjectCollection
144
            ) {
145
                $array[$key] = $value->toArray();
146
147
                continue;
148
            }
149
150
            if (! is_array($value)) {
151
                continue;
152
            }
153
154
            $array[$key] = $this->parseArray($value);
155
        }
156
157
        return $array;
158
    }
159
160
    /**
161
     * @param \ReflectionClass $class
162
     *
163
     * @return array|\Spatie\DataTransferObject\Property[]
164
     */
165
    protected function getPublicProperties(ReflectionClass $class): array
166
    {
167
        $properties = [];
168
169
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
170
            $properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty);
171
        }
172
173
        return $properties;
174
    }
175
176
    private function makeImmutable(Property $property): DataTransferObject
177
    {
178
        $propertyName = $property->getName();
179
180
        $value = $this->{$propertyName};
181
182
        $property->markImmutable();
183
184
        unset($this->{$propertyName});
185
186
        $this->propertyValues[$propertyName] = $value;
187
188
        $this->propertyDefinitions[$propertyName] = $property;
189
190
        return $this;
191
    }
192
}
193