AsMetadataObject.php$0 ➔ encode()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
c 1
b 0
f 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace ByTIC\DataObjects\Casts\Metadata;
4
5
use ByTIC\DataObjects\Casts\Castable;
6
use ByTIC\DataObjects\Casts\CastsAttributes;
7
use Nip\Utility\Json;
8
9
/**
10
 * Class AsMetadataObject
11
 * @package ByTIC\DataObjects\Casts\Metadata
12
 */
13
class AsMetadataObject implements Castable
14
{
15
    /**
16
     * Get the caster class to use when casting from / to this cast target.
17
     *
18
     * @param array $arguments
19
     * @return object|string
20
     */
21
    public static function castUsing(array $arguments)
22
    {
23
        $encoder = $arguments[0] ?? null;
24
        $metadataClass = $arguments[1] ?? Metadata::class;
25
26
        return new class($encoder, $metadataClass) implements CastsAttributes {
0 ignored issues
show
Bug Best Practice introduced by
The expression return new ClassNode($encoder, $metadataClass) returns the type anonymous//src/Casts/Met.../AsMetadataObject.php$0 which is incompatible with the return type mandated by ByTIC\DataObjects\Casts\Castable::castUsing() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
27
28
            protected $encoder = 'json';
29
            protected $metadataClass = Metadata::class;
30
31
            /**
32
             *  constructor.
33
             * @param $encoder
34
             */
35
            public function __construct($encoder, $metadataClass)
36
            {
37
                $this->encoder = $encoder;
38
                $this->metadataClass = $metadataClass;
39
            }
40
41
            /**
42
             * @inheritDoc
43
             */
44
            public function get($model, $key, $value, $attributes): Metadata
45
            {
46
                if (empty($value)) {
47
                    $value = [];
48
                } else {
49
                    $value = $this->decode($value);
50
                }
51
                if (!is_array($value)) {
52
                    $value = [];
53
                }
54
                return (new $this->metadataClass($value))
55
                    ->setObserver(
56
                        function (Metadata $metadata) use ($model, $key) {
57
                            $model->set($key, $metadata);
58
                        }
59
                    );
60
            }
61
62
            /**
63
             * @inheritDoc
64
             */
65
            public function set($model, $key, $value, $attributes)
66
            {
67
                if (is_string($value)) {
68
                    return [$key => $value];
69
                }
70
                if ($value instanceof Metadata) {
71
                    $value = $this->encode($value);
72
                }
73
                return [$key => $value];
74
            }
75
76
            /**
77
             * @inheritDoc
78
             */
79
            public function serialize($model, string $key, $value, array $attributes)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
            public function serialize($model, /** @scrutinizer ignore-unused */ string $key, $value, array $attributes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
            public function serialize(/** @scrutinizer ignore-unused */ $model, string $key, $value, array $attributes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attributes is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
            public function serialize($model, string $key, $value, /** @scrutinizer ignore-unused */ array $attributes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
            {
81
                return $value->getArrayCopy();
82
            }
83
84
            /**
85
             * @param $value
86
             * @return false|string
87
             */
88
            protected function encode($value)
89
            {
90
                if ($this->encoder == 'serialize') {
91
                    return $value instanceof Metadata ? $value->serialize() : serialize($value);
92
                }
93
                return json_encode($value);
94
            }
95
96
            /**
97
             * @param $value
98
             * @return mixed
99
             */
100
            protected function decode($value)
101
            {
102
                if ($this->encoder == 'serialize') {
103
                    return unserialize($value);
104
                }
105
                return Json::decode($value, true);
106
            }
107
        };
108
    }
109
}
110