denormalize()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 4
nop 4
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\IsmpClient\Impl\Serializer\V4;
6
7
use Lamoda\IsmpClient\V4\Dto\FacadeDocBodyResponse\Body;
8
use Symfony\Component\Serializer\Exception\BadMethodCallException;
9
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
10
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
11
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
12
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
13
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
14
use Symfony\Component\Serializer\SerializerInterface;
15
16
final class FacadeDocBodyResponseBodyDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface, CacheableSupportsMethodInterface
17
{
18
    private const ORIGINAL_STRING_FIELD_KEY = 'original_string';
19
20
    /**
21
     * @var SerializerInterface|DenormalizerInterface
22
     */
23
    private $denormalizer;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 4
    public function denormalize($data, $class, $format = null, array $context = [])
29
    {
30 4
        if (null === $this->denormalizer) {
31 1
            throw new BadMethodCallException('Please set a serializer before calling denormalize()!');
32
        }
33 3
        if (!\is_array($data)) {
34 1
            throw new InvalidArgumentException('Data expected to be an array, ' . \gettype($data) . ' given.');
35
        }
36 2
        if (Body::class !== $class) {
37 1
            throw new InvalidArgumentException('Unsupported class: ' . $class);
38
        }
39
40 1
        $data[self::ORIGINAL_STRING_FIELD_KEY] = $this->denormalizer->serialize($data, $format, $context);
0 ignored issues
show
Bug introduced by
The method serialize does only exist in Symfony\Component\Serializer\SerializerInterface, but not in Symfony\Component\Serial...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...
41
42 1
        return $this->denormalizer->denormalize($data, $class, $format, $context);
0 ignored issues
show
Bug introduced by
The method denormalize does only exist in Symfony\Component\Serial...r\DenormalizerInterface, but not in Symfony\Component\Serializer\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...
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 11
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
49
    {
50 11
        return Body::class === $type
51 11
            && is_array($data)
52 11
            && !array_key_exists(self::ORIGINAL_STRING_FIELD_KEY, $data);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 19
    public function setDenormalizer(DenormalizerInterface $denormalizer): void
59
    {
60 19
        if (!$denormalizer instanceof SerializerInterface) {
61 1
            throw new InvalidArgumentException('Denormalizer must implement serializer interface also');
62
        }
63
64 18
        $this->denormalizer = $denormalizer;
65 18
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 9
    public function hasCacheableSupportsMethod(): bool
71
    {
72 9
        return $this->denormalizer instanceof CacheableSupportsMethodInterface && $this->denormalizer->hasCacheableSupportsMethod();
73
    }
74
}
75