Passed
Pull Request — master (#1546)
by
unknown
11:49
created

UnionHandler   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 95
rs 10
c 1
b 0
f 0
wmc 21

6 Methods

Rating   Name   Duplication   Size   Complexity  
A serializeUnion() 0 7 1
A getSubscribingMethods() 0 21 2
A matchSimpleType() 0 12 5
A deserializeUnion() 0 7 2
A determineType() 0 9 3
B testPrimitive() 0 20 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Handler;
6
7
use JMS\Serializer\Context;
8
use JMS\Serializer\DeserializationContext;
9
use JMS\Serializer\Exception\RuntimeException;
10
use JMS\Serializer\GraphNavigatorInterface;
11
use JMS\Serializer\SerializationContext;
12
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
13
use JMS\Serializer\Visitor\SerializationVisitorInterface;
14
15
final class UnionHandler implements SubscribingHandlerInterface
16
{
17
    private static $aliases = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float'];
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public static function getSubscribingMethods()
23
    {
24
        $methods = [];
25
        $formats = ['json', 'xml'];
26
27
        foreach ($formats as $format) {
28
            $methods[] = [
29
                'type' => 'union',
30
                'format' => $format,
31
                'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
32
                'method' => 'deserializeUnion',
33
            ];
34
            $methods[] = [
35
                'type' => 'union',
36
                'format' => $format,
37
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
38
                'method' => 'serializeUnion',
39
            ];
40
        }
41
42
        return $methods;
43
    }
44
45
    public function serializeUnion(
46
        SerializationVisitorInterface $visitor,
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

46
        /** @scrutinizer ignore-unused */ SerializationVisitorInterface $visitor,

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...
47
        mixed $data,
48
        array $type,
49
        SerializationContext $context
50
    ) {
51
        return $this->matchSimpleType($data, $type, $context);
52
    }
53
54
    public function deserializeUnion(DeserializationVisitorInterface $visitor, mixed $data, array $type, DeserializationContext $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

54
    public function deserializeUnion(/** @scrutinizer ignore-unused */ DeserializationVisitorInterface $visitor, mixed $data, array $type, DeserializationContext $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...
55
    {
56
        if ($data instanceof \SimpleXMLElement) {
57
            throw new RuntimeException('XML deserialisation into union types is not supported yet.');
58
        }
59
60
        return $this->matchSimpleType($data, $type, $context);
61
    }
62
63
    private function matchSimpleType(mixed $data, array $type, Context $context)
64
    {
65
        $dataType = $this->determineType($data, $type, $context->getFormat());
66
        $alternativeName = null;
67
68
        if (isset(static::$aliases[$dataType])) {
0 ignored issues
show
Bug introduced by
Since $aliases is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $aliases to at least protected.
Loading history...
69
            $alternativeName = static::$aliases[$dataType];
70
        }
71
72
        foreach ($type['params'] as $possibleType) {
73
            if ($possibleType['name'] === $dataType || $possibleType['name'] === $alternativeName) {
74
                return $context->getNavigator()->accept($data, $possibleType);
75
            }
76
        }
77
    }
78
79
    private function determineType(mixed $data, array $type, string $format): ?string
80
    {
81
        foreach ($type['params'] as $possibleType) {
82
            if ($this->testPrimitive($data, $possibleType['name'], $format)) {
83
                return $possibleType['name'];
84
            }
85
        }
86
87
        return null;
88
    }
89
90
    private function testPrimitive(mixed $data, string $type, string $format): bool
0 ignored issues
show
Unused Code introduced by
The parameter $format 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

90
    private function testPrimitive(mixed $data, string $type, /** @scrutinizer ignore-unused */ string $format): bool

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...
91
    {
92
        switch ($type) {
93
            case 'integer':
94
            case 'int':
95
                return (string) (int) $data === (string) $data;
96
97
            case 'double':
98
            case 'float':
99
                return (string) (float) $data === (string) $data;
100
101
            case 'bool':
102
            case 'boolean':
103
                return (string) (bool) $data === (string) $data;
104
105
            case 'string':
106
                return (string) $data === (string) $data;
107
        }
108
109
        return false;
110
    }
111
}
112