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