Completed
Pull Request — master (#918)
by Asmir
02:38
created

ArrayCollectionHandler::deserializeCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
namespace JMS\Serializer\Handler;
22
23
use Doctrine\Common\Collections\ArrayCollection;
24
use Doctrine\Common\Collections\Collection;
25
use JMS\Serializer\DeserializationContext;
26
use JMS\Serializer\DeserializationVisitorInterface;
27
use JMS\Serializer\GraphNavigatorInterface;
28
use JMS\Serializer\SerializationContext;
29
use JMS\Serializer\SerializationVisitorInterface;
30
31
final class ArrayCollectionHandler implements SubscribingHandlerInterface
32
{
33
    /**
34
     * @var bool
35
     */
36
    private $initializeExcluded = true;
37
38 290
    public function __construct($initializeExcluded = true)
39
    {
40 290
        $this->initializeExcluded = $initializeExcluded;
41 290
    }
42
43 288
    public static function getSubscribingMethods()
44
    {
45 288
        $methods = array();
46 288
        $formats = array('json', 'xml', 'yml');
47
        $collectionTypes = array(
48 288
            'ArrayCollection',
49
            'Doctrine\Common\Collections\ArrayCollection',
50
            'Doctrine\ORM\PersistentCollection',
51
            'Doctrine\ODM\MongoDB\PersistentCollection',
52
            'Doctrine\ODM\PHPCR\PersistentCollection',
53
        );
54
55 288
        foreach ($collectionTypes as $type) {
56 288
            foreach ($formats as $format) {
57 288
                $methods[] = array(
58 288
                    'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
59 288
                    'type' => $type,
60 288
                    'format' => $format,
61 288
                    'method' => 'serializeCollection',
62
                );
63
64 288
                $methods[] = array(
65 288
                    'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
66 288
                    'type' => $type,
67 288
                    'format' => $format,
68 288
                    'method' => 'deserializeCollection',
69
                );
70
            }
71
        }
72
73 288
        return $methods;
74
    }
75
76 11
    public function serializeCollection(SerializationVisitorInterface $visitor, Collection $collection, array $type, SerializationContext $context)
77
    {
78
        // We change the base type, and pass through possible parameters.
79 11
        $type['name'] = 'array';
80
81 11
        $context->stopVisiting($collection);
82
83 11
        if ($this->initializeExcluded === false) {
84 1
            $exclusionStrategy = $context->getExclusionStrategy();
85 1
            if ($exclusionStrategy !== null && $exclusionStrategy->shouldSkipClass($context->getMetadataFactory()->getMetadataForClass(\get_class($collection)), $context)) {
86 1
                $context->startVisiting($collection);
87
88 1
                return $visitor->visitArray([], $type, $context);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\Serializa...Interface::visitArray() has too many arguments starting with $context. ( Ignorable by Annotation )

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

88
                return $visitor->/** @scrutinizer ignore-call */ visitArray([], $type, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
89
            }
90
        }
91 10
        $result = $visitor->visitArray($collection->toArray(), $type);
92
93 10
        $context->startVisiting($collection);
94 10
        return $result;
95
    }
96
97 6
    public function deserializeCollection(DeserializationVisitorInterface $visitor, $data, array $type, 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

97
    public function deserializeCollection(DeserializationVisitorInterface $visitor, $data, array $type, /** @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...
98
    {
99
        // See above.
100 6
        $type['name'] = 'array';
101
102 6
        return new ArrayCollection($visitor->visitArray($data, $type));
103
    }
104
}
105