SerializerTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 27
ccs 6
cts 7
cp 0.8571
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A deserialize() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LibraryCatalog\Service\Repository;
6
7
use LibraryCatalog\Transformer\Serializer;
8
9
trait SerializerTrait
10
{
11
    /** @var Serializer */
12
    protected Serializer $serializer;
13
14
    /**
15
     * @param $data
16
     * @param string $classname
17
     * @return object|null
18
     * @throws Serializer\Exception
19
     * @throws Serializer\HydrateException
20
     * @throws \LibraryCatalog\Transformer\Encoder\Exception
21
     */
22 12
    protected function deserialize($data, string $classname): ?object
23
    {
24 12
        $object = null;
25
26 12
        if ($data != '') {
27 6
            $object = $this->serializer->deserialize((string)$data);
28
29 6
            if (!($object instanceof $classname)) {
30
                // Do not generate error, just ignore not correct value.
31
                $object = null;
32
            }
33
        }
34
35 12
        return $object;
36
    }
37
}
38