Passed
Pull Request — master (#181)
by Alex
05:35
created

AssociationStubBase   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 43
eloc 66
dl 0
loc 244
rs 8.96
c 1
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getRelationName() 0 3 1
A setMultiplicity() 0 3 1
A setKeyField() 0 3 1
A setRelationName() 0 3 1
A getKeyField() 0 3 1
A getMultiplicity() 0 3 1
A getForeignField() 0 3 1
A compare() 0 18 4
D isOk() 0 29 20
A setTargType() 0 3 1
A setThroughField() 0 3 1
A getThroughField() 0 3 1
A setBaseType() 0 3 1
A setForeignField() 0 3 1
A getTargType() 0 3 1
A getBaseType() 0 3 1
A isCompatible() 0 16 5

How to fix   Complexity   

Complex Class

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.

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 imtermate relation.
21
     *
22
     * @var string
23
     */
24
    protected $throughField;
25
26
    /**
27
     * Foreign key field of other end of relation.
28
     *
29
     * @var string
30
     */
31
    protected $foreignField;
32
33
    /**
34
     * @var string
35
     */
36
    protected $relationName;
37
38
    /**
39
     * Target type this relation points to, if known.  Is null for known-side polymorphic relations.
40
     *
41
     * @var string|null
42
     */
43
    protected $targType;
44
45
    /**
46
     * Base type this relation is attached to.
47
     *
48
     * @var string
49
     */
50
    protected $baseType;
51
52
    /**
53
     * @return string
54
     */
55
    public function getRelationName()
56
    {
57
        return $this->relationName;
58
    }
59
60
    /**
61
     * @param string $relationName
62
     */
63
    public function setRelationName($relationName)
64
    {
65
        $this->relationName = $relationName;
66
    }
67
68
    /**
69
     * @return AssociationStubRelationType
70
     */
71
    public function getMultiplicity()
72
    {
73
        return $this->multiplicity;
74
    }
75
76
    /**
77
     * @param AssociationStubRelationType $multiplicity
78
     */
79
    public function setMultiplicity(AssociationStubRelationType $multiplicity)
80
    {
81
        $this->multiplicity = $multiplicity;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getKeyField()
88
    {
89
        return $this->keyField;
90
    }
91
92
    /**
93
     * @param string $keyField
94
     */
95
    public function setKeyField($keyField)
96
    {
97
        $this->keyField = $keyField;
98
    }
99
100
    public function isCompatible(AssociationStubBase $otherStub)
101
    {
102
        if ($this->morphicType() != $otherStub->morphicType()) {
103
            return false;
104
        }
105
106
        if (!$this->isOk()) {
107
            return false;
108
        }
109
        if (!$otherStub->isOk()) {
110
            return false;
111
        }
112
        $thisMult = $this->getMultiplicity();
113
        $thatMult = $otherStub->getMultiplicity();
114
        return (AssociationStubRelationType::MANY()->getValue() == $thisMult->getValue()
115
                || $thisMult->getValue() != $thatMult->getValue());
116
    }
117
118
    /**
119
     * Is this AssociationStub sane?
120
     */
121
    public function isOk()
122
    {
123
        if (null === $this->multiplicity) {
124
            return false;
125
        }
126
        $relName = $this->relationName;
127
        if (null === $relName || !is_string($relName) || empty($relName)) {
0 ignored issues
show
introduced by
The condition is_string($relName) is always true.
Loading history...
128
            return false;
129
        }
130
        $keyField = $this->keyField;
131
        if (null === $keyField || !is_string($keyField) || empty($keyField)) {
0 ignored issues
show
introduced by
The condition is_string($keyField) is always true.
Loading history...
132
            return false;
133
        }
134
        $baseType = $this->baseType;
135
        if (null === $baseType || !is_string($baseType) || empty($baseType)) {
0 ignored issues
show
introduced by
The condition is_string($baseType) is always true.
Loading history...
136
            return false;
137
        }
138
        $targType = $this->targType;
139
        if ($this instanceof AssociationStubMonomorphic && null === $targType) {
140
            return false;
141
        }
142
        if (null !== $targType && (!is_string($targType) || empty($targType))) {
0 ignored issues
show
introduced by
The condition is_string($targType) is always true.
Loading history...
143
            return false;
144
        }
145
        $foreignField = $this->foreignField;
146
        if (null !== $targType && (null === $foreignField || !is_string($foreignField) || empty($foreignField))) {
0 ignored issues
show
introduced by
The condition is_string($foreignField) is always true.
Loading history...
147
            return false;
148
        }
149
        return (null === $targType) === (null === $foreignField);
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getTargType()
156
    {
157
        return $this->targType;
158
    }
159
160
    /**
161
     * @param string $targType
162
     */
163
    public function setTargType($targType)
164
    {
165
        $this->targType = $targType;
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    public function getBaseType()
172
    {
173
        return $this->baseType;
174
    }
175
176
    /**
177
     * @param string $baseType
178
     */
179
    public function setBaseType($baseType)
180
    {
181
        $this->baseType = $baseType;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getForeignField()
188
    {
189
        return $this->foreignField;
190
    }
191
192
    /**
193
     * @param string $foreignField
194
     */
195
    public function setForeignField($foreignField)
196
    {
197
        $this->foreignField = $foreignField;
198
    }
199
200
    /**
201
     * @return string
202
     */
203
    public function getThroughField()
204
    {
205
        return $this->throughField;
206
    }
207
208
    /**
209
     * @param string $foreignField
210
     */
211
    public function setThroughField($foreignField)
212
    {
213
        $this->throughField = $foreignField;
214
    }
215
216
    /**
217
     * Supply a canonical sort ordering to determine order in associations.
218
     *
219
     * @param AssociationStubBase $other
220
     *
221
     * @return int
222
     */
223
    public function compare(AssociationStubBase $other)
224
    {
225
        $thisClass = get_class($this);
226
        $otherClass = get_class($other);
227
        $classComp = strcmp($thisClass, $otherClass);
228
        if (0 !== $classComp) {
229
            return $classComp / abs($classComp);
230
        }
231
        $thisBase = $this->getBaseType();
232
        $otherBase = $other->getBaseType();
233
        $baseComp = strcmp($thisBase, $otherBase);
234
        if (0 !== $baseComp) {
235
            return $baseComp / abs($baseComp);
236
        }
237
        $thisMethod = $this->getRelationName();
238
        $otherMethod = $other->getRelationName();
239
        $methodComp = strcmp($thisMethod, $otherMethod);
240
        return 0 === $methodComp ? 0 : $methodComp / abs($methodComp);
241
    }
242
243
    /**
244
     * Return what type of stub this is - polymorphic, monomorphic, or something else
245
     *
246
     * @return string
247
     */
248
    abstract public function morphicType();
249
}
250