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
|
|||
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 |
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.