Passed
Pull Request — master (#3)
by Pavel
01:47
created

FacadeDocBodyResponseBodyDenormalizer.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\IsmpClient\Impl\Serializer;
6
7
use Lamoda\IsmpClient\V3\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
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
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 5
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
49
    {
50 5
        return Body::class === $type
51 5
            && is_array($data)
52 5
            && !array_key_exists(self::ORIGINAL_STRING_FIELD_KEY, $data);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 8
    public function setDenormalizer(DenormalizerInterface $denormalizer): void
59
    {
60 8
        if (!$denormalizer instanceof SerializerInterface) {
61 1
            throw new InvalidArgumentException('Denormalizer must implement serializer interface also');
62
        }
63
64 7
        $this->denormalizer = $denormalizer;
65 7
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function hasCacheableSupportsMethod(): bool
71
    {
72 3
        return $this->denormalizer instanceof CacheableSupportsMethodInterface && $this->denormalizer->hasCacheableSupportsMethod();
73
    }
74
}
75