RelationshipProcessor   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 21.74 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 15
loc 69
ccs 26
cts 27
cp 0.963
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 15 15 3
A createRelationship() 0 19 3
A handleLinks() 0 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
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Definition\ConfigurationProcessor;
5
6
use Mikemirten\Component\JsonApi\Mapper\Definition\Definition;
7
use Mikemirten\Component\JsonApi\Mapper\Definition\Relationship;
8
9
/**
10
 * Processor of relationships
11
 *
12
 * @package Mikemirten\Component\JsonApi\Mapper\Definition\AnnotationProcessor
13
 */
14
class RelationshipProcessor extends AbstractProcessor
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 2 View Code Duplication
    public function process(array $config, Definition $definition)
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...
20
    {
21 2
        if (! isset($config['relationships'])) {
22
            return;
23
        }
24
25 2
        $reflection = new \ReflectionClass($definition->getClass());
26
27 2
        foreach ($config['relationships'] as $name => $data)
28
        {
29 2
            $relationship = $this->createRelationship($reflection, $name, $data);
30
31 2
            $definition->addRelationship($relationship);
32
        }
33 2
    }
34
35
    /**
36
     * Process relationship
37
     *
38
     * @param  \ReflectionClass $reflection
39
     * @param  string           $name
40
     * @param  array            $data
41
     * @return Relationship
42
     */
43 2
    protected function createRelationship(\ReflectionClass $reflection, string $name, array $data): Relationship
44
    {
45 2
        $getter = isset($data['getter'])
46 2
            ? $data['getter']
47 2
            : $this->resolveGetter($reflection, $name);
48
49 2
        $type = ($data['type'] === 'one')
50 2
            ? Relationship::TYPE_X_TO_ONE
51 2
            : Relationship::TYPE_X_TO_MANY;
52
53 2
        $relationship = new Relationship($name, $type, $getter);
54
55 2
        $relationship->setIncludeData($data['dataAllowed']);
56 2
        $relationship->setDataLimit($data['dataLimit']);
57
58 2
        $this->handleLinks($data, $relationship);
59
60 2
        return $relationship;
61
    }
62
63
    /**
64
     * Handle links
65
     *
66
     * @param array        $data
67
     * @param Relationship $relationship
68
     */
69 2
    protected function handleLinks(array $data, Relationship $relationship)
70
    {
71 2
        if (! isset($data['links'])) {
72 1
            return;
73
        }
74
75 1
        foreach ($data['links'] as $linkName => $linkData)
76
        {
77 1
            $link = $this->createLink($linkName, $linkData);
78
79 1
            $relationship->addLink($link);
80
        }
81
    }
82
}