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 ( 724f07...891eea )
by Brent
11s queued 10s
created

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