Passed
Push — master ( 3969cc...b281ee )
by Asmir
03:28 queued 01:08
created

EnumHandler::getSubscribingMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 13
nc 2
nop 0
dl 0
loc 20
rs 9.8333
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Handler;
6
7
use JMS\Serializer\Exception\InvalidMetadataException;
8
use JMS\Serializer\Exception\RuntimeException;
9
use JMS\Serializer\GraphNavigatorInterface;
10
use JMS\Serializer\SerializationContext;
11
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
12
use JMS\Serializer\Visitor\SerializationVisitorInterface;
13
14
final class EnumHandler implements SubscribingHandlerInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public static function getSubscribingMethods()
20
    {
21
        $methods = [];
22
23
        foreach (['json', 'xml'] as $format) {
24
            $methods[] = [
25
                'type' => 'enum',
26
                'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
27
                'format' => $format,
28
                'method' => 'deserializeEnum',
29
            ];
30
            $methods[] = [
31
                'type' => 'enum',
32
                'format' => $format,
33
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
34
                'method' => 'serializeEnum',
35
            ];
36
        }
37
38
        return $methods;
39
    }
40
41
    public function serializeEnum(
42
        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

42
        /** @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...
43
        \UnitEnum $enum,
0 ignored issues
show
Bug introduced by
The type UnitEnum 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...
44
        array $type,
45
        SerializationContext $context
46
    ) {
47
        if (isset($type['params'][1]) && 'value' === $type['params'][1]) {
48
            if (!$enum instanceof \BackedEnum) {
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...
49
                throw new InvalidMetadataException(sprintf('The type "%s" is not a backed enum, thus you can not use "value" as serialization mode for its value.', get_class($enum)));
50
            }
51
52
            $valueType = isset($type['params'][2]) ? ['name' => $type['params'][2]] : null;
53
54
            return $context->getNavigator()->accept($enum->value, $valueType);
55
        } else {
56
            return $context->getNavigator()->accept($enum->name);
57
        }
58
    }
59
60
    /**
61
     * @param int|string|\SimpleXMLElement $data
62
     * @param array $type
63
     */
64
    public function deserializeEnum(DeserializationVisitorInterface $visitor, $data, array $type): ?\UnitEnum
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

64
    public function deserializeEnum(/** @scrutinizer ignore-unused */ DeserializationVisitorInterface $visitor, $data, array $type): ?\UnitEnum

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...
65
    {
66
        $enumType = $type['params'][0];
67
        $caseValue = (string) $data;
68
69
        $ref = new \ReflectionEnum($enumType);
70
        if (isset($type['params'][1]) && 'value' === $type['params'][1] || (!isset($type['params'][1]) && is_a($enumType, \BackedEnum::class, true))) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (IssetNode && 'value' ==...ackedEnum::class, true), Probably Intended Meaning: IssetNode && ('value' ==...ckedEnum::class, true))
Loading history...
71
            if (!is_a($enumType, \BackedEnum::class, true)) {
72
                throw new InvalidMetadataException(sprintf('The type "%s" is not a backed enum, thus you can not use "value" as serialization mode for its value.', $enumType));
73
            }
74
75
            if ('int' === $ref->getBackingType()->getName()) {
0 ignored issues
show
Bug introduced by
The method getBackingType() does not exist on ReflectionEnum. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            if ('int' === $ref->/** @scrutinizer ignore-call */ getBackingType()->getName()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
                if (!is_numeric($caseValue)) {
77
                    throw new RuntimeException(sprintf('"%s" is not a valid backing value for enum "%s"', $caseValue, $enumType));
78
                }
79
80
                $caseValue = (int) $caseValue;
81
            }
82
83
            return $enumType::from($caseValue);
84
        } else {
85
            if (!$ref->hasCase($caseValue)) {
0 ignored issues
show
Bug introduced by
The method hasCase() does not exist on ReflectionEnum. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
            if (!$ref->/** @scrutinizer ignore-call */ hasCase($caseValue)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
                throw new InvalidMetadataException(sprintf('The type "%s" does not have the case "%s"', $ref->getName(), $caseValue));
0 ignored issues
show
Bug introduced by
The method getName() does not exist on ReflectionEnum. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
                throw new InvalidMetadataException(sprintf('The type "%s" does not have the case "%s"', $ref->/** @scrutinizer ignore-call */ getName(), $caseValue));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
            }
88
89
            return $ref->getCase($caseValue)->getValue();
0 ignored issues
show
Bug introduced by
The method getCase() does not exist on ReflectionEnum. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
            return $ref->/** @scrutinizer ignore-call */ getCase($caseValue)->getValue();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
        }
91
    }
92
}
93