Passed
Push — master ( 3ec415...9f2f47 )
by Daniel
16:25
created

UuidNormalizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 7
dl 0
loc 27
ccs 0
cts 9
cp 0
rs 10
c 1
b 1
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A denormalize() 0 7 2
A supportsDenormalization() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[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
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Serializer;
15
16
use ApiPlatform\Core\Bridge\RamseyUuid\Identifier\Normalizer\UuidNormalizer as BaseUuidNormalizer;
17
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
18
19
/**
20
 * Because we have a couple of entities which can be fetched with either the UUID or another
21
 * field, we should pass strings through.
22
 *
23
 * @author Daniel West <[email protected]>
24
 */
25
class UuidNormalizer implements DenormalizerInterface
26
{
27
    private BaseUuidNormalizer $decorated;
28
29
    public function __construct(BaseUuidNormalizer $decorated)
30
    {
31
        $this->decorated = $decorated;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function denormalize($data, $class, $format = null, array $context = [])
38
    {
39
        if (\is_string($data)) {
40
            return $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $data returns the type string which is incompatible with the return type mandated by Symfony\Component\Serial...nterface::denormalize() of array|object.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
41
        }
42
43
        return $this->decorated->denormalize($data, $class, $format, $context);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function supportsDenormalization($data, $type, $format = null)
50
    {
51
        return $this->decorated->supportsDenormalization($data, $type, $format);
52
    }
53
}
54