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 ( 807941...300eda )
by Brent
20s queued 12s
created

DataTransferObject::getFieldValidators()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16

Duplication

Lines 13
Ratio 81.25 %

Importance

Changes 0
Metric Value
dl 13
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 1
nop 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
    /** @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 = new ValueCaster();
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
            if (is_array($value)) {
52
                $value = $valueCaster->cast($value, $validator);
53
            }
54
55
            if (! $validator->isValidType($value)) {
56
                throw DataTransferObjectError::invalidType(
57
                    static::class,
58
                    $field,
59
                    $validator->allowedTypes,
60
                    $value
61
                );
62
            }
63
64
            $this->{$field} = $value;
65
66
            unset($parameters[$field]);
67
        }
68
69
        if (! $this->ignoreMissing && count($parameters)) {
70
            throw DataTransferObjectError::unknownProperties(array_keys($parameters), static::class);
71
        }
72
    }
73
74 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...
75
    {
76
        $data = [];
77
78
        $class = new ReflectionClass(static::class);
79
80
        $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
81
82
        foreach ($properties as $reflectionProperty) {
83
            $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this);
84
        }
85
86
        return $data;
87
    }
88
89
    /**
90
     * @param string ...$keys
91
     *
92
     * @return static
93
     */
94
    public function only(string ...$keys): DataTransferObject
95
    {
96
        $valueObject = clone $this;
97
98
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
99
100
        return $valueObject;
101
    }
102
103
    /**
104
     * @param string ...$keys
105
     *
106
     * @return static
107
     */
108
    public function except(string ...$keys): DataTransferObject
109
    {
110
        $valueObject = clone $this;
111
112
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
113
114
        return $valueObject;
115
    }
116
117
    public function toArray(): array
118
    {
119
        if (count($this->onlyKeys)) {
120
            $array = Arr::only($this->all(), $this->onlyKeys);
121
        } else {
122
            $array = Arr::except($this->all(), $this->exceptKeys);
123
        }
124
125
        $array = $this->parseArray($array);
126
127
        return $array;
128
    }
129
130
    protected function parseArray(array $array): array
131
    {
132
        foreach ($array as $key => $value) {
133
            if (
134
                $value instanceof DataTransferObject
135
                || $value instanceof DataTransferObjectCollection
136
            ) {
137
                $array[$key] = $value->toArray();
138
139
                continue;
140
            }
141
142
            if (! is_array($value)) {
143
                continue;
144
            }
145
146
            $array[$key] = $this->parseArray($value);
147
        }
148
149
        return $array;
150
    }
151
152
    /**
153
     * @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...
154
     *
155
     * @return \Spatie\DataTransferObject\FieldValidator[]
156
     */
157
    private function getFieldValidators(): array
158
    {
159 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...
160
            $class = new ReflectionClass(static::class);
161
162
            $properties = [];
163
164
            foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
165
                $field = $reflectionProperty->getName();
166
167
                $properties[$field] = FieldValidator::fromReflection($reflectionProperty);
168
            }
169
170
            return $properties;
171
        });
172
    }
173
}
174