Completed
Push — master ( 3db7ff...8c62aa )
by Bas
04:43 queued 02:03
created

ParameterBagHandler::getParameterType()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace DMT\Serializer;
4
5
use JMS\Serializer\Context;
6
use JMS\Serializer\Exception\RuntimeException;
7
use JMS\Serializer\GraphNavigator;
8
use JMS\Serializer\Handler\SubscribingHandlerInterface;
9
use JMS\Serializer\JsonDeserializationVisitor;
10
use JMS\Serializer\JsonSerializationVisitor;
11
use JMS\Serializer\VisitorInterface;
12
use phpDocumentor\Reflection\Types\Object_;
13
14
class ParameterBagHandler implements SubscribingHandlerInterface
15
{
16 2
    public static function getSubscribingMethods(): array
17
    {
18
        return [
19
            [
20 2
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
21 2
                'format' => 'json',
22 2
                'type' => 'ParameterBag',
23 2
                'method' => 'serializeParameterBag',
24
            ],
25
            [
26 2
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
27 2
                'format' => 'json',
28 2
                'type' => 'ParameterBag',
29 2
                'method' => 'deserializeParameterBag',
30
            ],
31
        ];
32
    }
33
34
    /**
35
     * @param JsonSerializationVisitor|VisitorInterface $visitor
36
     * @param ParameterBag|null $data
37
     * @param array $type
38
     * @param Context $context
39
     *
40
     * @return array|null
41
     * @throws RuntimeException
42
     */
43 25
    public function serializeParameterBag(
44
        VisitorInterface $visitor, ? ParameterBag $data, array $type, Context $context
0 ignored issues
show
Unused Code introduced by
The parameter $type 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

44
        VisitorInterface $visitor, ? ParameterBag $data, /** @scrutinizer ignore-unused */ array $type, Context $context

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 $visitor 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

44
        /** @scrutinizer ignore-unused */ VisitorInterface $visitor, ? ParameterBag $data, array $type, Context $context

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...
45
    ): ? array
46
    {
47 25
        $result = [];
48 25
        if ($data === null) {
49 6
            return $context->shouldSerializeNull() ? $result : null;
50
        }
51
52 19
        foreach ($data as $parameter) {
53 13
            if (!$context->shouldSerializeNull() && $parameter->getValue() === null) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $context->shouldSerializeNull() of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
54 6
                continue;
55
            }
56 13
            $result[$parameter->getName()] = $parameter->getValue();
57
        }
58
59 19
        return count($result) || $context->shouldSerializeNull() ? $result : null;
60
    }
61
62
    /**
63
     * Deserialize elements into a bag of parameters.
64
     *
65
     * @param JsonDeserializationVisitor|VisitorInterface $visitor
66
     * @param array $data
67
     * @param array $type
68
     * @param Context $context
69
     *
70
     * @return ParameterBag
71
     * @throws RuntimeException
72
     */
73 25
    public function deserializeParameterBag(
74
        VisitorInterface $visitor, $data = null, array $type, Context $context
0 ignored issues
show
Unused Code introduced by
The parameter $visitor 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
        /** @scrutinizer ignore-unused */ VisitorInterface $visitor, $data = null, array $type, Context $context

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
    ): ParameterBag
76
    {
77 25
        $result = new ParameterBag();
78 25
        if (null === $data) {
79 6
            return $result;
80
        }
81 19
        if (!is_iterable($data)) {
82 4
            throw new RuntimeException('ParameterBag only excepts an array of parameters');
83
        }
84
85
        /** @var ParameterInterface $parameter */
86 15
        $parameterType = $this->getParameterType($type);
87 13
        $parameter = unserialize(sprintf('O:%d:"%s":0:{}', strlen($parameterType), $parameterType));
88
89 13
        foreach ($data as $key => $value) {
90 13
            if (!$context->shouldSerializeNull() && $value === null) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $context->shouldSerializeNull() of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
91 6
                continue;
92
            }
93 13
            $instance = clone $parameter;
94 13
            $instance->setName($key);
95 13
            $instance->setValue($value);
96
97 13
            $result[] = $instance;
98
        }
99
100 13
        return $result;
101
    }
102
103
    /**
104
     * Get the class name of the object to store in parameter bag.
105
     *
106
     * @param array $type
107
     *
108
     * @return string The class name for the parameters.
109
     * @throws RuntimeException
110
     */
111 15
    protected function getParameterType(array $type): string
112
    {
113 15
        if (empty($type['params'])) {
114 5
            return Parameter::class;
115
        }
116
117 10
        $parameterType = $type['params'][0]['name'] ?? $type['params'][0];
118 10
        if (!class_exists($parameterType) || !is_subclass_of($parameterType, ParameterInterface::class)) {
119 2
            throw new RuntimeException('Parameter(s) must implement ' . ParameterInterface::class);
120
        }
121
122 8
        return $parameterType;
123
    }
124
}
125