Completed
Push — master ( 5ba1dc...b85ec4 )
by Tomasz
02:09
created

NormalizerContainer::getHandler()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 22
loc 22
ccs 12
cts 12
cp 1
rs 8.6737
cc 5
eloc 12
nc 5
nop 1
crap 5
1
<?php
2
namespace Thunder\Serializard\NormalizerContainer;
3
4
/**
5
 * @author Tomasz Kowalczyk <[email protected]>
6
 */
7 View Code Duplication
final class NormalizerContainer implements NormalizerContainerInterface
0 ignored issues
show
Duplication introduced by
This class 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...
8
{
9
    private $handlers = array();
10
    private $interfaces = array();
11
    private $aliases = array();
12
13 17
    public function add($class, $root, $handler)
14
    {
15 17
        if(false === is_callable($handler)) {
16 1
            throw new \RuntimeException(sprintf('Invalid handler for class %s!', $class));
17
        }
18
19 16
        if(class_exists($class)) {
20 12
            $this->aliases[$class] = $root;
21 12
            $this->handlers[$class] = $handler;
22 16
        } elseif(interface_exists($class)) {
23 3
            $this->aliases[$class] = $root;
24 3
            $this->interfaces[$class] = $handler;
25 3
        } else {
26 1
            throw new \RuntimeException(sprintf('Given value %s is neither class nor interface name!', $class));
27
        }
28 15
    }
29
30 2
    public function addAlias($alias, $class)
31
    {
32 2
        $handler = $this->getHandler($class);
33
34 2
        if(null === $handler) {
35 1
            throw new \RuntimeException(sprintf('Handler for class %s does not exist!', $class));
36
        }
37
38 1
        $this->handlers[$alias] = $handler;
39 1
        $this->aliases[$alias] = $this->aliases[$class];
40 1
    }
41
42 2
    public function getRoot($class)
43
    {
44 2
        return $this->aliases[$class];
45
    }
46
47 14
    public function getHandler($class)
48
    {
49 14
        if(array_key_exists($class, $this->handlers)) {
50 9
            return $this->handlers[$class];
51
        }
52
53 7
        $parents = array_intersect(array_keys($this->handlers), class_parents($class));
54 7
        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...
55 2
            return $this->handlers[array_pop($parents)];
56
        }
57
58 5
        $interfaces = array_intersect(array_keys($this->interfaces), array_values(class_implements($class)));
59 5
        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...
60 3
            if(count($interfaces) > 1) {
61 1
                throw new \RuntimeException(sprintf('Class %s implements interfaces with colliding handlers!', $class));
62
            }
63
64 2
            return $this->interfaces[$interfaces[0]];
65
        }
66
67 2
        return null;
68
    }
69
}
70