Completed
Push — master ( 2a6a78...c2b2ef )
by DELEVOYE
05:24
created

Serializer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the MindbazBundle package.
5
 *
6
 * (c) David DELEVOYE <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Kozikaza\MindbazBundle\Serializer\Bridge;
13
14
use JMS\Serializer\ArrayTransformerInterface;
15
use JMS\Serializer\SerializerInterface as JMSSerializerInterface;
16
use Kozikaza\MindbazBundle\Model\Subscriber;
17
use mbzSubscriber\Subscriber as MindbazSubscriber;
18
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
19
use Symfony\Component\Serializer\SerializerInterface;
20
21
/**
22
 * @author Vincent Chalamon <[email protected]>
23
 */
24
class Serializer
25
{
26
    /**
27
     * @var SerializerInterface|DenormalizerInterface|JMSSerializerInterface|ArrayTransformerInterface
28
     */
29
    private $serializer;
30
31
    /**
32
     * @param SerializerInterface|DenormalizerInterface|JMSSerializerInterface|ArrayTransformerInterface $serializer
33
     */
34
    public function __construct($serializer)
35
    {
36
        $this->serializer = $serializer;
37
    }
38
39
    /**
40
     * @param array  $data
41
     * @param string $class
42
     *
43
     * @return Subscriber
44
     */
45
    public function denormalize(array $data, $class)
46
    {
47
        return $this->serializer instanceof JMSSerializerInterface ? $this->serializer->fromArray($data, $class) : $this->serializer->denormalize($data, $class);
0 ignored issues
show
Bug introduced by
The method denormalize does only exist in Symfony\Component\Serial...r\DenormalizerInterface, but not in JMS\Serializer\ArrayTran...zer\SerializerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
    }
49
50
    /**
51
     * @param Subscriber $data
52
     * @param string     $format
53
     *
54
     * @return MindbazSubscriber
55
     */
56
    public function serialize(Subscriber $data, $format)
57
    {
58
        return $this->serializer->serialize($data, $format);
0 ignored issues
show
Bug introduced by
The method serialize does only exist in JMS\Serializer\Serialize...zer\SerializerInterface, but not in JMS\Serializer\ArrayTran...r\DenormalizerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
59
    }
60
61
    /**
62
     * @param MindbazSubscriber $data
63
     * @param string            $type
64
     * @param string            $format
65
     *
66
     * @return Subscriber
67
     */
68
    public function deserialize(MindbazSubscriber $data, $type, $format)
69
    {
70
        return $this->serializer->deserialize($data, $type, $format);
0 ignored issues
show
Documentation introduced by
$data is of type object<mbzSubscriber\Subscriber>, but the function expects a string.

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...
Bug introduced by
The method deserialize does only exist in JMS\Serializer\Serialize...zer\SerializerInterface, but not in JMS\Serializer\ArrayTran...r\DenormalizerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
71
    }
72
}
73