IteratorHandler::getSubscribingMethods()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 53
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 35
c 0
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\Type\Type;
15
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
16
use JMS\Serializer\Visitor\SerializationVisitorInterface;
17
18
/**
19
 * @phpstan-import-type TypeArray from Type
20
 */
21
final class IteratorHandler implements SubscribingHandlerInterface
22
{
23
    private const SUPPORTED_FORMATS = ['json', 'xml'];
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public static function getSubscribingMethods()
29
    {
30
        $methods = [];
31
32
        foreach (self::SUPPORTED_FORMATS as $format) {
33
            $methods[] = [
34
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
35
                'type' => Iterator::class,
36
                'format' => $format,
37
                'method' => 'serializeIterable',
38
            ];
39
40
            $methods[] = [
41
                'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
42
                'type' => Iterator::class,
43
                'format' => $format,
44
                'method' => 'deserializeIterator',
45
            ];
46
        }
47
48
        foreach (self::SUPPORTED_FORMATS as $format) {
49
            $methods[] = [
50
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
51
                'type' => ArrayIterator::class,
52
                'format' => $format,
53
                'method' => 'serializeIterable',
54
            ];
55
56
            $methods[] = [
57
                'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
58
                'type' => ArrayIterator::class,
59
                'format' => $format,
60
                'method' => 'deserializeIterator',
61
            ];
62
        }
63
64
        foreach (self::SUPPORTED_FORMATS as $format) {
65
            $methods[] = [
66
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
67
                'type' => Generator::class,
68
                'format' => $format,
69
                'method' => 'serializeIterable',
70
            ];
71
72
            $methods[] = [
73
                'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
74
                'type' => Generator::class,
75
                'format' => $format,
76
                'method' => 'deserializeGenerator',
77
            ];
78
        }
79
80
        return $methods;
81
    }
82
83
    /**
84
     * @param TypeArray $type
0 ignored issues
show
Bug introduced by
The type JMS\Serializer\Handler\TypeArray 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...
85
     *
86
     * @return array|\ArrayObject|null
87
     */
88
    public function serializeIterable(
89
        SerializationVisitorInterface $visitor,
90
        iterable $iterable,
91
        array $type,
92
        SerializationContext $context
93
    ): ?iterable {
94
        $type['name'] = 'array';
95
96
        $context->stopVisiting($iterable);
97
        $result = $visitor->visitArray(Functions::iterableToArray($iterable), $type);
98
        $context->startVisiting($iterable);
99
100
        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...
101
    }
102
103
    /**
104
     * @param TypeArray $type
105
     * @param mixed $data
106
     */
107
    public function deserializeIterator(
108
        DeserializationVisitorInterface $visitor,
109
        $data,
110
        array $type,
111
        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

111
        /** @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...
112
    ): \Iterator {
113
        $type['name'] = 'array';
114
115
        return new ArrayIterator($visitor->visitArray($data, $type));
116
    }
117
118
    /**
119
     * @param mixed $data
120
     * @param TypeArray $type
121
     */
122
    public function deserializeGenerator(
123
        DeserializationVisitorInterface $visitor,
124
        $data,
125
        array $type,
126
        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

126
        /** @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...
127
    ): Generator {
128
        return (static function () use (&$visitor, &$data, &$type): Generator {
129
            $type['name'] = 'array';
130
            yield from $visitor->visitArray($data, $type);
131
        })();
132
    }
133
}
134