FallbackHydratorContainer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 47.27 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 26
loc 55
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 12 12 3
A addAlias() 7 7 1
A getHandler() 7 22 5
A hydrate() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Thunder\Serializard\HydratorContainer;
3
4
use Thunder\Serializard\Exception\HydratorConflictException;
5
use Thunder\Serializard\Exception\HydratorNotFoundException;
6
use Thunder\Serializard\Exception\ClassNotFoundException;
7
8
/**
9
 * @author Tomasz Kowalczyk <[email protected]>
10
 */
11
final class FallbackHydratorContainer implements HydratorContainerInterface
12
{
13
    private $handlers = [];
14
    private $interfaces = [];
15
    private $aliases = [];
16
17 12 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...
18
    {
19 12
        if(class_exists($class)) {
20 9
            $this->aliases[$class] = $class;
21 9
            $this->handlers[$class] = $handler;
22 3
        } elseif(interface_exists($class)) {
23 2
            $this->aliases[$class] = $class;
24 2
            $this->interfaces[$class] = $handler;
25
        } else {
26 1
            throw ClassNotFoundException::fromClass($class);
27
        }
28 11
    }
29
30 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...
31
    {
32 2
        $handler = $this->getHandler($class);
33
34 1
        $this->handlers[$alias] = $handler;
35 1
        $this->aliases[$alias] = $this->aliases[$class];
36 1
    }
37
38 10
    public function getHandler($class)
39
    {
40 10
        if(array_key_exists($class, $this->handlers)) {
41 6
            return $this->handlers[$class];
42
        }
43
44 5
        $parents = array_intersect(array_keys($this->handlers), class_parents($class));
45 5
        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...
46 1
            return $this->handlers[array_pop($parents)];
47
        }
48
49 4
        $interfaces = array_intersect(array_keys($this->interfaces), array_values(class_implements($class)));
50 4 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...
51 2
            if(\count($interfaces) > 1) {
52 1
                throw HydratorConflictException::fromClass($class, $interfaces);
53
            }
54
55 1
            return $this->interfaces[array_shift($interfaces)];
56
        }
57
58 2
        throw HydratorNotFoundException::fromClass($class);
59
    }
60
61 1
    public function hydrate($class, array $data)
62
    {
63 1
        return \call_user_func($this->getHandler($class), $data, $this);
64
    }
65
}
66