Passed
Pull Request — master (#181)
by Alex
06:13
created

AssociationStubBase::isOk()   C

Complexity

Conditions 14
Paths 8

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 26
rs 6.2666
cc 14
nc 8
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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|null
16
     */
17
    protected $keyField;
18
19
    /**
20
     * Foreign key field of imtermate relation.
21
     *
22
     * @var string|null
23
     */
24
    protected $throughField;
25
26
    /**
27
     * Foreign key field of other end of relation.
28
     *
29
     * @var string|null
30
     */
31
    protected $foreignField;
32
33
    /**
34
     * @var string|null
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|null
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 = $this->checkStringInput($relationName) ? $relationName : $this->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 = $this->checkStringInput($keyField) ? $keyField : $this->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
        if (null === $this->relationName) {
127
            return false;
128
        }
129
        if (null === $this->keyField) {
130
            return false;
131
        }
132
        if (null === $this->baseType) {
133
            return false;
134
        }
135
        $targType = $this->targType;
136
        if ($this instanceof AssociationStubMonomorphic && null === $targType) {
137
            return false;
138
        }
139
        if (null !== $targType && (!is_string($targType) || empty($targType))) {
0 ignored issues
show
introduced by
The condition is_string($targType) is always true.
Loading history...
140
            return false;
141
        }
142
        $foreignField = $this->foreignField;
143
        if (null !== $targType && (null === $foreignField || !is_string($foreignField) || empty($foreignField))) {
144
            return false;
145
        }
146
        return (null === $targType) === (null === $foreignField);
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getTargType()
153
    {
154
        return $this->targType;
155
    }
156
157
    /**
158
     * @param string $targType
159
     */
160
    public function setTargType($targType)
161
    {
162
        $this->targType = $targType;
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getBaseType()
169
    {
170
        return $this->baseType;
171
    }
172
173
    /**
174
     * @param string $baseType
175
     */
176
    public function setBaseType($baseType)
177
    {
178
        $this->baseType = $this->checkStringInput($baseType) ? $baseType : $this->baseType;
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function getForeignField()
185
    {
186
        return $this->foreignField;
187
    }
188
189
    /**
190
     * @param string $foreignField
191
     */
192
    public function setForeignField($foreignField)
193
    {
194
        $this->foreignField = $foreignField;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getThroughField()
201
    {
202
        return $this->throughField;
203
    }
204
205
    /**
206
     * @param string $foreignField
207
     */
208
    public function setThroughField($foreignField)
209
    {
210
        $this->throughField = $foreignField;
211
    }
212
213
    /**
214
     * Supply a canonical sort ordering to determine order in associations.
215
     *
216
     * @param AssociationStubBase $other
217
     *
218
     * @return int
219
     */
220
    public function compare(AssociationStubBase $other)
221
    {
222
        $thisClass = get_class($this);
223
        $otherClass = get_class($other);
224
        $classComp = strcmp($thisClass, $otherClass);
225
        if (0 !== $classComp) {
226
            return $classComp / abs($classComp);
227
        }
228
        $thisBase = $this->getBaseType();
229
        $otherBase = $other->getBaseType();
230
        $baseComp = strcmp($thisBase, $otherBase);
231
        if (0 !== $baseComp) {
232
            return $baseComp / abs($baseComp);
233
        }
234
        $thisMethod = $this->getRelationName();
235
        $otherMethod = $other->getRelationName();
236
        $methodComp = strcmp($thisMethod, $otherMethod);
237
        return 0 === $methodComp ? 0 : $methodComp / abs($methodComp);
238
    }
239
240
    /**
241
     * Return what type of stub this is - polymorphic, monomorphic, or something else
242
     *
243
     * @return string
244
     */
245
    abstract public function morphicType();
246
247
    /**
248
     * @param $input
249
     * @return bool
250
     */
251
    private function checkStringInput($input)
252
    {
253
        if (null === $input || !is_string($input) || empty($input)) {
254
            return false;
255
        }
256
        return true;
257
    }
258
}
259