Attributable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 9
dl 0
loc 31
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fill() 0 3 1
A toArray() 0 7 2
A __toString() 0 3 1
A __get() 0 3 1
A __set() 0 3 1
1
<?php
2
3
namespace Isswp101\Persimmon\Concerns;
4
5
trait Attributable
6
{
7
    private array $attributes;
8
9
    public function __get(string $key): mixed
10
    {
11
        return $this->attributes[$key] ?? null;
12
    }
13
14
    public function __set(string $key, mixed $value): void
15
    {
16
        $this->attributes[$key] = $value;
17
    }
18
19
    public function fill(array $attributes): void
20
    {
21
        $this->attributes = $attributes;
22
    }
23
24
    public function toArray(array $keys = []): array
25
    {
26
        if ($keys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $keys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
27
            return array_intersect_key($this->attributes, array_flip((array)$keys));
28
        }
29
30
        return $this->attributes;
31
    }
32
33
    public function __toString(): string
34
    {
35
        return json_encode($this->toArray());
36
    }
37
}
38