Passed
Push — master ( 341743...0998b7 )
by Dominik
01:57
created

ObjectFieldSerializer::serializerOrException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
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
    public function __construct(AccessorInterface $accessor)
22
    {
23
        $this->accessor = $accessor;
24
    }
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
    public function serializeField(string $path, Request $request, $object, SerializerInterface $serializer = null)
35
    {
36
        $this->serializerOrException($serializer);
37
38
        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
    private function serializerOrException(SerializerInterface $serializer = null)
47
    {
48
        if (null === $serializer) {
49
            throw new \RuntimeException(sprintf('Serializer needed: %s', SerializerInterface::class));
50
        }
51
    }
52
}
53