Test Failed
Pull Request — master (#115)
by Alex
03:29
created

MetadataGubbinsHolder   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 93
Duplicated Lines 25.81 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 24
loc 93
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addEntity() 0 9 2
B getRelationsByRelationName() 0 26 5
B getRelationsByClass() 0 22 5
A getRelations() 17 17 4
A checkClassExists() 7 7 2

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 AlgoWeb\PODataLaravel\Models;
4
5
class MetadataGubbinsHolder
6
{
7
    protected $relations = [];
8
9
    public function addEntity(EntityGubbins $entity)
10
    {
11
        $className = $entity->getClassName();
12
        if (array_key_exists($className, $this->relations)) {
13
            $msg = $className.' already added';
14
            throw new \InvalidArgumentException($msg);
15
        }
16
        $this->relations[$className] = $entity;
17
    }
18
19
    public function getRelationsByRelationName($className, $relName)
20
    {
21
        $this->checkClassExists($className);
22
23
        $rels = $this->relations[$className];
24
25
        if (!array_key_exists($relName, $rels->getStubs())) {
26
            $msg = 'Relation ' . $relName . ' not registered on ' . $className;
27
            throw new \InvalidArgumentException($msg);
28
        }
29
        $stub = $rels->getStubs()[$relName];
30
        $targType = $stub->getTargType();
31
        if (!array_key_exists($targType, $this->relations)) {
32
            return [];
33
        }
34
        $targRel = $this->relations[$targType];
35
        // now dig out compatible stubs on target type
36
        $targStubs = $targRel->getStubs();
37
        $relStubs = [];
38
        foreach ($targStubs as $targStub) {
39
            if ($stub->isCompatible($targStub)) {
40
                $relStubs[] = $targStub;
41
            }
42
        }
43
        return $relStubs;
44
    }
45
46
    public function getRelationsByClass($className)
47
    {
48
        $this->checkClassExists($className);
49
50
        $rels = $this->relations[$className];
51
        $stubs = $rels->getStubs();
52
53
        $associations = [];
54
        foreach ($stubs as $relName => $stub) {
55
            $others = $this->getRelationsByRelationName($className, $relName);
56
            if (1 === count($others)) {
57
                $others = $others[0];
58
                $assoc = new Association();
59
                $first = -1 === $stub->compare($others);
60
                $assoc->setFirst($first ? $stub : $others);
61
                $assoc->setLast($first ? $others : $stub);
62
                assert($assoc->isOk());
63
                $associations[] = $assoc;
64
            }
65
        }
66
        return $associations;
67
    }
68
69 View Code Duplication
    public function getRelations()
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...
70
    {
71
        $classNames = array_keys($this->relations);
72
73
        $associations = [];
74
75
        foreach ($classNames as $class) {
76
            $rawAssoc = $this->getRelationsByClass($class);
77
            foreach ($rawAssoc as $raw) {
78
                if (!in_array($raw, $associations)) {
79
                    $associations[] = $raw;
80
                }
81
            }
82
        }
83
84
        return $associations;
85
    }
86
87
    /**
88
     * @param $className
89
     */
90 View Code Duplication
    protected function checkClassExists($className)
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...
91
    {
92
        if (!array_key_exists($className, $this->relations)) {
93
            $msg = $className . ' does not exist in holder';
94
            throw new \InvalidArgumentException($msg);
95
        }
96
    }
97
}
98