Completed
Push — master ( 2e0cd6...c623e8 )
by Pavel
12:31
created

JmsDoctrineHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 26 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 84.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 2
dl 13
loc 50
ccs 16
cts 19
cp 0.8421
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serializeRelation() 0 12 3
A deserializeRelation() 0 5 1
A convertEntityToIds() 13 13 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
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\ORM\Mapping\ClassMetadata;
7
use JMS\Serializer\Context;
8
use JMS\Serializer\VisitorInterface;
9
10
final class JmsDoctrineHandler
11
{
12
    const TYPE = 'DoctrineAssociation';
13
14
    /** @var  ManagerRegistry */
15
    private $registry;
16
17
    /**
18
     * JmsDoctrineHandler constructor.
19
     *
20
     * @param ManagerRegistry $registry
21
     */
22 2
    public function __construct(ManagerRegistry $registry)
23
    {
24 2
        $this->registry = $registry;
25 2
    }
26
27 2
    public function serializeRelation(VisitorInterface $visitor, $relation, array $type, Context $context)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29 2
        if ($relation instanceof \Traversable) {
30 2
            $relation = iterator_to_array($relation);
31 2
        }
32
33 2
        if (is_array($relation)) {
34 2
            return array_map([$this, 'convertEntityToIds'], $relation);
35
        }
36
37 1
        return $this->convertEntityToIds($relation);
38
    }
39
40
    public function deserializeRelation(VisitorInterface $visitor, $data, array $type, Context $context)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        // fixme
43
        throw new \BadMethodCallException('Not supported at the moment');
44
    }
45
46 1 View Code Duplication
    private function convertEntityToIds($entity)
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...
47
    {
48 1
        $class    = get_class($entity);
49 1
        $metadata = $this->registry->getManagerForClass($class)->getClassMetadata($class);
50
51 1
        $ids = $metadata->getIdentifierValues($entity);
52
53 1
        if (!$metadata instanceof ClassMetadata || $metadata->isIdentifierComposite) {
54
            return $ids;
55
        }
56
57 1
        return array_shift($ids);
58
    }
59
}
60