Total Complexity | 13 |
Total Lines | 77 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
24 | return new class($encoder) implements CastsAttributes { |
||
|
|||
25 | |||
26 | protected $encoder = 'json'; |
||
27 | |||
28 | /** |
||
29 | * constructor. |
||
30 | * @param $encoder |
||
31 | */ |
||
32 | public function __construct($encoder) |
||
33 | { |
||
34 | $this->encoder = $encoder; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @inheritDoc |
||
39 | */ |
||
40 | public function get($model, $key, $value, $attributes): Metadata |
||
41 | { |
||
42 | if (empty($value)) { |
||
43 | $value = []; |
||
44 | } else { |
||
45 | $value = $this->decode($value); |
||
46 | } |
||
47 | if (!is_array($value)) { |
||
48 | $value = []; |
||
49 | } |
||
50 | return (new Metadata($value))->setObserver( |
||
51 | function (Metadata $metadata) use ($model, $key) { |
||
52 | $model->set($key, $metadata); |
||
53 | } |
||
54 | ); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @inheritDoc |
||
59 | */ |
||
60 | public function set($model, $key, $value, $attributes) |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @inheritDoc |
||
73 | */ |
||
74 | public function serialize($model, string $key, $value, array $attributes) |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param $value |
||
81 | * @return false|string |
||
82 | */ |
||
83 | protected function encode($value) |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param $value |
||
93 | * @return mixed |
||
94 | */ |
||
95 | protected function decode($value) |
||
101 | } |
||
102 | }; |
||
105 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: