Attributable::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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