Passed
Pull Request — master (#1448)
by Asmir
02:29
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\GraphNavigatorInterface;
9
use JMS\Serializer\SerializationContext;
10
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
11
use JMS\Serializer\Visitor\SerializationVisitorInterface;
12
13
final class EnumHandler implements SubscribingHandlerInterface
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public static function getSubscribingMethods()
19
    {
20
        $methods = [];
21
22
        foreach (['json', 'xml'] as $format) {
23
            $methods[] = [
24
                'type' => 'enum',
25
                'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
26
                'format' => $format,
27
                'method' => 'deserializeEnum',
28
            ];
29
            $methods[] = [
30
                'type' => 'enum',
31
                'format' => $format,
32
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
33
                'method' => 'serializeEnum',
34
            ];
35
        }
36
37
        return $methods;
38
    }
39
40
    public function serializeEnum(
41
        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

41
        /** @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...
42
        \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...
43
        array $type,
44
        SerializationContext $context
45
    ) {
46
        if (isset($type['params'][1]) && 'value' === $type['params'][1]) {
47
            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...
48
                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)));
49
            }
50
51
            $valueType = isset($type['params'][2]) ? ['name' => $type['params'][2]] : null;
52
53
            return $context->getNavigator()->accept($enum->value, $valueType);
54
        } else {
55
            return $context->getNavigator()->accept($enum->name);
56
        }
57
    }
58
59
    /**
60
     * @param int|string|\SimpleXMLElement $data
61
     * @param array $type
62
     */
63
    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

63
    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...
64
    {
65
        $ref = new \ReflectionEnum($type['params'][0]);
66
67
        if (isset($type['params'][1]) && 'value' === $type['params'][1]) {
68
            $backingType = $ref->getBackingType();
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

68
            /** @scrutinizer ignore-call */ 
69
            $backingType = $ref->getBackingType();

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...
69
70
            if (!$backingType instanceof \ReflectionNamedType) {
71
                throw new InvalidMetadataException(sprintf('The type "%s" is not a backed enum, thus you can not use "value" as serialization mode for its value.', $ref->getName()));
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

71
                throw new InvalidMetadataException(sprintf('The type "%s" is not a backed enum, thus you can not use "value" as serialization mode for its value.', $ref->/** @scrutinizer ignore-call */ 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...
72
            }
73
74
            $cases = $ref->getCases();
0 ignored issues
show
Bug introduced by
The method getCases() 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

74
            /** @scrutinizer ignore-call */ 
75
            $cases = $ref->getCases();

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...
75
            foreach ($cases as $case) {
76
                $value = $case->getValue();
77
                $backingTypeName = $backingType->getName();
78
79
                if ('int' === $backingTypeName) {
80
                    if ($value->value === (int) $data) {
81
                        return $value;
82
                    }
83
                } elseif ('string' === $backingTypeName) {
84
                    if ($value->value === (string) $data) {
85
                        return $value;
86
                    }
87
                }
88
            }
89
90
            return null;
91
        } else {
92
            $caseValue = (string) $data;
93
            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

93
            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...
94
                throw new InvalidMetadataException(sprintf('The type "%s" does not have the case "%s"', $ref->getName(), $caseValue));
95
            }
96
97
            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

97
            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...
98
        }
99
    }
100
}
101