Passed
Pull Request — master (#153)
by Alex
07:18
created

AssociationStubBase::getThroughField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
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
        assert(2 == $count);
0 ignored issues
show
Bug introduced by
The call to assert() has too few arguments starting with description. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
        /** @scrutinizer ignore-call */ 
109
        assert(2 == $count);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

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