Completed
Pull Request — master (#743)
by Asmir
03:52
created

ArrayCollectionHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 0
loc 74
ccs 30
cts 34
cp 0.8824
rs 10
c 0
b 0
f 0

4 Methods

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

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
                $context->startVisiting($collection);
85
86
                return $visitor->visitArray([], $type, $context);
87
            }
88
        }
89 12
        $result = $visitor->visitArray($collection->toArray(), $type, $context);
90
91 12
        $context->startVisiting($collection);
92 12
        return $result;
93
    }
94
95 9
    public function deserializeCollection(DeserializationVisitorInterface $visitor, $data, array $type, DeserializationContext $context)
96
    {
97
        // See above.
98 9
        $type['name'] = 'array';
99
100 9
        return new ArrayCollection($visitor->visitArray($data, $type, $context));
101
    }
102
}
103