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
Push — master ( 2bf407...746599 )
by Brent
14s
created

DataTransferObject::__construct()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8817
c 0
b 0
f 0
cc 6
nc 5
nop 1
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
    public function __construct(array $parameters)
19
    {
20
        $class = new ReflectionClass(static::class);
21
22
        $properties = $this->getPublicProperties($class);
23
24
        foreach ($properties as $property) {
25
            if (
26
                ! isset($parameters[$property->getName()])
27
                && ! $property->isDefault()
28
                && ! $property->isNullable()
29
            ) {
30
                throw DataTransferObjectError::uninitialized($property);
31
            }
32
33
            $value = $parameters[$property->getName()] ?? $property->getValue($this);
34
35
            $property->set($value);
36
37
            unset($parameters[$property->getName()]);
38
        }
39
40
        if (count($parameters)) {
41
            throw DataTransferObjectError::unknownProperties(array_keys($parameters), $class->getName());
42
        }
43
    }
44
45
    public function all(): array
46
    {
47
        $data = [];
48
49
        $class = new ReflectionClass(static::class);
50
51
        $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
52
53
        foreach ($properties as $reflectionProperty) {
54
            $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this);
55
        }
56
57
        return $data;
58
    }
59
60
    /**
61
     * @param string ...$keys
62
     *
63
     * @return static
64
     */
65
    public function only(string ...$keys): DataTransferObject
66
    {
67
        $valueObject = clone $this;
68
69
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
70
71
        return $valueObject;
72
    }
73
74
    /**
75
     * @param string ...$keys
76
     *
77
     * @return static
78
     */
79
    public function except(string ...$keys): DataTransferObject
80
    {
81
        $valueObject = clone $this;
82
83
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
84
85
        return $valueObject;
86
    }
87
88
    public function toArray(): array
89
    {
90
        if (count($this->onlyKeys)) {
91
            $array = Arr::only($this->all(), $this->onlyKeys);
92
        } else {
93
            $array = Arr::except($this->all(), $this->exceptKeys);
94
        }
95
96
        $array = $this->parseArray($array);
97
98
        return $array;
99
    }
100
101
    protected function parseArray(array $array): array
102
    {
103
        foreach ($array as $key => $value) {
104
            if (
105
                $value instanceof DataTransferObject
106
                || $value instanceof DataTransferObjectCollection
107
            ) {
108
                $array[$key] = $value->toArray();
109
110
                continue;
111
            }
112
113
            if (! is_array($value)) {
114
                continue;
115
            }
116
117
            $array[$key] = $this->parseArray($value);
118
        }
119
120
        return $array;
121
    }
122
123
    /**
124
     * @param \ReflectionClass $class
125
     *
126
     * @return array|\Spatie\DataTransferObject\Property[]
127
     */
128
    protected function getPublicProperties(ReflectionClass $class): array
129
    {
130
        $properties = [];
131
132
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
133
            $properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty);
134
        }
135
136
        return $properties;
137
    }
138
}
139