Test Failed
Pull Request — master (#115)
by Alex
03:27
created

AssociationStubBase::getMultiplicity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Models;
4
5
abstract class AssociationStubBase
1 ignored issue
show
Coding Style introduced by
AssociationStubBase does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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 mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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
        if (AssociationStubRelationType::MANY() != $thisMult && $thisMult == $thatMult) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(\AlgoWeb\POData...thisMult == $thatMult);.
Loading history...
116
            return false;
117
        }
118
119
        return true;
120
    }
121
122
    /**
123
     * Is this AssociationStub sane?
124
     */
125
    public function isOk()
126
    {
127
        if (null === $this->multiplicity) {
128
            return false;
129
        }
130
        $relName = $this->relationName;
131
        if (null === $relName || !is_string($relName) || empty($relName)) {
132
            return false;
133
        }
134
        $keyField = $this->keyField;
135
        if (null === $keyField || !is_string($keyField) || empty($keyField)) {
136
            return false;
137
        }
138
        $baseType = $this->baseType;
139
        if (null === $baseType || !is_string($baseType) || empty($baseType)) {
140
            return false;
141
        }
142
        $targType = $this->targType;
143
        if ($this instanceof AssociationStubMonomorphic && null === $targType) {
144
            return false;
145
        }
146 View Code Duplication
        if (null !== $targType && (!is_string($targType) || empty($targType))) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
            return false;
148
        }
149
        $foreignField = $this->foreignField;
150 View Code Duplication
        if (null !== $targType && (null === $foreignField || !is_string($foreignField) || empty($foreignField))) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
            return false;
152
        }
153
        if (null === $targType && null !== $foreignField) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(null === $targT...ull !== $foreignField);.
Loading history...
154
            return false;
155
        }
156
157
        return true;
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
     * Supply a canonical sort ordering to determine order in associations
210
     *
211
     * @param AssociationStubBase $other
212
     *
213
     * @return int
1 ignored issue
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
214
     */
215
    public function compare(AssociationStubBase $other)
216
    {
217
        $thisClass = get_class($this);
218
        $otherClass = get_class($other);
219
        if ($thisClass !== $otherClass) {
220
            return $thisClass < $otherClass ? -1 : 1;
221
        }
222
        $thisBase = $this->getBaseType();
223
        $otherBase = $other->getBaseType();
224
        if ($thisBase !== $otherBase) {
225
            return $thisBase < $otherBase ? -1 : 1;
226
        }
227
        $thisMethod = $this->getRelationName();
228
        $otherMethod = $other->getRelationName();
229
        $methodComp = strcmp($thisMethod, $otherMethod);
230
        return 0 === $methodComp ? 0 : $methodComp / abs($methodComp);
231
    }
232
}
233