Passed
Pull Request — master (#1315)
by Tobias
02:16
created

IteratorHandler::deserializeIterable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Handler;
6
7
use ArrayIterator;
8
use Generator;
9
use Iterator;
10
use JMS\Serializer\DeserializationContext;
11
use JMS\Serializer\Functions;
12
use JMS\Serializer\GraphNavigatorInterface;
13
use JMS\Serializer\SerializationContext;
14
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
15
use JMS\Serializer\Visitor\SerializationVisitorInterface;
16
17
final class IteratorHandler implements SubscribingHandlerInterface
18
{
19
    private const SUPPORTED_FORMATS = ['json', 'xml'];
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public static function getSubscribingMethods()
25
    {
26
        $methods = [];
27
28
        foreach (self::SUPPORTED_FORMATS as $format) {
29
            $methods[] = [
30
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
31
                'type' => Iterator::class,
32
                'format' => $format,
33
                'method' => 'serializeIterable',
34
            ];
35
36
            $methods[] = [
37
                'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
38
                'type' => Iterator::class,
39
                'format' => $format,
40
                'method' => 'deserializeIterator',
41
            ];
42
        }
43
44
        foreach (self::SUPPORTED_FORMATS as $format) {
45
            $methods[] = [
46
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
47
                'type' => ArrayIterator::class,
48
                'format' => $format,
49
                'method' => 'serializeIterable',
50
            ];
51
52
            $methods[] = [
53
                'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
54
                'type' => ArrayIterator::class,
55
                'format' => $format,
56
                'method' => 'deserializeIterator',
57
            ];
58
        }
59
60
        foreach (self::SUPPORTED_FORMATS as $format) {
61
            $methods[] = [
62
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
63
                'type' => Generator::class,
64
                'format' => $format,
65
                'method' => 'serializeIterable',
66
            ];
67
68
            $methods[] = [
69
                'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
70
                'type' => Generator::class,
71
                'format' => $format,
72
                'method' => 'deserializeGenerator',
73
            ];
74
        }
75
76
        return $methods;
77
    }
78
79
    /**
80
     * @return array|\ArrayObject|null
81
     */
82
    public function serializeIterable(
83
        SerializationVisitorInterface $visitor,
84
        iterable $iterable,
85
        array $type,
86
        SerializationContext $context
87
    ): ?iterable {
88
        $type['name'] = 'array';
89
90
        $context->stopVisiting($iterable);
91
        $result = $visitor->visitArray(Functions::iterableToArray($iterable), $type);
92
        $context->startVisiting($iterable);
93
94
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type ArrayObject|array which is incompatible with the type-hinted return iterable|null.
Loading history...
95
    }
96
97
    /**
98
     * @param mixed $data
99
     */
100
    public function deserializeIterator(
101
        DeserializationVisitorInterface $visitor,
102
        $data,
103
        array $type,
104
        DeserializationContext $context
0 ignored issues
show
Unused Code introduced by
The parameter $context 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

104
        /** @scrutinizer ignore-unused */ 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...
105
    ): \Iterator {
106
        $type['name'] = 'array';
107
108
        return new ArrayIterator($visitor->visitArray($data, $type));
109
    }
110
111
    /**
112
     * @param mixed $data
113
     */
114
    public function deserializeGenerator(
115
        DeserializationVisitorInterface $visitor,
116
        $data,
117
        array $type,
118
        DeserializationContext $context
0 ignored issues
show
Unused Code introduced by
The parameter $context 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

118
        /** @scrutinizer ignore-unused */ 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...
119
    ): Generator {
120
        return (static function () use (&$visitor, &$data, &$type): Generator {
121
            $type['name'] = 'array';
122
            yield from $visitor->visitArray($data, $type);
123
        })();
124
    }
125
}
126