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 (#80)
by
unknown
01:14
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 bool */
13
    protected $ignoreMissing = false;
14
15
    /** @var array */
16
    protected $exceptKeys = [];
17
18
    /** @var array */
19
    protected $onlyKeys = [];
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
        $validators = $this->getFieldValidators();
34
35
        $valueCaster = $this->getValueCaster();
36
37
        foreach ($validators as $field => $validator) {
38
            if (
39
                ! isset($parameters[$field])
40
                && ! $validator->hasDefaultValue
41
                && ! $validator->isNullable
42
            ) {
43
                throw DataTransferObjectError::uninitialized(
44
                    static::class,
45
                    $field
46
                );
47
            }
48
49
            $value = $parameters[$field] ?? $this->{$field} ?? null;
50
51
            $value = $this->castValue($valueCaster, $validator, $value);
52
53
            if (! $validator->isValidType($value)) {
54
                throw DataTransferObjectError::invalidType(
55
                    static::class,
56
                    $field,
57
                    $validator->allowedTypes,
58
                    $value
59
                );
60
            }
61
62
            $this->{$field} = $value;
63
64
            unset($parameters[$field]);
65
        }
66
67
        if (! $this->ignoreMissing && count($parameters)) {
68
            throw DataTransferObjectError::unknownProperties(array_keys($parameters), static::class);
69
        }
70
    }
71
72 View Code Duplication
    public function all(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $data = [];
75
76
        $class = new ReflectionClass(static::class);
77
78
        $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
79
80
        foreach ($properties as $reflectionProperty) {
81
            $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this);
82
        }
83
84
        return $data;
85
    }
86
87
    /**
88
     * @param string ...$keys
89
     *
90
     * @return static
91
     */
92
    public function only(string ...$keys): DataTransferObject
93
    {
94
        $valueObject = clone $this;
95
96
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
97
98
        return $valueObject;
99
    }
100
101
    /**
102
     * @param string ...$keys
103
     *
104
     * @return static
105
     */
106
    public function except(string ...$keys): DataTransferObject
107
    {
108
        $valueObject = clone $this;
109
110
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
111
112
        return $valueObject;
113
    }
114
115
    public function toArray(): array
116
    {
117
        if (count($this->onlyKeys)) {
118
            $array = Arr::only($this->all(), $this->onlyKeys);
119
        } else {
120
            $array = Arr::except($this->all(), $this->exceptKeys);
121
        }
122
123
        $array = $this->parseArray($array);
124
125
        return $array;
126
    }
127
128
    protected function parseArray(array $array): array
129
    {
130
        foreach ($array as $key => $value) {
131
            if (
132
                $value instanceof DataTransferObject
133
                || $value instanceof DataTransferObjectCollection
134
            ) {
135
                $array[$key] = $value->toArray();
136
137
                continue;
138
            }
139
140
            if (! is_array($value)) {
141
                continue;
142
            }
143
144
            $array[$key] = $this->parseArray($value);
145
        }
146
147
        return $array;
148
    }
149
150
    /**
151
     * @param \ReflectionClass $class
0 ignored issues
show
Bug introduced by
There is no parameter named $class. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
152
     *
153
     * @return \Spatie\DataTransferObject\FieldValidator[]
154
     */
155
    private function getFieldValidators(): array
156
    {
157 View Code Duplication
        return DTOCache::resolve(static::class, function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
            $class = new ReflectionClass(static::class);
159
160
            $properties = [];
161
162
            foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
163
                $field = $reflectionProperty->getName();
164
165
                $properties[$field] = FieldValidator::fromReflection($reflectionProperty);
166
            }
167
168
            return $properties;
169
        });
170
    }
171
172
    /**
173
     * @param \Spatie\DataTransferObject\ValueCaster $valueCaster
174
     * @param \Spatie\DataTransferObject\FieldValidator $fieldValidator
175
     * @param mixed $value
176
     * @return mixed
177
     */
178
    protected function castValue(ValueCaster $valueCaster, FieldValidator $fieldValidator, $value)
179
    {
180
        if (is_array($value)) {
181
            return $valueCaster->cast($value, $fieldValidator);
182
        }
183
184
        return $value;
185
    }
186
187
    protected function getValueCaster(): ValueCaster
188
    {
189
        return new ValueCaster();
190
    }
191
}
192