Passed
Pull Request — master (#1384)
by
unknown
05:10 queued 01:55
created

BackedEnumHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 25
c 1
b 0
f 1
dl 0
loc 78
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A serializeBackedEnum() 0 8 2
A getSubscribingMethods() 0 20 2
A getValueType() 0 3 1
A deserializeBackedEnum() 0 9 2
A tryFrom() 0 3 1
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 ('int' === $this->getValueType($type, $context)) {
69
            $value = $visitor->visitInteger($value, $type);
70
        } else {
71
            $value = $visitor->visitString($value, $type);
72
        }
73
74
        return $this->tryFrom($type, $value);
75
    }
76
77
    /**
78
     * @param array           $type
79
     * @param null|int|string $value
80
     *
81
     * @return null|\BackedEnum
82
     */
83
    protected function tryFrom(array $type, $value)
84
    {
85
        return $type['enum']::tryFrom($value);
86
    }
87
88
    protected function getValueType(array $type, Context $context): string
89
    {
90
        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...
91
    }
92
}
93