Completed
Push — master ( 17d32c...a8eddd )
by Dominik
02:16
created

ObjectFieldSerializer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Serializer\Field;
6
7
use Chubbyphp\Serialization\Accessor\AccessorInterface;
8
use Chubbyphp\Serialization\SerializerInterface;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
11
final class ObjectFieldSerializer implements FieldSerializerInterface
12
{
13
    /**
14
     * @var AccessorInterface
15
     */
16
    private $accessor;
17
18
    /**
19
     * @param AccessorInterface $accessor
20
     */
21 2
    public function __construct(AccessorInterface $accessor)
22
    {
23 2
        $this->accessor = $accessor;
24 2
    }
25
26
    /**
27
     * @param string                   $path
28
     * @param Request                  $request
29
     * @param object                   $object
30
     * @param SerializerInterface|null $serializer
31
     *
32
     * @return mixed
33
     */
34 2
    public function serializeField(string $path, Request $request, $object, SerializerInterface $serializer = null)
35
    {
36 2
        $this->serializerOrException($serializer);
37
38 1
        return $serializer->serialize($request, $this->accessor->getValue($object), $path);
0 ignored issues
show
Bug introduced by
It seems like $serializer is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
39
    }
40
41
    /**
42
     * @param SerializerInterface|null $serializer
43
     *
44
     * @throws \RuntimeException
45
     */
46 2
    private function serializerOrException(SerializerInterface $serializer = null)
47
    {
48 2
        if (null === $serializer) {
49 1
            throw new \RuntimeException(sprintf('Serializer needed: %s', SerializerInterface::class));
50
        }
51 1
    }
52
}
53