Completed
Push — master ( 88ab05...dae06d )
by Song
02:49
created

BelongsToMany::addScript()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 89

Duplication

Lines 89
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 89
loc 89
rs 8.24
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A BelongsToMany::getOtherKey() 0 20 4
B BelongsToMany::getOriginalData() 0 30 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Encore\Admin\Grid\Displayers;
4
5
use Encore\Admin\Admin;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany as Relation;
7
use Illuminate\Support\Arr;
8
9
class BelongsToMany extends BelongsTo
10
{
11
    /**
12
     * Other key for many-to-many relation.
13
     *
14
     * @var string
15
     */
16
    protected static $otherKey = [];
17
18
    /**
19
     * Get other key for this many-to-many relation.
20
     *
21
     * @throws \Exception
22
     *
23
     * @return string
24
     */
25
    protected function getOtherKey()
26
    {
27
        if (isset(static::$otherKey[$this->getName()])) {
28
            return static::$otherKey[$this->getName()];
29
        }
30
31
        $model = $this->getGrid()->model()->getOriginalModel();
0 ignored issues
show
Bug introduced by
The method getOriginalModel does only exist in Encore\Admin\Grid\Model, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
32
33
        if (is_callable([$model, $this->getName()]) &&
34
            ($relation = $model->{$this->getName()}()) instanceof Relation
35
        ) {
36
            /* @var Relation $relation */
37
            $fullKey = $relation->getQualifiedRelatedPivotKeyName();
38
            $fullKeyArray = explode('.', $fullKey);
39
40
            return static::$otherKey[$this->getName()] = end($fullKeyArray);
41
        }
42
43
        throw new \Exception('Column of this field must be a `BelongsToMany` relation.');
44
    }
45
46
    /**
47
     * @throws \Exception
48
     *
49
     * @return false|string|void
50
     */
51
    protected function getOriginalData()
52
    {
53
        $relations = $this->getColumn()->getOriginal();
54
55
        if (is_string($relations)) {
56
            $data = explode(',', $relations);
57
        }
58
59
        if (!is_array($relations)) {
60
            return;
61
        }
62
63
        $first = current($relations);
64
65
        if (is_null($first)) {
66
            $data = null;
67
68
        // MultipleSelect value store as an ont-to-many relationship.
69
        } elseif (is_array($first)) {
70
            foreach ($relations as $relation) {
71
                $data[] = Arr::get($relation, "pivot.{$this->getOtherKey()}");
0 ignored issues
show
Bug introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
72
            }
73
74
            // MultipleSelect value store as a column.
75
        } else {
76
            $data = $relations;
77
        }
78
79
        return json_encode($data);
80
    }
81
}
82