Completed
Push — master ( 66f480...24a417 )
by Tomasz
02:30 queued 17s
created

Serializard::getFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
namespace Thunder\Serializard;
3
4
use Thunder\Serializard\Format\FormatInterface;
5
use Thunder\Serializard\FormatContainer\FormatContainerInterface as Formats;
6
use Thunder\Serializard\HandlerContainer\HandlerContainerInterface as Handlers;
7
8
/**
9
 * @author Tomasz Kowalczyk <[email protected]>
10
 */
11
final class Serializard
12
{
13
    private $normalizers;
14
    private $hydrators;
15
    /** @var FormatInterface[] */
16
    private $formats;
17
18
    public function __construct(Formats $formats, Handlers $normalizers, Handlers $hydrators)
19
    {
20
        $this->normalizers = $normalizers;
21
        $this->hydrators = $hydrators;
22
        $this->formats = $formats;
0 ignored issues
show
Documentation Bug introduced by
It seems like $formats of type object<Thunder\Serializa...rmatContainerInterface> is incompatible with the declared type array<integer,object<Thu...ormat\FormatInterface>> of property $formats.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
    }
24
25
    public function serialize($var, $format)
26
    {
27
        return $this->getFormat($format)->serialize($var, $this->normalizers);
28
    }
29
30
    public function unserialize($var, $class, $format)
31
    {
32
        return $this->getFormat($format)->unserialize($var, $class, $this->hydrators);
33
    }
34
35
    private function getFormat($alias)
36
    {
37
        $format = $this->formats->get($alias);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->formats (of type array<integer,object<Thu...ormat\FormatInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
38
39
        if(false === $format instanceof FormatInterface) {
40
            throw new \RuntimeException(sprintf('No registered format for alias %s!', $alias));
41
        }
42
43
        return $format;
44
    }
45
}
46