Completed
Push — master ( f908d1...654df8 )
by Asmir
05:00 queued 02:28
created

IteratorHandler::getSubscribingMethods()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 53
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 35
c 2
b 0
f 0
nc 8
nop 0
dl 0
loc 53
rs 9.36

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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