Completed
Push — master ( e24a08...649c71 )
by Joshua
9s
created

RelationshipsTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 1
dl 79
loc 79
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addRelationship() 7 7 1
A getRelationship() 7 7 2
A getRelationships() 4 4 1
A hasRelationship() 4 4 1
A hasRelationships() 4 4 1
validateRelationship() 1 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
3
namespace As3\Modlr\Metadata\Traits;
4
5
use As3\Modlr\Exception\MetadataException;
6
use As3\Modlr\Metadata\RelationshipMetadata;
7
8
/**
9
 * Common relationship metadata get, set, and add methods.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13 View Code Duplication
trait RelationshipsTrait
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...
14
{
15
    /**
16
     * All relationship fields assigned to this metadata object.
17
     * A relationship is a field that relates to another entity.
18
     *
19
     * @var RelationshipMetadata[]
20
     */
21
    public $relationships = [];
22
23
    /**
24
     * Adds a relationship field to this entity.
25
     *
26
     * @param   RelationshipMetadata    $relationship
27
     * @return  self
28
     */
29
    public function addRelationship(RelationshipMetadata $relationship)
30
    {
31
        $this->validateRelationship($relationship);
32
        $this->relationships[$relationship->getKey()] = $relationship;
33
        ksort($this->relationships);
34
        return $this;
35
    }
36
37
    /**
38
     * Gets a relationship field from this entity.
39
     * Returns null if the relationship does not exist.
40
     *
41
     * @param   string  $key
42
     * @return  RelationshipMetadata|null
43
     */
44
    public function getRelationship($key)
45
    {
46
        if (!isset($this->relationships[$key])) {
47
            return null;
48
        }
49
        return $this->relationships[$key];
50
    }
51
52
    /**
53
     * Gets all relationship fields for this entity.
54
     *
55
     * @return  RelationshipMetadata[]
56
     */
57
    public function getRelationships()
58
    {
59
        return $this->relationships;
60
    }
61
62
    /**
63
     * Determines if a relationship field exists on this entity.
64
     *
65
     * @param   string  $key
66
     * @return  bool
67
     */
68
    public function hasRelationship($key)
69
    {
70
        return null !== $this->getRelationship($key);
71
    }
72
73
    /**
74
     * Determines any relationship fields exist.
75
     *
76
     * @return  bool
77
     */
78
    public function hasRelationships()
79
    {
80
        return !empty($this->relationships);
81
    }
82
83
    /**
84
     * Validates that the relationship can be added.
85
     *
86
     * @param   RelationshipMetadata    $relationship
87
     * @return  self
88
     * @throws  MetadataException
89
     */
90
    abstract protected function validateRelationship(RelationshipMetadata $relationship);
91
}
92