Test Failed
Pull Request — master (#13)
by Rail
10:24
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
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 $dynamicRelations;
30
31 9
    public static function bootDynamicRelations()
32
    {
33 9
        if (!static::$dynamicRelations) {
34 5
            static::$dynamicRelations = new RelationStore();
35
        }
36 9
    }
37
38
    /**
39
     * Dynamically handle calls to the class.
40
     *
41
     * @param  string  $method
42
     * @param  array   $parameters
43
     * @return mixed
44
     *
45
     * @throws \BadMethodCallException
46
     */
47 9
    public function __call($method, $parameters)
48
    {
49
        // Handle internal calls
50 9
        if (in_array($method, static::$methodAllowed)) {
51 9
            $manager = new RelationManager(static::$dynamicRelations);
52
53 9
            return $manager->$method(...array_merge([$this], $parameters));
54
        }
55
56
        // Handle relationships
57 7
        $dynamicRelation = static::$dynamicRelations->get($this, $method);
58
59 7
        if ($dynamicRelation) {
60 6
            return call_user_func_array($dynamicRelation->bindTo($this, static::class), $parameters);
61
        }
62
        
63 7
        return parent::__call($method, $parameters);
64
    }
65
66
    /**
67
     * Convert static call to
68
     *
69
     * @param  string  $method
70
     * @param  array   $parameters
71
     * @return mixed
72
     *
73
     * @throws \BadMethodCallException
74
     */
75 9
    public static function __callStatic($method, $parameters)
76
    {
77 9
        if (in_array($method, static::$methodAllowed)) {
78 9
            $entity = new static;
79 9
            return $entity->$method(...$parameters);
80
        } else {
81 7
            return parent::__callStatic($method, $parameters);
82
        }
83
    }
84
  
85
86 1
    public static function forceEagerLoading(...$relation)
87
    {
88
        static::registerModelEvent('booting', function ($model) use ($relation) {
89
            $model->with = $model->with + $relation;
90 1
        });
91 1
    }
92
}
93