Passed
Push — master ( d98b51...14cd9e )
by Iman
01:49
created

DynamicRelations::has_many()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
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 4
    public static function macro($name, $macro)
15
    {
16 4
        static::$macros[$name] = $macro;
17 4
    }
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 4
    public function __call($method, $parameters)
29
    {
30 4
        $macro = static::$macros[$method] ?? null;
31 4
        if (! $macro) {
32 4
            return parent::__call($method, $parameters);
33
        }
34
35 4
        if ($macro instanceof Closure) {
36 4
            $macro = $macro->bindTo($this, static::class);
37
        }
38
39 4
        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
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
49
     */
50 1
    public static function has_many($relationName = null, string $related, $foreignKey = null, $localKey = null)
51
    {
52 1
        return new AbstractRelation(['hasMany', static::class, $relationName, [$related, $foreignKey, $localKey]]);
53
    }
54
55 1
    public static function has_one($relationName = null, $related, $foreignKey = null, $localKey = null)
56
    {
57 1
        return new AbstractRelation(['hasOne', static::class, $relationName, [$related, $foreignKey, $localKey]]);
58
    }
59
60
    /**
61
     * @param string $related
62
     * @param $relationName
63
     * @param null $foreignKey
64
     * @param null $ownerKey
65
     *
66
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
67
     */
68 1
    public static function belongs_to($relationName = null, string $related, $foreignKey = null, $ownerKey = null)
69
    {
70 1
        if (is_null($foreignKey)) {
71 1
            $foreignKey = Str::snake($relationName).'_id';
72
        }
73
74 1
        return new AbstractRelation(['belongsTo', static::class, $relationName, [$related, $foreignKey, $ownerKey, $relationName]]);
75
    }
76
77 1 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...
78
        $parentKey = null, $relatedKey = null)
79
    {
80 1
        $params = [$related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName];
81
82 1
        return new AbstractRelation(['belongsToMany', static::class, $relationName, $params]);
83
    }
84
85
    /**
86
     * Define a polymorphic many-to-many relationship.
87
     *
88
     * @param  string  $relationName
89
     * @param  string  $related
90
     * @param  string  $name
91
     * @param  string  $table
92
     * @param  string  $foreignPivotKey
93
     * @param  string  $relatedPivotKey
94
     * @param  string  $parentKey
95
     * @param  string  $relatedKey
96
     * @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...
97
     *
98
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
99
     */
100 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...
101
        $relatedPivotKey = null, $parentKey = null, $relatedKey = null)
102
    {
103
        $params = [$related, $name, $table, $foreignPivotKey,
104
            $relatedPivotKey, $parentKey, $relatedKey, $relationName];
105
106
        return new AbstractRelation(['morphToMany', static::class, $relationName, $params]);
107
    }
108
109
    /**
110
     * Define a polymorphic one-to-many relationship.
111
     *
112
     * @param  string  $relationName
113
     * @param  string  $related
114
     * @param  string  $name
115
     * @param  string  $type
116
     * @param  string  $id
117
     * @param  string  $localKey
118
     *
119
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
120
     */
121 1
    public static function morph_many($relationName, $related, $name, $type = null, $id = null, $localKey = null)
122
    {
123 1
        $params = [$related, $name, $type, $id, $localKey];
124
125 1
        return new AbstractRelation(['morphMany', static::class, $relationName, $params]);
126
    }
127
128
    public static function morph_one($relationName, $related, $name, $type = null, $id = null, $localKey = null)
129
    {
130
        $params = [$related, $name, $type, $id, $localKey];
131
        return new AbstractRelation(['morphOne', static::class, $relationName, $params]);
132
    }
133
134
    public static function morph_to($relationName, $type = null, $id = null, $ownerKey = null)
135
    {
136
        $params = [$relationName, $type, $id, $ownerKey];
137
138
        return new AbstractRelation(['morphTo', static::class, $relationName, $params]);
139
    }
140
141
    public static function forceEagerLoading(...$relation)
142
    {
143
        static::registerModelEvent('booting', function ($model) use ($relation) {
144
            $model->with = $model->with + $relation;
145
        });
146
    }
147
}