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 (#55)
by Arthur
02:02
created

DataTransferObject::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
    protected $exceptKeys = [];
14
15
    /** @var array */
16
    protected $onlyKeys = [];
17
18
    /** @var Property[] | array */
19
    protected $properties = [];
20
21
    /**
22
     * @param array $parameters
23
     *
24
     * @return \Spatie\DataTransferObject\ImmutableDataTransferObject|static
25
     */
26
    public static function immutable(array $parameters): ImmutableDataTransferObject
27
    {
28
        return new ImmutableDataTransferObject(new static($parameters));
29
    }
30
31
    public function __construct(array $parameters)
32
    {
33
        $class = new ReflectionClass(static::class);
34
35
        $properties = $this->getPublicProperties($class);
36
37
        foreach ($properties as $property) {
38
            $property->setDefault($property->getValue($this));
39
            $method = $property->getName();
40
41
            if (method_exists($this, $method)) {
42
                $factory = new Attribute($property);
43
                $property = $this->$method($factory)->getProperty();
44
            }
45
            $default = $property->getDefault();
46
            if (
47
                !isset($parameters[$property->getName()])
48
                && !isset($default)
49
                && !$property->isNullable()
50
                && $property->isRequired()
51
            ) {
52
                throw DataTransferObjectError::uninitialized($property);
53
            }
54
55
            if (array_key_exists($property->getName(), $parameters)) {
56
                $property->set($parameters[$property->getName()]);
57
            } else {
58
                $property->setUninitialized();
59
            }
60
61
            $this->properties[$property->getName()] = $property;
62
63
            unset($parameters[$property->getName()]);
64
            unset($this->{$property->getName()});
65
        }
66
67
68
        if (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
    public function __set($name, $value)
89
    {
90
        throw DataTransferObjectError::immutable($name);
91
    }
92
93
    public function __get($name)
94
    {
95
        $properties = $this->properties[$name];
96
        $value = $properties->getActualValue();
97
        return $value;
98
    }
99
100
    /**
101
     * @param string ...$keys
102
     *
103
     * @return static
104
     */
105
    public function only(string ...$keys): DataTransferObject
106
    {
107
        $valueObject = clone $this;
108
109
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
110
111
        return $valueObject;
112
    }
113
114
    /**
115
     * @param string ...$keys
116
     *
117
     * @return static
118
     */
119
    public function except(string ...$keys): DataTransferObject
120
    {
121
        $valueObject = clone $this;
122
123
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
124
125
        return $valueObject;
126
    }
127
128
    public function toArray(): array
129
    {
130
        if (count($this->onlyKeys)) {
131
            $array = Arr::only($this->all(), $this->onlyKeys);
132
        } else {
133
            $array = Arr::except($this->all(), $this->exceptKeys);
134
        }
135
136
        $array = $this->parseArray($array);
137
138
        return $array;
139
    }
140
141
    protected function parseArray(array $array): array
142
    {
143
        foreach ($array as $key => $value) {
144
            if (
145
                $value instanceof DataTransferObject
146
                || $value instanceof DataTransferObjectCollection
147
            ) {
148
                $array[$key] = $value->toArray();
149
150
                continue;
151
            }
152
153
            if (!is_array($value)) {
154
                continue;
155
            }
156
157
            $array[$key] = $this->parseArray($value);
158
        }
159
160
        return $array;
161
    }
162
163
    /**
164
     * @param \ReflectionClass $class
165
     *
166
     * @return array|\Spatie\DataTransferObject\Property[]
167
     */
168
    protected function getPublicProperties(ReflectionClass $class): array
169
    {
170
        $properties = [];
171
172
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
173
            $properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty);
174
        }
175
176
        return $properties;
177
    }
178
179
    protected function propertyIsOptional($property)
180
    {
181
        $isOptionalMethod = $property->getName() . "IsOptional";
182
        return method_exists($this, $isOptionalMethod) && is_bool($this->$isOptionalMethod()) ? $this->$isOptionalMethod() : false;
183
    }
184
}
185