Issues (124)

src/Traits/Relationships.php (1 issue)

1
<?php
2
3
namespace CodexShaper\DBM\Traits;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
use Illuminate\Database\Eloquent\Relations\HasMany;
8
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
9
use Illuminate\Database\Eloquent\Relations\HasOne;
10
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
11
use Illuminate\Database\Eloquent\Relations\MorphMany;
12
use Illuminate\Database\Eloquent\Relations\MorphOne;
13
use Illuminate\Database\Eloquent\Relations\MorphTo;
14
use Illuminate\Database\Eloquent\Relations\MorphToMany;
15
16
trait Relationships
17
{
18
    /*@var object*/
19
    protected $parent;
20
    /*@var string|object*/
21
    protected $related;
22
    /*@var string|null*/
23
    protected $foreignKey;
24
    /*@var string|null*/
25
    protected $localKey;
26
    /*@var string|null*/
27
    protected $relation;
28
    /*@var string|null*/
29
    protected $pivotTable;
30
    /*@var string|null*/
31
    protected $parentPivotKey;
32
    /*@var string|null*/
33
    protected $relatedPivotKey;
34
    /*@var string|null*/
35
    protected $parentKey;
36
    /*@var string|null*/
37
    protected $relatedKey;
38
    /*@var string*/
39
    protected $through;
40
    /*@var string|null*/
41
    protected $firstKey;
42
    /*@var string|null*/
43
    protected $secondKey;
44
    /*@var string|null*/
45
    protected $secondLocalKey;
46
    /*@var string*/
47
    protected $morphName;
48
    /*@var string|null*/
49
    protected $morphType;
50
    /*@var string|null*/
51
    protected $morphId;
52
    /*@var string|null*/
53
    protected $morphLocalKey;
54
    /*@var string|null*/
55
    protected $inverse;
56
57
    /**
58
     * Set Common Relation.
59
     *
60
     * @param  object  $parent
61
     * @param  string|object  $related
62
     * @param  string|null  $foreignKey
63
     * @param  string|null  $localKey
64
     * @param  string|null  $relation
65
     *
66
     * @return $this
67
     */
68
    public function setCommonRelation($parent, $related, $foreignKey = null, $localKey = null, $relation = null)
69
    {
70
        $this->parent = $parent;
71
        $this->related = $related;
72
        $this->foreignKey = $foreignKey;
73
        $this->localKey = $localKey;
74
        $this->relation = $relation;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Set Many to Many Relation.
81
     *
82
     * @param  object  $parent
83
     * @param  string|object  $related
84
     * @param  string|null  $pivotTable
85
     * @param  string|null  $parentPivotKey
86
     * @param  string|null  $relatedPivotKey
87
     * @param  string|null  $parentKey
88
     * @param  string|null  $relatedKey
89
     * @param  string|null  $relation
90
     *
91
     * @return $this
92
     */
93
    public function setManyToManyRelation(
94
        $parent,
95
        $related,
96
        $pivotTable = null,
97
        $parentPivotKey = null,
98
        $relatedPivotKey = null,
99
        $parentKey = null,
100
        $relatedKey = null,
101
        $relation = null)
102
    {
103
        $this->parent = $parent;
104
        $this->related = $related;
105
        $this->pivotTable = $pivotTable;
106
        $this->parentPivotKey = $parentPivotKey;
107
        $this->relatedPivotKey = $relatedPivotKey;
108
        $this->parentKey = $parentKey;
109
        $this->relatedKey = $relatedKey;
110
        $this->relation = $relation;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Set Has Through Relation.
117
     *
118
     * @param  object  $parent
119
     * @param  string|object  $related
120
     * @param  string  $through
121
     * @param  string|null  $firstKey
122
     * @param  string|null  $secondKey
123
     * @param  string|null  $localKey
124
     * @param  string|null  $secondLocalKey
125
     *
126
     * @return $this
127
     */
128
    public function setHasThroughRelation(
129
        $parent,
130
        $related,
131
        $through,
132
        $firstKey = null,
133
        $secondKey = null,
134
        $localKey = null,
135
        $secondLocalKey = null)
136
    {
137
        $this->parent = $parent;
138
        $this->related = $related;
139
        $this->through = $through;
140
        $this->firstKey = $firstKey;
141
        $this->secondKey = $secondKey;
142
        $this->localKey = $localKey;
143
        $this->secondLocalKey = $secondLocalKey;
144
145
        return $this;
146
    }
147
148
    /**
149
     * Set Morph Relation.
150
     *
151
     * @param  object  $parent
152
     * @param  string|object  $related
153
     * @param  string  $morphName
154
     * @param  string|null  $morphType
155
     * @param  string|null  $morphId
156
     * @param  string|null  $morphLocalKey
157
     *
158
     * @return $this
159
     */
160
    public function setMorphRelation(
161
        $parent,
162
        $related,
163
        $morphName,
164
        $morphType = null,
165
        $morphId = null,
166
        $morphLocalKey = null)
167
    {
168
        $this->parent = $parent;
169
        $this->related = $related;
170
        $this->morphName = $morphName;
171
        $this->morphType = $morphType;
172
        $this->morphId = $morphId;
173
        $this->morphLocalKey = $morphLocalKey;
174
175
        return $this;
176
    }
177
178
    /**
179
     * Set MorphTo Relation.
180
     *
181
     * @param  object  $parent
182
     * @param  string|null  $morphName
183
     * @param  string|null  $morphType
184
     * @param  string|null  $morphId
185
     * @param  string|null  $morphLocalKey
186
     *
187
     * @return $this
188
     */
189
    public function setMorphToRelation(
190
        $parent,
191
        $morphName = null,
192
        $morphType = null,
193
        $morphId = null,
194
        $morphLocalKey = null)
195
    {
196
        $this->parent = $parent;
197
        $this->morphName = $morphName;
198
        $this->morphType = $morphType;
199
        $this->morphId = $morphId;
200
        $this->morphLocalKey = $morphLocalKey;
201
202
        return $this;
203
    }
204
205
    /**
206
     * Set MorphToMany Relation.
207
     *
208
     * @param  object  $parent
209
     * @param  string|object  $related
210
     * @param  string  $morphName
211
     * @param  string|null  $pivotTable
212
     * @param  string|null  $parentPivotKey
213
     * @param  string|null  $relatedPivotKey
214
     * @param  string|null  $parentKey
215
     * @param  string|null  $relatedKey
216
     * @param  string|null  $inverse
217
     *
218
     * @return $this
219
     */
220
    public function setMorphToManyRelation(
221
        $parent,
222
        $related,
223
        $morphName,
224
        $pivotTable = null,
225
        $parentPivotKey = null,
226
        $relatedPivotKey = null,
227
        $parentKey = null,
228
        $relatedKey = null,
229
        $inverse = false)
230
    {
231
        $this->parent = $parent;
232
        $this->related = $related;
233
        $this->morphName = $morphName;
234
        $this->pivotTable = $pivotTable;
235
        $this->parentPivotKey = $parentPivotKey;
236
        $this->relatedPivotKey = $relatedPivotKey;
237
        $this->parentKey = $parentKey;
238
        $this->relatedKey = $relatedKey;
239
        $this->inverse = $inverse;
0 ignored issues
show
Documentation Bug introduced by
It seems like $inverse can also be of type false. However, the property $inverse is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
240
241
        return $this;
242
    }
243
244
    /*
245
     * Relationship.
246
     */
247
    public function has_one(): HasOne
248
    {
249
        return $this->parent->hasOne($this->related, $this->foreignKey, $this->localKey);
250
    }
251
252
    public function has_many(): HasMany
253
    {
254
        return $this->parent->hasMany($this->related, $this->foreignKey, $this->localKey);
255
    }
256
257
    public function belongs_to_many(): BelongsToMany
258
    {
259
        return $this->parent->belongsToMany(
260
            $this->related,
261
            $this->pivotTable,
262
            $this->parentPivotKey,
263
            $this->relatedPivotKey,
264
            $this->parentKey,
265
            $this->relatedKey,
266
            $this->relation
267
        );
268
    }
269
270
    public function belongs_to(): BelongsTo
271
    {
272
        return $this->parent->belongsTo($this->related, $this->foreignKey, $this->localKey, $this->relation);
273
    }
274
275
    public function has_one_through(): HasOneThrough
276
    {
277
        return $this->parent->hasOneThrough(
278
            $this->related,
279
            $this->through,
280
            $this->firstKey,
281
            $this->secondKey,
282
            $this->localKey,
283
            $this->secondLocalKey
284
        );
285
    }
286
287
    public function has_many_through(): HasManyThrough
288
    {
289
        return $this->parent->hasManyThrough(
290
            $this->related,
291
            $this->through,
292
            $this->firstKey,
293
            $this->secondKey,
294
            $this->localKey,
295
            $this->secondLocalKey
296
        );
297
    }
298
299
    public function morph_one(): MorphOne
300
    {
301
        return $this->parent->morphOne(
302
            $this->related,
303
            $this->morphName,
304
            $this->morphType,
305
            $this->morphId,
306
            $this->morphLocalKey
307
        );
308
    }
309
310
    public function morph_to(): MorphTo
311
    {
312
        return $this->parent->morphTo(
313
            $this->morphName,
314
            $this->morphType,
315
            $this->morphId,
316
            $this->morphLocalKey
317
        );
318
    }
319
320
    public function morph_many(): MorphMany
321
    {
322
        return $this->parent->morphMany(
323
            $this->related,
324
            $this->morphName,
325
            $this->morphType,
326
            $this->morphId,
327
            $this->morphLocalKey
328
        );
329
    }
330
331
    public function morph_to_many(): MorphToMany
332
    {
333
        return $this->parent->morphToMany(
334
            $this->related,
335
            $this->morphName,
336
            $this->pivotTable,
337
            $this->parentPivotKey,
338
            $this->relatedPivotKey,
339
            $this->parentKey,
340
            $this->relatedKey,
341
            $this->inverse
342
        );
343
    }
344
345
    public function morphed_by_many(): MorphToMany
346
    {
347
        return $this->parent->morphedByMany(
348
            $this->related,
349
            $this->morphName,
350
            $this->pivotTable,
351
            $this->parentPivotKey,
352
            $this->relatedPivotKey,
353
            $this->parentKey,
354
            $this->relatedKey
355
        );
356
    }
357
}
358