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 ( 9d8638...60b218 )
by Brent
16s
created

DataTransferObject   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 3
dl 0
loc 127
rs 10
c 0
b 0
f 0

7 Methods

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