Passed
Push — master ( 9f53b8...a9876b )
by Iman
01:47
created

DynamicRelations::morphed_by_many()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Imanghafoori\Relativity;
4
5
use Illuminate\Support\Str;
6
7
trait DynamicRelations
8
{
9
    use BaseEloquentOverrides;
10
11
    protected static $dynamicRelations = [];
12
13 6
    public static function defineRelation($name, $macro)
14
    {
15 6
        static::$dynamicRelations[$name] = $macro;
16 6
    }
17
18
    /**
19
     * Dynamically handle calls to the class.
20
     *
21
     * @param  string  $method
22
     * @param  array   $parameters
23
     * @return mixed
24
     *
25
     * @throws \BadMethodCallException
26
     */
27 6
    public function __call($method, $parameters)
28
    {
29 6
        $dynamicRelation = static::$dynamicRelations[$method] ?? null;
30 6
        if (! $dynamicRelation) {
31 6
            return parent::__call($method, $parameters);
32
        }
33
34 6
        return call_user_func_array($dynamicRelation->bindTo($this, static::class), $parameters);
35
    }
36
37 1
    public static function morphed_by_many($relationName, $related, $name, $table = null, $foreignPivotKey = null,
38
        $relatedPivotKey = null, $parentKey = null, $relatedKey = null)
39
    {
40 1
        return new AbstractRelation(['morphedByMany', static::class, $relationName, [$related, $name, $table, $foreignPivotKey,
41 1
            $relatedPivotKey, $parentKey, $relatedKey, $relationName]]);
42
    }
43
44
    /**
45
     * @param string $related
46
     * @param $relationName
47
     * @param null $foreignKey
48
     * @param null $localKey
49
     *
50
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
51
     */
52 2
    public static function has_many($relationName = null, string $related, $foreignKey = null, $localKey = null)
53
    {
54 2
        return new AbstractRelation(['hasMany', static::class, $relationName, [$related, $foreignKey, $localKey]]);
55
    }
56
57 1
    public static function has_one($relationName = null, $related, $foreignKey = null, $localKey = null)
58
    {
59 1
        return new AbstractRelation(['hasOne', static::class, $relationName, [$related, $foreignKey, $localKey]]);
60
    }
61
62
    /**
63
     * @param string $related
64
     * @param $relationName
65
     * @param null $foreignKey
66
     * @param null $ownerKey
67
     *
68
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
69
     */
70 1
    public static function belongs_to($relationName = null, string $related, $foreignKey = null, $ownerKey = null)
71
    {
72 1
        if (is_null($foreignKey)) {
73
            $foreignKey = Str::snake($relationName).'_id';
74
        }
75
76 1
        return new AbstractRelation(['belongsTo', static::class, $relationName, [$related, $foreignKey, $ownerKey, $relationName]]);
77
    }
78
79 1
    public static function belongs_to_many($relationName, $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null,
80
        $parentKey = null, $relatedKey = null)
81
    {
82 1
        $params = [$related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName];
83
84 1
        return new AbstractRelation(['belongsToMany', static::class, $relationName, $params]);
85
    }
86
87
    /**
88
     * Define a polymorphic many-to-many relationship.
89
     *
90
     * @param  string  $relationName
91
     * @param  string  $related
92
     * @param  string  $name
93
     * @param  string  $table
94
     * @param  string  $foreignPivotKey
95
     * @param  string  $relatedPivotKey
96
     * @param  string  $parentKey
97
     * @param  string  $relatedKey
98
     * @param  bool  $inverse
99
     *
100
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
101
     */
102 1
    public static function morph_to_many($relationName, $related, $name, $table = null, $foreignPivotKey = null,
103
        $relatedPivotKey = null, $parentKey = null,
104
        $relatedKey = null, $inverse = false)
105
    {
106 1
        $params = [$related, $name, $table, $foreignPivotKey,
107 1
            $relatedPivotKey, $parentKey, $relatedKey, $inverse, $relationName];
108
109 1
        return new AbstractRelation(['morphToMany', static::class, $relationName, $params]);
110
    }
111
112
    /**
113
     * Define a polymorphic one-to-many relationship.
114
     *
115
     * @param  string  $relationName
116
     * @param  string  $related
117
     * @param  string  $name
118
     * @param  string  $type
119
     * @param  string  $id
120
     * @param  string  $localKey
121
     *
122
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
123
     */
124 1
    public static function morph_many($relationName, $related, $name, $type = null, $id = null, $localKey = null)
125
    {
126 1
        $params = [$related, $name, $type, $id, $localKey];
127
128 1
        return new AbstractRelation(['morphMany', static::class, $relationName, $params]);
129
    }
130
131
    public static function morph_one($relationName, $related, $name, $type = null, $id = null, $localKey = null)
132
    {
133
        $params = [$related, $name, $type, $id, $localKey];
134
        return new AbstractRelation(['morphOne', static::class, $relationName, $params]);
135
    }
136
137
    public static function morph_to($relationName, $type = null, $id = null, $ownerKey = null)
138
    {
139
        $params = [$relationName, $type, $id, $ownerKey];
140
141
        return new AbstractRelation(['morphTo', static::class, $relationName, $params]);
142
    }
143
144
    /**
145
     * Define a has-many-through relationship.
146
     *
147
     * @param string $relationName
148
     * @param string $related
149
     * @param string $through
150
     * @param string|null $firstKey
151
     * @param string|null $secondKey
152
     * @param string|null $localKey
153
     * @param string|null $secondLocalKey
154
     *
155
     * @return \Imanghafoori\Relativity\AbstractRelation
156
     */
157
    public static function has_many_through($relationName, $related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null)
158
    {
159
        $params = [$related, $through, $firstKey, $secondKey, $localKey, $secondLocalKey];
160
161
        return new AbstractRelation(['hasManyThrough', static::class, $relationName, $params]);
162
    }
163
164 1
    public static function forceEagerLoading(...$relation)
165
    {
166
        static::registerModelEvent('booting', function ($model) use ($relation) {
167 1
            $model->with = $model->with + $relation;
168 1
        });
169
    }
170
}