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 ( a74825...53104a )
by Brent
01:34 queued 11s
created

DataTransferObject   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 202
Duplicated Lines 15.84 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 6
dl 32
loc 202
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A immutable() 0 4 1
A arrayOf() 0 9 1
B __construct() 0 40 8
A all() 14 14 2
A only() 0 8 1
A except() 0 8 1
A toArray() 0 12 2
A parseArray() 0 21 5
A getFieldValidators() 18 21 3
A castValue() 0 8 2
A getValueCaster() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    /**
32
     * @param array $arrayOfParameters
33
     *
34
     * @return \Spatie\DataTransferObject\ImmutableDataTransferObject[]|static[]
35
     */
36
    public static function arrayOf(array $arrayOfParameters): array
37
    {
38
        return array_map(
39
            function ($parameters) {
40
                return new static($parameters);
41
            },
42
            $arrayOfParameters
43
        );
44
    }
45
46
    public function __construct(array $parameters = [])
47
    {
48
        $validators = $this->getFieldValidators();
49
50
        $valueCaster = $this->getValueCaster();
51
52
        foreach ($validators as $field => $validator) {
53
            if (
54
                ! isset($parameters[$field])
55
                && ! $validator->hasDefaultValue
56
                && ! $validator->isNullable
57
            ) {
58
                throw DataTransferObjectError::uninitialized(
59
                    static::class,
60
                    $field
61
                );
62
            }
63
64
            $value = $parameters[$field] ?? $this->{$field} ?? null;
65
66
            $value = $this->castValue($valueCaster, $validator, $value);
67
68
            if (! $validator->isValidType($value)) {
69
                throw DataTransferObjectError::invalidType(
70
                    static::class,
71
                    $field,
72
                    $validator->allowedTypes,
73
                    $value
74
                );
75
            }
76
77
            $this->{$field} = $value;
78
79
            unset($parameters[$field]);
80
        }
81
82
        if (! $this->ignoreMissing && count($parameters)) {
83
            throw DataTransferObjectError::unknownProperties(array_keys($parameters), static::class);
84
        }
85
    }
86
87 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...
88
    {
89
        $data = [];
90
91
        $class = new ReflectionClass(static::class);
92
93
        $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
94
95
        foreach ($properties as $reflectionProperty) {
96
            $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this);
97
        }
98
99
        return $data;
100
    }
101
102
    /**
103
     * @param string ...$keys
104
     *
105
     * @return static
106
     */
107
    public function only(string ...$keys): DataTransferObject
108
    {
109
        $valueObject = clone $this;
110
111
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
112
113
        return $valueObject;
114
    }
115
116
    /**
117
     * @param string ...$keys
118
     *
119
     * @return static
120
     */
121
    public function except(string ...$keys): DataTransferObject
122
    {
123
        $valueObject = clone $this;
124
125
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
126
127
        return $valueObject;
128
    }
129
130
    public function toArray(): array
131
    {
132
        if (count($this->onlyKeys)) {
133
            $array = Arr::only($this->all(), $this->onlyKeys);
134
        } else {
135
            $array = Arr::except($this->all(), $this->exceptKeys);
136
        }
137
138
        $array = $this->parseArray($array);
139
140
        return $array;
141
    }
142
143
    protected function parseArray(array $array): array
144
    {
145
        foreach ($array as $key => $value) {
146
            if (
147
                $value instanceof DataTransferObject
148
                || $value instanceof DataTransferObjectCollection
149
            ) {
150
                $array[$key] = $value->toArray();
151
152
                continue;
153
            }
154
155
            if (! is_array($value)) {
156
                continue;
157
            }
158
159
            $array[$key] = $this->parseArray($value);
160
        }
161
162
        return $array;
163
    }
164
165
    /**
166
     * @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...
167
     *
168
     * @return \Spatie\DataTransferObject\FieldValidator[]
169
     */
170
    protected function getFieldValidators(): array
171
    {
172 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...
173
            $class = new ReflectionClass(static::class);
174
175
            $properties = [];
176
177
            foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
178
                // Skip static properties
179
                if ($reflectionProperty->isStatic()) {
180
                    continue;
181
                }
182
183
                $field = $reflectionProperty->getName();
184
185
                $properties[$field] = FieldValidator::fromReflection($reflectionProperty);
186
            }
187
188
            return $properties;
189
        });
190
    }
191
192
    /**
193
     * @param \Spatie\DataTransferObject\ValueCaster $valueCaster
194
     * @param \Spatie\DataTransferObject\FieldValidator $fieldValidator
195
     * @param mixed $value
196
     * @return mixed
197
     */
198
    protected function castValue(ValueCaster $valueCaster, FieldValidator $fieldValidator, $value)
199
    {
200
        if (is_array($value)) {
201
            return $valueCaster->cast($value, $fieldValidator);
202
        }
203
204
        return $value;
205
    }
206
207
    protected function getValueCaster(): ValueCaster
208
    {
209
        return new ValueCaster();
210
    }
211
}
212