Completed
Push — master ( ce8189...62192d )
by Tomasz
22s
created

FallbackNormalizerContainer::getHandler()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26

Duplication

Lines 7
Ratio 26.92 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
dl 7
loc 26
ccs 14
cts 14
cp 1
rs 8.8817
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6
1
<?php
2
namespace Thunder\Serializard\NormalizerContainer;
3
4
use Thunder\Serializard\Exception\ClassNotFoundException;
5
use Thunder\Serializard\Exception\NormalizerConflictException;
6
use Thunder\Serializard\Exception\NormalizerNotFoundException;
7
8
/**
9
 * @author Tomasz Kowalczyk <[email protected]>
10
 */
11
final class FallbackNormalizerContainer implements NormalizerContainerInterface
12
{
13
    private $default;
14
    private $handlers = [];
15
    private $interfaces = [];
16
    private $aliases = [];
17
18 20 View Code Duplication
    public function add($class, callable $handler)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20 20
        if(class_exists($class)) {
21 16
            $this->aliases[$class] = $class;
22 16
            $this->handlers[$class] = $handler;
23 20
        } elseif(interface_exists($class)) {
24 3
            $this->aliases[$class] = $class;
25 3
            $this->interfaces[$class] = $handler;
26 3
        } else {
27 1
            throw ClassNotFoundException::fromClass($class);
28
        }
29 19
    }
30
31 2 View Code Duplication
    public function addAlias($alias, $class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33 2
        $handler = $this->getHandler($class);
34
35 1
        $this->handlers[$alias] = $handler;
36 1
        $this->aliases[$alias] = $this->aliases[$class];
37 1
    }
38
39 18
    public function getHandler($class)
40
    {
41 18
        if(isset($this->handlers[$class])) {
42 13
            return $this->handlers[$class];
43
        }
44
45 8
        $parents = array_intersect(array_keys($this->handlers), class_parents($class));
46 8
        if($parents) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parents of type array<integer|string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
47 2
            return $this->handlers[array_pop($parents)];
48
        }
49
50 6
        $interfaces = array_intersect(array_keys($this->interfaces), array_values(class_implements($class)));
51 6 View Code Duplication
        if($interfaces) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $interfaces of type array<integer|string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52 3
            if(\count($interfaces) > 1) {
53 1
                throw NormalizerConflictException::fromClass($class, $interfaces);
54
            }
55
56 2
            return $this->interfaces[array_shift($interfaces)];
57
        }
58
59 3
        if(null === $this->default) {
60 2
            throw NormalizerNotFoundException::fromClass($class);
61
        }
62
63 1
        return $this->default;
64
    }
65
66 2
    public function setDefault(callable $handler)
67
    {
68 2
        $this->default = $handler;
69 2
    }
70
71 1
    public function hasDefault()
72
    {
73 1
        return null !== $this->default;
74
    }
75
76 1
    public function getDefault()
77
    {
78 1
        return $this->default;
79
    }
80
}
81