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

AssociationStubBase::isCompatible()   C

Complexity

Conditions 12
Paths 96

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 5.1612
c 0
b 0
f 0
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\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