Completed
Push — master ( 068c5a...89419d )
by Asmir
21:45 queued 02:18
created

StdClassHandler::serializeStdClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 4
crap 2
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 JMS\Serializer\Context;
22
use JMS\Serializer\GraphNavigator;
23
use JMS\Serializer\Metadata\StaticPropertyMetadata;
24
use JMS\Serializer\VisitorInterface;
25
26
/**
27
 * @author Asmir Mustafic <[email protected]>
28
 */
29
class StdClassHandler implements SubscribingHandlerInterface
30
{
31 362
    public static function getSubscribingMethods()
32
    {
33 362
        $methods = array();
34 362
        $formats = array('json', 'xml', 'yml');
35
36 362
        foreach ($formats as $format) {
37 362
            $methods[] = array(
38 362
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
39 362
                'type' => 'stdClass',
40 362
                'format' => $format,
41 362
                'method' => 'serializeStdClass',
42
            );
43 362
        }
44
45 362
        return $methods;
46
    }
47
48 6
    public function serializeStdClass(VisitorInterface $visitor, \stdClass $stdClass, array $type, Context $context)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50 6
        $classMetadata = $context->getMetadataFactory()->getMetadataForClass('stdClass');
51 6
        $visitor->startVisitingObject($classMetadata, $stdClass, array('name' => 'stdClass'), $context);
0 ignored issues
show
Documentation introduced by
$classMetadata 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...
52
53 6
        foreach ((array)$stdClass as $name => $value) {
54 4
            $metadata = new StaticPropertyMetadata('stdClass', $name, $value);
55 4
            $visitor->visitProperty($metadata, $value, $context);
56 6
        }
57
58 6
        return $visitor->endVisitingObject($classMetadata, $stdClass, array('name' => 'stdClass'), $context);
0 ignored issues
show
Documentation introduced by
$classMetadata 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...
59
    }
60
}
61