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

AssociationStubBase::setForeignField()   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 1
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 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))) {
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...
143
            return false;
144
        }
145
        $foreignField = $this->foreignField;
146 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...
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
        if ($thisClass !== $otherClass) {
212
            return $thisClass < $otherClass ? -1 : 1;
213
        }
214
        $thisBase = $this->getBaseType();
215
        $otherBase = $other->getBaseType();
216
        if ($thisBase !== $otherBase) {
217
            return $thisBase < $otherBase ? -1 : 1;
218
        }
219
        $thisMethod = $this->getRelationName();
220
        $otherMethod = $other->getRelationName();
221
        $methodComp = strcmp($thisMethod, $otherMethod);
222
        return 0 === $methodComp ? 0 : $methodComp / abs($methodComp);
223
    }
224
}
225