Test Failed
Pull Request — master (#13)
by Rail
02:08
created

DynamicRelations::morph_to_many()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 9
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
trait DynamicRelations
6
{
7
    use BaseEloquentOverrides;
8
9
    protected static $methodAllowed = [
10
        'getDynamicRelations',
11
        'hasDynamicRelation',
12
        'removeRelation',
13
        'defineRelation',
14
        'morphed_by_many',
15
        'has_many',
16
        'has_one',
17
        'belongs_to',
18
        'belongs_to_many',
19
        'morph_to_many',
20
        'morph_many',
21
        'morph_one',
22
        'morph_to',
23
        'has_many_through'
24
    ];
25
26
    /**
27
     * @var RelationStore
28
     */
29
    protected static $relationStore;
30
31
    /**
32
     * retrieve a new instance of the store
33
     *
34
     * @return RelationStore
35
     */
36 6
    public function newRelationStoreClass(): RelationStore
37
    {
38 6
        return new RelationStore();
39
    }
40
41
    /**
42
     * Dynamically handle calls to the class.
43
     *
44
     * @param  string  $method
45
     * @param  array   $parameters
46
     * @return mixed
47
     *
48
     * @throws \BadMethodCallException
49
     */
50 9
    public function __call($method, $parameters)
51
    {
52 9
        if (empty(static::$relationStore)) {
53 6
            static::$relationStore = $this->newRelationStoreClass();
54
        }
55
56
        // Handle internal calls
57 9
        if (in_array($method, static::$methodAllowed)) {
58 9
            $manager = new RelationManager(static::$relationStore);
59
60 9
            return $manager->$method(...array_merge([$this], $parameters));
61
        }
62
63
        // Handle relationships
64 7
        $dynamicRelation = static::$relationStore->get($this, $method);
65
66 7
        if ($dynamicRelation) {
67 6
            return call_user_func_array($dynamicRelation->bindTo($this, static::class), $parameters);
68
        }
69
        
70 7
        return parent::__call($method, $parameters);
71
    }
72
73
    /**
74
     * Convert static call to
75
     *
76
     * @param  string  $method
77
     * @param  array   $parameters
78
     * @return mixed
79
     *
80
     * @throws \BadMethodCallException
81
     */
82 9
    public static function __callStatic($method, $parameters)
83
    {
84 9
        if (in_array($method, static::$methodAllowed)) {
85 9
            $entity = new static;
86 9
            return $entity->$method(...$parameters);
87
        } else {
88 7
            return parent::__callStatic($method, $parameters);
89
        }
90
    }
91
  
92
93 1
    public static function forceEagerLoading(...$relation)
94
    {
95
        static::registerModelEvent('booting', function ($model) use ($relation) {
96
            $model->with = $model->with + $relation;
97 1
        });
98 1
    }
99
}
100