Test Failed
Push — master ( c3cd17...167a1a )
by Iman
13:05
created

DynamicRelations::morphToMany()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5706
c 0
b 0
f 0
cc 7
nc 16
nop 9

1 Method

Rating   Name   Duplication   Size   Complexity  
A DynamicRelations::__call() 0 13 3

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 Closure;
6
use Illuminate\Support\Str;
7
8
trait DynamicRelations
9
{
10
    use BaseEloquentOverrides;
11
12
    protected static $macros = [];
13
14
    public static function macro($name, $macro)
15
    {
16
        static::$macros[$name] = $macro;
17
    }
18
19
    /**
20
     * Dynamically handle calls to the class.
21
     *
22
     * @param  string  $method
23
     * @param  array   $parameters
24
     * @return mixed
25
     *
26
     * @throws \BadMethodCallException
27
     */
28
    public function __call($method, $parameters)
29
    {
30
        $macro = static::$macros[$method] ?? null;
31
        if (! $macro) {
32
            return parent::__call($method, $parameters);
33
        }
34
35
        if ($macro instanceof Closure) {
36
            $macro = $macro->bindTo($this, static::class);
37
        }
38
39
        return call_user_func_array($macro, $parameters);
40
    }
41
42
    /**
43
     * @param string $related
44
     * @param $relationName
45
     * @param null $foreignKey
46
     * @param null $localKey
47
     */
48
    public static function has_many($relationName = null, string $related, $foreignKey = null, $localKey = null)
49
    {
50
        return new AbstractRelation(['hasMany', static::class, $relationName, [$related, $foreignKey, $localKey]]);
51
    }
52
53
    public static function has_one($relationName = null, $related, $foreignKey = null, $localKey = null)
54
    {
55
        return new AbstractRelation(['hasOne', static::class, $relationName, [$related, $foreignKey, $localKey]]);
56
    }
57
58
    /**
59
     * @param string $related
60
     * @param $relationName
61
     * @param null $foreignKey
62
     * @param null $ownerKey
63
     *
64
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
65
     */
66
    public static function belongs_to($relationName = null, string $related, $foreignKey = null, $ownerKey = null)
67
    {
68
        if (is_null($foreignKey)) {
69
            $foreignKey = Str::snake($relationName).'_id';
70
        }
71
72
        return new AbstractRelation(['belongsTo', static::class, $relationName, [$related, $foreignKey, $ownerKey, $relationName]]);
73
    }
74
75 View Code Duplication
    public static function belongs_to_many($relationName, $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
76
        $parentKey = null, $relatedKey = null)
77
    {
78
        $params = [$related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName];
79
80
        return new AbstractRelation(['belongsToMany', static::class, $relationName, $params]);
81
    }
82
83
    /**
84
     * Define a polymorphic many-to-many relationship.
85
     *
86
     * @param  string  $relationName
87
     * @param  string  $related
88
     * @param  string  $name
89
     * @param  string  $table
90
     * @param  string  $foreignPivotKey
91
     * @param  string  $relatedPivotKey
92
     * @param  string  $parentKey
93
     * @param  string  $relatedKey
94
     * @param  bool  $inverse
0 ignored issues
show
Bug introduced by
There is no parameter named $inverse. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
95
     *
96
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
97
     */
98 View Code Duplication
    public function morph_to_many($relationName, $related, $name, $table = null, $foreignPivotKey = null,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
99
        $relatedPivotKey = null, $parentKey = null, $relatedKey = null)
100
    {
101
        $params = [$related, $name, $table, $foreignPivotKey,
102
            $relatedPivotKey, $parentKey, $relatedKey, $relationName];
103
104
        return new AbstractRelation(['morphToMany', static::class, $relationName, $params]);
105
    }
106
107
    /**
108
     * Define a polymorphic one-to-many relationship.
109
     *
110
     * @param  string  $relationName
111
     * @param  string  $related
112
     * @param  string  $name
113
     * @param  string  $type
114
     * @param  string  $id
115
     * @param  string  $localKey
116
     *
117
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
118
     */
119
    public static function morph_many($relationName, $related, $name, $type = null, $id = null, $localKey = null)
120
    {
121
        $params = [$related, $name, $type, $id, $localKey];
122
123
        return new AbstractRelation(['morphMany', static::class, $relationName, $params]);
124
    }
125
126
    public static function morph_one($relationName, $related, $name, $type = null, $id = null, $localKey = null)
127
    {
128
        $params = [$related, $name, $type, $id, $localKey];
129
        return new AbstractRelation(['morphOne', static::class, $relationName, $params]);
130
    }
131
132
    public static function morph_to($relationName, $type = null, $id = null, $ownerKey = null)
133
    {
134
        $params = [$relationName, $type, $id, $ownerKey];
135
136
        return new AbstractRelation(['morphTo', static::class, $relationName, $params]);
137
    }
138
139
    public static function forceEagerLoading(...$relation)
140
    {
141
        static::registerModelEvent('booting', function ($model) use ($relation) {
142
            $model->with = $model->with + $relation;
143
        });
144
    }
145
}