Passed
Pull Request — master (#154)
by Alex
07:05
created

AssociationStubBase::isCompatible()   C

Complexity

Conditions 12
Paths 96

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 5.1612
cc 12
eloc 17
nc 96
nop 1

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
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
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
        $thisPoly = $this instanceof AssociationStubPolymorphic;
103
        $thatPoly = $otherStub instanceof AssociationStubPolymorphic;
104
        $thisMono = $this instanceof AssociationStubMonomorphic;
105
        $thatMono = $otherStub instanceof AssociationStubMonomorphic;
106
107
        $count = ($thisPoly ? 1 : 0)+($thatPoly ? 1 : 0)+($thisMono ? 1 : 0)+($thatMono ? 1 : 0);
108
        /** @scrutinizer ignore-call */
109
        assert(2 == $count);
110
        if ($thisPoly && $thatMono) {
111
            return false;
112
        }
113
        if ($thisMono && $thatPoly) {
114
            return false;
115
        }
116
        if (!$this->isOk()) {
117
            return false;
118
        }
119
        if (!$otherStub->isOk()) {
120
            return false;
121
        }
122
        $thisMult = $this->getMultiplicity();
123
        $thatMult = $otherStub->getMultiplicity();
124
        return (AssociationStubRelationType::MANY() == $thisMult || $thisMult != $thatMult);
125
    }
126
127
    /**
128
     * Is this AssociationStub sane?
129
     */
130
    public function isOk()
131
    {
132
        if (null === $this->multiplicity) {
133
            return false;
134
        }
135
        $relName = $this->relationName;
136
        if (null === $relName || !is_string($relName) || empty($relName)) {
137
            return false;
138
        }
139
        $keyField = $this->keyField;
140
        if (null === $keyField || !is_string($keyField) || empty($keyField)) {
141
            return false;
142
        }
143
        $baseType = $this->baseType;
144
        if (null === $baseType || !is_string($baseType) || empty($baseType)) {
145
            return false;
146
        }
147
        $targType = $this->targType;
148
        if ($this instanceof AssociationStubMonomorphic && null === $targType) {
149
            return false;
150
        }
151 View Code Duplication
        if (null !== $targType && (!is_string($targType) || empty($targType))) {
152
            return false;
153
        }
154
        $foreignField = $this->foreignField;
155 View Code Duplication
        if (null !== $targType && (null === $foreignField || !is_string($foreignField) || empty($foreignField))) {
156
            return false;
157
        }
158
        return (null === $targType) === (null === $foreignField);
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    public function getTargType()
165
    {
166
        return $this->targType;
167
    }
168
169
    /**
170
     * @param string $targType
171
     */
172
    public function setTargType($targType)
173
    {
174
        $this->targType = $targType;
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getBaseType()
181
    {
182
        return $this->baseType;
183
    }
184
185
    /**
186
     * @param string $baseType
187
     */
188
    public function setBaseType($baseType)
189
    {
190
        $this->baseType = $baseType;
191
    }
192
193
    /**
194
     * @return string
195
     */
196
    public function getForeignField()
197
    {
198
        return $this->foreignField;
199
    }
200
201
    /**
202
     * @param string $foreignField
203
     */
204
    public function setForeignField($foreignField)
205
    {
206
        $this->foreignField = $foreignField;
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getThroughField()
213
    {
214
        return $this->throughField;
215
    }
216
217
    /**
218
     * @param string $foreignField
219
     */
220
    public function setThroughField($foreignField)
221
    {
222
        $this->throughField = $foreignField;
223
    }
224
225
    /**
226
     * Supply a canonical sort ordering to determine order in associations.
227
     *
228
     * @param AssociationStubBase $other
229
     *
230
     * @return int
231
     */
232
    public function compare(AssociationStubBase $other)
233
    {
234
        $thisClass = get_class($this);
235
        $otherClass = get_class($other);
236
        $classComp = strcmp($thisClass, $otherClass);
237
        if (0 !== $classComp) {
238
            return $classComp / abs($classComp);
239
        }
240
        $thisBase = $this->getBaseType();
241
        $otherBase = $other->getBaseType();
242
        $baseComp = strcmp($thisBase, $otherBase);
243
        if (0 !== $baseComp) {
244
            return $baseComp / abs($baseComp);
245
        }
246
        $thisMethod = $this->getRelationName();
247
        $otherMethod = $other->getRelationName();
248
        $methodComp = strcmp($thisMethod, $otherMethod);
249
        return 0 === $methodComp ? 0 : $methodComp / abs($methodComp);
250
    }
251
}
252