Passed
Pull Request — master (#11)
by Dominik
04:20 queued 01:29
created

DenormalizerObjectMappingRegistry   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 59
loc 59
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 2
A addObjectMapping() 4 4 1
B getObjectMapping() 25 25 3

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
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Doctrine\Denormalizer;
6
7
use Chubbyphp\Deserialization\Denormalizer\DenormalizerObjectMappingRegistryInterface;
8
use Chubbyphp\Deserialization\DeserializerLogicException;
9
use Chubbyphp\Deserialization\Mapping\DenormalizationObjectMappingInterface;
10
use Doctrine\Common\Persistence\Proxy;
11
12 View Code Duplication
final class DenormalizerObjectMappingRegistry implements DenormalizerObjectMappingRegistryInterface
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...
13
{
14
    /**
15
     * @var DenormalizationObjectMappingInterface[]
16
     */
17
    private $objectMappings;
18
19
    /**
20
     * @param array $objectMappings
21
     */
22
    public function __construct(array $objectMappings)
23
    {
24
        $this->objectMappings = [];
25
        foreach ($objectMappings as $objectMapping) {
26
            $this->addObjectMapping($objectMapping);
27
        }
28
    }
29
30
    /**
31
     * @param DenormalizationObjectMappingInterface $objectMapping
32
     */
33
    private function addObjectMapping(DenormalizationObjectMappingInterface $objectMapping)
34
    {
35
        $this->objectMappings[$objectMapping->getClass()] = $objectMapping;
36
    }
37
38
    /**
39
     * @param string $class
40
     *
41
     * @return DenormalizationObjectMappingInterface
42
     *
43
     * @throws DeserializerLogicException
44
     */
45
    public function getObjectMapping(string $class): DenormalizationObjectMappingInterface
46
    {
47
        $reflectionClass = new \ReflectionClass($class);
48
49
        if (in_array(Proxy::class, $reflectionClass->getInterfaceNames(), true)) {
50
            $parentClass = $reflectionClass->getParentClass()->name;
51
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
52
                sprintf(
53
                    'Use "%s" instead of "%s" for "%s"',
54
                    DoctrineDenormalizerObjectMappingRegistry::class,
55
                    self::class,
56
                    $parentClass
57
                ),
58
                E_USER_DEPRECATED
59
            );
60
61
            $class = $parentClass;
62
        }
63
64
        if (isset($this->objectMappings[$class])) {
65
            return $this->objectMappings[$class];
66
        }
67
68
        throw DeserializerLogicException::createMissingMapping($class);
69
    }
70
}
71