Passed
Pull Request — master (#1384)
by
unknown
04:43 queued 01:37
created

BackedEnumHandler::getValueType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Handler;
6
7
use JMS\Serializer\Context;
8
use JMS\Serializer\SerializationContext;
9
use JMS\Serializer\GraphNavigatorInterface;
10
use JMS\Serializer\Visitor\SerializationVisitorInterface;
11
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
12
13
class BackedEnumHandler implements SubscribingHandlerInterface
14
{
15
    /**
16
     * {@inheritDoc}
17
     */
18
    public static function getSubscribingMethods(): array
19
    {
20
        $methods = [];
21
        foreach (['json', 'xml'] as $format) {
22
            $methods[] = [
23
                'type' => \BackedEnum::class,
0 ignored issues
show
Bug introduced by
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
                'format' => $format,
25
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
26
                'method' => 'serializeBackedEnum',
27
            ];
28
29
            $methods[] = [
30
                'type' => \BackedEnum::class,
31
                'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
32
                'format' => $format,
33
                'method' => 'deserializeBackedEnum',
34
            ];
35
        }
36
37
        return $methods;
38
    }
39
40
    /**
41
     * @param SerializationVisitorInterface $visitor
42
     * @param \BackedEnum                   $object
43
     * @param array                         $type
44
     * @param SerializationContext          $context
45
     *
46
     * @return null|string|int
47
     */
48
    public function serializeBackedEnum(SerializationVisitorInterface $visitor, $object, array $type, Context $context)
49
    {
50
        $value = $object->value;
51
        if ('int' === $this->getValueType($type, $context)) {
52
            return $visitor->visitInteger($value, $type);
53
        }
54
55
        return $visitor->visitString($value, $type);
56
    }
57
58
    /**
59
     * @param DeserializationVisitorInterface $visitor
60
     * @param string|int                      $value
61
     * @param array                           $type
62
     * @param Context                         $context
63
     *
64
     * @return null|\BackedEnum
65
     */
66
    public function deserializeBackedEnum(DeserializationVisitorInterface $visitor, $value, array $type, Context $context)
67
    {
68
        if ('xml' === $context->getFormat() && $this->isDataXmlNull($value)) {
69
            return null;
70
        }
71
72
        if ('int' === $this->getValueType($type, $context)) {
73
            $value = $visitor->visitInteger($value, $type);
74
        } else {
75
            $value = $visitor->visitString($value, $type);
76
        }
77
78
        return $this->tryFrom($type, $value);
79
    }
80
81
    /**
82
     * @param array           $type
83
     * @param null|int|string $value
84
     *
85
     * @return null|\BackedEnum
86
     */
87
    protected function tryFrom(array $type, $value)
88
    {
89
        return $type['enum']::tryFrom($value);
90
    }
91
92
    protected function getValueType(array $type, Context $context): string
93
    {
94
        return $context->getMetadataFactory()->getMetadataForClass($type['enum'])->propertyMetadata['value']->type['name'];
0 ignored issues
show
Bug introduced by
The property propertyMetadata does not seem to exist on Metadata\ClassHierarchyMetadata.
Loading history...
95
    }
96
97
    private function isDataXmlNull($data): bool
98
    {
99
        $attributes = $data->attributes('xsi', true);
100
101
        return isset($attributes['nil'][0]) && 'true' === (string) $attributes['nil'][0];
102
    }
103
}
104