Completed
Push — master ( e5c932...e18320 )
by Alex
02:04
created

AssociationStubBase   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 223
Duplicated Lines 2.69 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 48
lcom 1
cbo 1
dl 6
loc 223
rs 8.4864
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getRelationName() 0 4 1
A setRelationName() 0 4 1
A getMultiplicity() 0 4 1
A setMultiplicity() 0 4 1
A getKeyField() 0 4 1
A setKeyField() 0 4 1
C isCompatible() 0 25 12
D isOk() 6 30 20
A getTargType() 0 4 1
A setTargType() 0 4 1
A getBaseType() 0 4 1
A setBaseType() 0 4 1
A getForeignField() 0 4 1
A setForeignField() 0 4 1
A compare() 0 19 4

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like AssociationStubBase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AssociationStubBase, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations;
4
5
abstract class AssociationStubBase
6
{
7
    /**
8
     * @var AssociationStubRelationType
9
     */
10
    protected $multiplicity;
11
12
    /**
13
     * Foreign key field of this end of relation.
14
     *
15
     * @var string
16
     */
17
    protected $keyField;
18
19
    /**
20
     * Foreign key field of other end of relation.
21
     *
22
     * @var string
23
     */
24
    protected $foreignField;
25
26
    /**
27
     * @var string
28
     */
29
    protected $relationName;
30
31
    /**
32
     * Target type this relation points to, if known.  Is null for known-side polymorphic relations.
33
     *
34
     * @var string
35
     */
36
    protected $targType;
37
38
    /**
39
     * Base type this relation is attached to.
40
     *
41
     * @var string
42
     */
43
    protected $baseType;
44
45
    /**
46
     * @return string
47
     */
48
    public function getRelationName()
49
    {
50
        return $this->relationName;
51
    }
52
53
    /**
54
     * @param string $relationName
55
     */
56
    public function setRelationName($relationName)
57
    {
58
        $this->relationName = $relationName;
59
    }
60
61
    /**
62
     * @return AssociationStubRelationType
63
     */
64
    public function getMultiplicity()
65
    {
66
        return $this->multiplicity;
67
    }
68
69
    /**
70
     * @param AssociationStubRelationType $multiplicity
71
     */
72
    public function setMultiplicity(AssociationStubRelationType $multiplicity)
73
    {
74
        $this->multiplicity = $multiplicity;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getKeyField()
81
    {
82
        return $this->keyField;
83
    }
84
85
    /**
86
     * @param string $keyField
87
     */
88
    public function setKeyField($keyField)
89
    {
90
        $this->keyField = $keyField;
91
    }
92
93
    public function isCompatible(AssociationStubBase $otherStub)
94
    {
95
        $thisPoly = $this instanceof AssociationStubPolymorphic;
96
        $thatPoly = $otherStub instanceof AssociationStubPolymorphic;
97
        $thisMono = $this instanceof AssociationStubMonomorphic;
98
        $thatMono = $otherStub instanceof AssociationStubMonomorphic;
99
100
        $count = ($thisPoly ? 1 : 0)+($thatPoly ? 1 : 0)+($thisMono ? 1 : 0)+($thatMono ? 1 : 0);
101
        assert(2 == $count);
102
        if ($thisPoly && $thatMono) {
103
            return false;
104
        }
105
        if ($thisMono && $thatPoly) {
106
            return false;
107
        }
108
        if (!$this->isOk()) {
109
            return false;
110
        }
111
        if (!$otherStub->isOk()) {
112
            return false;
113
        }
114
        $thisMult = $this->getMultiplicity();
115
        $thatMult = $otherStub->getMultiplicity();
116
        return (AssociationStubRelationType::MANY() == $thisMult || $thisMult != $thatMult);
117
    }
118
119
    /**
120
     * Is this AssociationStub sane?
121
     */
122
    public function isOk()
123
    {
124
        if (null === $this->multiplicity) {
125
            return false;
126
        }
127
        $relName = $this->relationName;
128
        if (null === $relName || !is_string($relName) || empty($relName)) {
129
            return false;
130
        }
131
        $keyField = $this->keyField;
132
        if (null === $keyField || !is_string($keyField) || empty($keyField)) {
133
            return false;
134
        }
135
        $baseType = $this->baseType;
136
        if (null === $baseType || !is_string($baseType) || empty($baseType)) {
137
            return false;
138
        }
139
        $targType = $this->targType;
140
        if ($this instanceof AssociationStubMonomorphic && null === $targType) {
141
            return false;
142
        }
143 View Code Duplication
        if (null !== $targType && (!is_string($targType) || empty($targType))) {
144
            return false;
145
        }
146
        $foreignField = $this->foreignField;
147 View Code Duplication
        if (null !== $targType && (null === $foreignField || !is_string($foreignField) || empty($foreignField))) {
148
            return false;
149
        }
150
        return (null === $targType) === (null === $foreignField);
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getTargType()
157
    {
158
        return $this->targType;
159
    }
160
161
    /**
162
     * @param string $targType
163
     */
164
    public function setTargType($targType)
165
    {
166
        $this->targType = $targType;
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    public function getBaseType()
173
    {
174
        return $this->baseType;
175
    }
176
177
    /**
178
     * @param string $baseType
179
     */
180
    public function setBaseType($baseType)
181
    {
182
        $this->baseType = $baseType;
183
    }
184
185
    /**
186
     * @return string
187
     */
188
    public function getForeignField()
189
    {
190
        return $this->foreignField;
191
    }
192
193
    /**
194
     * @param string $foreignField
195
     */
196
    public function setForeignField($foreignField)
197
    {
198
        $this->foreignField = $foreignField;
199
    }
200
201
    /**
202
     * Supply a canonical sort ordering to determine order in associations.
203
     *
204
     * @param AssociationStubBase $other
205
     *
206
     * @return int
207
     */
208
    public function compare(AssociationStubBase $other)
209
    {
210
        $thisClass = get_class($this);
211
        $otherClass = get_class($other);
212
        $classComp = strcmp($thisClass, $otherClass);
213
        if (0 !== $classComp) {
214
            return $classComp / abs($classComp);
215
        }
216
        $thisBase = $this->getBaseType();
217
        $otherBase = $other->getBaseType();
218
        $baseComp = strcmp($thisBase, $otherBase);
219
        if (0 !== $baseComp) {
220
            return $baseComp / abs($baseComp);
221
        }
222
        $thisMethod = $this->getRelationName();
223
        $otherMethod = $other->getRelationName();
224
        $methodComp = strcmp($thisMethod, $otherMethod);
225
        return 0 === $methodComp ? 0 : $methodComp / abs($methodComp);
226
    }
227
}
228