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 (#55)
by Arthur
02:21 queued 55s
created

DataTransferObject::__get()   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
/**
11
 * Class DataTransferObject.
12
 */
13
abstract class DataTransferObject
14
{
15
    /** @var array */
16
    protected $exceptKeys = [];
17
18
    /** @var array */
19
    protected $onlyKeys = [];
20
21
    /** @var Property[] | array */
22
    protected $properties = [];
23
24
    /** @var bool */
25
    protected $immutable;
26
27
    /**
28
     * @param array $parameters
29
     *
30
     * @return \Spatie\DataTransferObject\ImmutableDataTransferObject|static
31
     */
32
    public static function mutable(array $parameters): self
33
    {
34
        return new static($parameters, false);
35
    }
36
37
    /**
38
     * @param array $parameters
39
     *
40
     * @return \Spatie\DataTransferObject\ImmutableDataTransferObject|static
41
     */
42
    public static function immutable(array $parameters): self
43
    {
44
        return new static($parameters, true);
45
    }
46
47
    /**
48
     * DataTransferObject constructor.
49
     * @param array $parameters
50
     */
51
    final public function __construct(array $parameters, $immutable = true)
52
    {
53
        $this->immutable = $immutable;
54
        $this->boot($parameters);
55
    }
56
57
    /**
58
     * @param array $parameters
59
     */
60
    protected function boot(array $parameters): void
61
    {
62
        foreach ($this->getPublicProperties() as $property) {
63
64
            /* Setting the default value of the property */
65
            $property->setDefault($property->getValue($this));
66
67
            /* If a attribute method is set on the dto process it */
68
            if (method_exists($this, $method = $property->getName())) {
69
                $property = $this->$method(new Attribute($property))->getProperty();
70
            }
71
72
            /* Check if property passes the basic conditions */
73
            if (! array_key_exists($property->getName(), $parameters)
74
                && $property->isRequired()
75
                && is_null($property->getDefault())
76
                && ! $property->isNullable()
77
            ) {
78
                throw DataTransferObjectError::uninitialized($property);
79
            }
80
81
            /* set the value if it's present in the array and mark it as uninitialized otherwise */
82
            if (array_key_exists($property->getName(), $parameters)) {
83
                $property->set($parameters[$property->getName()]);
84
            } else {
85
                $property->setUninitialized();
86
            }
87
88
            /* add the property to an associative array with the name as key */
89
            $this->properties[$property->getName()] = $property;
90
91
            /* remove the property from the parameters array  */
92
            unset($parameters[$property->getName()]);
93
            /* remove the property from the dto  */
94
            unset($this->{$property->getName()});
95
        }
96
97
        /* Check if there are additional parameters left.
98
         * Throw error if there are.
99
         * Additional properties are not allowed in a dto.
100
         */
101
        if (count($parameters)) {
102
            throw DataTransferObjectError::unknownProperties(array_keys($parameters), static::class);
103
        }
104
105
        $this->validate();
106
    }
107
108
    protected function validate()
109
    {
110
        //IMPLEMENT VALIDATION FUNCTIONALITY CHECK THE RULES & CONSTRAINTS
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function all(): array
117
    {
118
        $data = [];
119
120
        foreach ($this->properties as $property) {
121
            $data[$property->getName()] = $property->getActualValue();
0 ignored issues
show
Documentation Bug introduced by
The method getName does not exist on object<Spatie\DataTransferObject\Property>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
122
        }
123
124
        return $data;
125
    }
126
127
    /**
128
     * Immutable behavior
129
     * Throw error if a user tries to set a property.
130
     * @param $name
131
     * @param $value
132
     * @Throws DataTransferObjectError
133
     */
134
    public function __set($name, $value)
135
    {
136
        if ($this->immutable) {
137
            throw DataTransferObjectError::immutable($name);
138
        }
139
    }
140
141
    /**
142
     * Proxy through to the properties array.
143
     * @param $name
144
     * @return mixed
145
     */
146
    public function __get($name)
147
    {
148
        return $this->properties[$name]->getActualValue();
149
    }
150
151
    /**
152
     * @param string ...$keys
153
     *
154
     * @return static
155
     */
156
    public function only(string ...$keys): DataTransferObject
157
    {
158
        $valueObject = clone $this;
159
160
        $valueObject->onlyKeys = array_merge($this->onlyKeys, $keys);
161
162
        return $valueObject;
163
    }
164
165
    /**
166
     * @param string ...$keys
167
     *
168
     * @return static
169
     */
170
    public function except(string ...$keys): DataTransferObject
171
    {
172
        $valueObject = clone $this;
173
174
        $valueObject->exceptKeys = array_merge($this->exceptKeys, $keys);
175
176
        return $valueObject;
177
    }
178
179
    /**
180
     * @return array
181
     */
182
    public function toArray(): array
183
    {
184
        if (count($this->onlyKeys)) {
185
            $array = Arr::only($this->all(), $this->onlyKeys);
186
        } else {
187
            $array = Arr::except($this->all(), $this->exceptKeys);
188
        }
189
190
        $array = $this->parseArray($array);
191
192
        return $array;
193
    }
194
195
    /**
196
     * @param array $array
197
     * @return array
198
     */
199
    protected function parseArray(array $array): array
200
    {
201
        foreach ($array as $key => $value) {
202
            if (
203
                $value instanceof DataTransferObject
204
                || $value instanceof DataTransferObjectCollection
205
            ) {
206
                $array[$key] = $value->toArray();
207
208
                continue;
209
            }
210
211
            if (! is_array($value)) {
212
                continue;
213
            }
214
215
            $array[$key] = $this->parseArray($value);
216
        }
217
218
        return $array;
219
    }
220
221
    /**
222
     * @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...
223
     *
224
     * @return array|\Spatie\DataTransferObject\Property[]
225
     */
226
    protected function getPublicProperties(): array
227
    {
228
        $class = new ReflectionClass(static::class);
229
        $properties = [];
230
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
231
            $properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty);
232
        }
233
234
        return $properties;
235
    }
236
}
237