Passed
Branch v4.2 (a5186f)
by Masiukevich
09:03
created

EmptyDataNormalizer::supportsNormalization()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
crap 3.0067
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Messages serializer implementation.
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\MessageSerializer\Symfony\Extensions;
14
15
use function ServiceBus\Common\createWithoutConstructor;
0 ignored issues
show
introduced by
The function ServiceBus\Common\createWithoutConstructor was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
16
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
17
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18
19
/**
20
 * Normalizer for an object without attributes (empty).
21
 */
22
final class EmptyDataNormalizer implements NormalizerInterface, DenormalizerInterface
23
{
24
    /**
25
     * @psalm-var array<string, array<array-key, string>>
26
     */
27
    private $localStorage = [];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function normalize($object, string $format = null, array $context = []): array
33
    {
34 1
        return [];
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     *
40
     * @throws \ReflectionException
41
     */
42 2
    public function supportsNormalization($data, string $format = null): bool
43
    {
44 2
        if (\is_object($data))
45
        {
46 1
            $class = \get_class($data);
47
48 1
            if (isset($this->localStorage[$class]) === false)
49
            {
50 1
                $this->localStorage[$class] = \array_map(
51 1
                    static function (\ReflectionProperty $property): string
52
                    {
53
                        return (string) $property->name;
54 1
                    },
55 1
                    (new \ReflectionClass($data))->getProperties()
56
                );
57
            }
58
59 1
            return empty($this->localStorage[$class]);
60
        }
61
62 1
        return false;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     *
68
     * @throws \ServiceBus\Common\Exceptions\ReflectionApiException
69
     */
70 2
    public function denormalize($data, string $type, string $format = null, array $context = []): object
71
    {
72
        /** @psalm-var class-string $type */
73
74 2
        return createWithoutConstructor($type);
0 ignored issues
show
Bug introduced by
The function createWithoutConstructor was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        return /** @scrutinizer ignore-call */ createWithoutConstructor($type);
Loading history...
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 2
    public function supportsDenormalization($data, string $type, string $format = null): bool
81
    {
82 2
        return empty($data);
83
    }
84
}
85