Completed
Push — master ( ae9c65...55772a )
by Asmir
07:43 queued 05:39
created

ArrayCollectionHandler::getSubscribingMethods()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 17
cts 17
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 22
nc 3
nop 0
crap 3
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\Context;
24
use JMS\Serializer\GraphNavigator;
25
use JMS\Serializer\VisitorInterface;
26
27
class ArrayCollectionHandler implements SubscribingHandlerInterface
28
{
29
    /**
30
     * @var bool
31
     */
32
    private $initializeExcluded = true;
33
34 389
    public function __construct($initializeExcluded = true)
35
    {
36 389
        $this->initializeExcluded = $initializeExcluded;
37 389
    }
38
39 387
    public static function getSubscribingMethods()
40
    {
41 387
        $methods = array();
42 387
        $formats = array('json', 'xml', 'yml');
43
        $collectionTypes = array(
44 387
            'ArrayCollection',
45
            'Doctrine\Common\Collections\ArrayCollection',
46
            'Doctrine\ORM\PersistentCollection',
47
            'Doctrine\ODM\MongoDB\PersistentCollection',
48
            'Doctrine\ODM\PHPCR\PersistentCollection',
49
        );
50
51 387
        foreach ($collectionTypes as $type) {
52 387
            foreach ($formats as $format) {
53 387
                $methods[] = array(
54 387
                    'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
55 387
                    'type' => $type,
56 387
                    'format' => $format,
57 387
                    'method' => 'serializeCollection',
58
                );
59
60 387
                $methods[] = array(
61 387
                    'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
62 387
                    'type' => $type,
63 387
                    'format' => $format,
64 387
                    'method' => 'deserializeCollection',
65
                );
66
            }
67
        }
68
69 387
        return $methods;
70
    }
71
72 13
    public function serializeCollection(VisitorInterface $visitor, Collection $collection, array $type, Context $context)
73
    {
74
        // We change the base type, and pass through possible parameters.
75 13
        $type['name'] = 'array';
76
77 13
        if ($this->initializeExcluded === false) {
78 1
            $exclusionStrategy = $context->getExclusionStrategy();
79 1
            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...
80 1
                return $visitor->visitArray([], $type, $context);
81
            }
82
        }
83
84 12
        return $visitor->visitArray($collection->toArray(), $type, $context);
85
    }
86
87 6
    public function deserializeCollection(VisitorInterface $visitor, $data, array $type, Context $context)
88
    {
89
        // See above.
90 6
        $type['name'] = 'array';
91
92 6
        return new ArrayCollection($visitor->visitArray($data, $type, $context));
93
    }
94
}
95