Test Failed
Push — master ( 4381fd...f48087 )
by Alex
01:40
created

AssociationStubBase::isCompatible()   C

Complexity

Conditions 12
Paths 96

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 5.1612
cc 12
eloc 18
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;
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
     * @var string
34
     */
35
    protected $targType;
36
37
    /**
38
     * Base type this relation is attached to
39
     * @var string
40
     */
41
    protected $baseType;
42
43
    /**
44
     * @return string
45
     */
46
    public function getRelationName()
47
    {
48
        return $this->relationName;
49
    }
50
51
    /**
52
     * @param string $relationName
53
     */
54
    public function setRelationName($relationName)
55
    {
56
        $this->relationName = $relationName;
57
    }
58
59
    /**
60
     * @return AssociationStubRelationType
61
     */
62
    public function getMultiplicity()
63
    {
64
        return $this->multiplicity;
65
    }
66
67
    /**
68
     * @param AssociationStubRelationType $multiplicity
69
     */
70
    public function setMultiplicity(AssociationStubRelationType $multiplicity)
71
    {
72
        $this->multiplicity = $multiplicity;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getKeyField()
79
    {
80
        return $this->keyField;
81
    }
82
83
    /**
84
     * @param string $keyField
85
     */
86
    public function setKeyField($keyField)
87
    {
88
        $this->keyField = $keyField;
89
    }
90
91
    public function isCompatible(AssociationStubBase $otherStub)
92
    {
93
        $thisPoly = $this instanceof AssociationStubPolymorphic;
94
        $thatPoly = $otherStub instanceof AssociationStubPolymorphic;
95
        $thisMono = $this instanceof AssociationStubMonomorphic;
96
        $thatMono = $otherStub instanceof AssociationStubMonomorphic;
97
98
        $count = ($thisPoly ? 1 : 0) + ($thatPoly ? 1 : 0) + ($thisMono ? 1 : 0) + ($thatMono ? 1 : 0);
99
        assert(2 == $count);
100
101
        if ($thisPoly && $thatMono) {
102
            return false;
103
        }
104
        if ($thisMono && $thatPoly) {
105
            return false;
106
        }
107
        if (!$this->isOk()) {
108
            return false;
109
        }
110
        if (!$otherStub->isOk()) {
111
            return false;
112
        }
113
        $thisMult = $this->getMultiplicity();
114
        $thatMult = $otherStub->getMultiplicity();
115
        return (AssociationStubRelationType::MANY() == $thisMult || $thisMult != $thatMult);
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)) {
128
            return false;
129
        }
130
        $keyField = $this->keyField;
131
        if (null === $keyField || !is_string($keyField) || empty($keyField)) {
132
            return false;
133
        }
134
        $baseType = $this->baseType;
135
        if (null === $baseType || !is_string($baseType) || empty($baseType)) {
136
            return false;
137
        }
138
        $targType = $this->targType;
139
        if ($this instanceof AssociationStubMonomorphic && null === $targType) {
140
            return false;
141
        }
142 View Code Duplication
        if (null !== $targType && (!is_string($targType) || empty($targType))) {
143
            return false;
144
        }
145
        $foreignField = $this->foreignField;
146 View Code Duplication
        if (null !== $targType && (null === $foreignField || !is_string($foreignField) || empty($foreignField))) {
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
     * Supply a canonical sort ordering to determine order in associations
202
     *
203
     * @param AssociationStubBase $other
204
     *
205
     * @return integer
206
     */
207
    public function compare(AssociationStubBase $other)
208
    {
209
        $thisClass = get_class($this);
210
        $otherClass = get_class($other);
211
        $classComp = strcmp($thisClass, $otherClass);
212
        if (0 !== $classComp) {
213
            return $classComp / abs($classComp);
214
        }
215
        $thisBase = $this->getBaseType();
216
        $otherBase = $other->getBaseType();
217
        $baseComp = strcmp($thisBase, $otherBase);
218
        if (0 !== $baseComp) {
219
            return $baseComp / abs($baseComp);
220
        }
221
        $thisMethod = $this->getRelationName();
222
        $otherMethod = $other->getRelationName();
223
        $methodComp = strcmp($thisMethod, $otherMethod);
224
        return 0 === $methodComp ? 0 : $methodComp / abs($methodComp);
225
    }
226
}
227