Passed
Push — main ( 62bcc5...88cf22 )
by Gabriel
13:45
created

AsMetadataObject.php$0 ➔ castUsing()   B

Complexity

Conditions 2

Size

Total Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 80
rs 8.4362

6 Methods

Rating   Name   Duplication   Size   Complexity  
A AsMetadataObject.php$0 ➔ set() 0 9 3
A AsMetadataObject.php$0 ➔ serialize() 0 3 1
A AsMetadataObject.php$0 ➔ encode() 0 6 3
A AsMetadataObject.php$0 ➔ get() 0 13 3
A AsMetadataObject.php$0 ➔ decode() 0 6 2
A AsMetadataObject.php$0 ➔ __construct() 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 = isset($arguments[0]) ? $arguments[0] : null;
24
        return new class($encoder) implements CastsAttributes {
0 ignored issues
show
Bug Best Practice introduced by
The expression return new ClassNode($encoder) 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...
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)
61
            {
62
                if (is_string($value)) {
63
                    return [$key => $value];
64
                }
65
                if ($value instanceof Metadata) {
66
                    $value = $this->encode($value);
67
                }
68
                return [$key => $value];
69
            }
70
71
            /**
72
             * @inheritDoc
73
             */
74
            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

74
            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

74
            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

74
            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...
75
            {
76
                return $value->getArrayCopy();
77
            }
78
79
            /**
80
             * @param $value
81
             * @return false|string
82
             */
83
            protected function encode($value)
84
            {
85
                if ($this->encoder == 'serialize') {
86
                    return $value instanceof Metadata ? $value->serialize() : serialize($value);
87
                }
88
                return json_encode($value);
89
            }
90
91
            /**
92
             * @param $value
93
             * @return mixed
94
             */
95
            protected function decode($value)
96
            {
97
                if ($this->encoder == 'serialize') {
98
                    return unserialize($value);
99
                }
100
                return Json::decode($value, true);
101
            }
102
        };
103
    }
104
}
105