Passed
Push — main ( c2020d...c060b3 )
by Gabriel
13:36
created

AsArrayObject.php$0 ➔ castUsing()   B

Complexity

Conditions 2

Size

Total Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 73
rs 8.589
c 0
b 0
f 0

6 Methods

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

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;
4
5
/**
6
 * Class AsArrayObject
7
 * @package ByTIC\DataObjects\Casts
8
 */
9
class AsArrayObject implements Castable
10
{
11
    /**
12
     * Get the caster class to use when casting from / to this cast target.
13
     *
14
     * @param array $arguments
15
     * @return object|string
16
     */
17
    public static function castUsing(array $arguments)
18
    {
19
        $encoder = isset($arguments[0]) ? $arguments[0] : null;
20
        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/AsArrayObject.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...
21
22
            protected $encoder = 'json';
23
24
            /**
25
             *  constructor.
26
             * @param $encoder
27
             */
28
            public function __construct($encoder)
29
            {
30
                $this->encoder = $encoder;
31
            }
32
33
            /**
34
             * @inheritDoc
35
             */
36
            public function get($model, $key, $value, $attributes): ArrayObject
37
            {
38
                if (empty($value)) {
39
                    $value = [];
40
                } else {
41
                    $value = $this->decode($value);
42
                }
43
                return new ArrayObject($value);
44
            }
45
46
            /**
47
             * @inheritDoc
48
             */
49
            public function set($model, $key, $value, $attributes)
50
            {
51
                if (is_string($value)) {
52
                    return [$key => $value];
53
                }
54
                if ($value instanceof ArrayObject) {
55
                    $value = $this->encode($value);
56
                }
57
                return [$key => $value];
58
            }
59
60
            /**
61
             * @inheritDoc
62
             */
63
            public function serialize($model, string $key, $value, array $attributes)
0 ignored issues
show
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

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

63
            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

63
            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...
64
            {
65
                return $value->getArrayCopy();
66
            }
67
68
            /**
69
             * @param $value
70
             * @return false|string
71
             */
72
            protected function encode($value)
73
            {
74
                if ($this->encoder == 'serialize') {
75
                    return $value instanceof ArrayObject ? $value->serialize() : serialize($value);
76
                }
77
                return json_encode($value);
78
            }
79
80
            /**
81
             * @param $value
82
             * @return mixed
83
             */
84
            protected function decode($value)
85
            {
86
                if ($this->encoder == 'serialize') {
87
                    return unserialize($value);
88
                }
89
                return json_decode($value, true);
90
            }
91
        };
92
    }
93
}
94