Completed
Pull Request — master (#2254)
by
unknown
12:23 queued 09:50
created

MultipleSelect   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 80
Duplicated Lines 45 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 36
loc 80
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOtherKey() 0 18 4
A fill() 18 18 5
A setOriginal() 18 18 5
A prepare() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
7
class MultipleSelect extends Select
8
{
9
    /**
10
     * Other key for many-to-many relation.
11
     *
12
     * @var string
13
     */
14
    protected $otherKey;
15
16
    /**
17
     * Get other key for this many-to-many relation.
18
     *
19
     * @throws \Exception
20
     *
21
     * @return string
22
     */
23
    protected function getOtherKey()
24
    {
25
        if ($this->otherKey) {
26
            return $this->otherKey;
27
        }
28
29
        if (is_callable([$this->form->model(), $this->column]) &&
30
            ($relation = $this->form->model()->{$this->column}()) instanceof BelongsToMany
31
        ) {
32
            /* @var BelongsToMany $relation */
33
            $fullKey = $relation->getQualifiedRelatedPivotKeyName();
34
            $fullKeyArray = explode('.', $fullKey);
35
36
            return $this->otherKey = end($fullKeyArray);
37
        }
38
39
        throw new \Exception('Column of this field must be a `BelongsToMany` relation.');
40
    }
41
42 View Code Duplication
    public function fill($data)
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...
43
    {
44
        $relations = array_get($data, $this->column);
0 ignored issues
show
Bug introduced by
It seems like $this->column can also be of type array; however, array_get() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
45
46
        if (is_string($relations)) {
47
            $this->value = explode(',', $relations);
48
        }
49
50
        if (is_array($relations)) {
51
            if (is_string(current($relations))) {
52
                $this->value = $relations;
53
            } else {
54
                foreach ($relations as $relation) {
55
                    $this->value[] = array_get($relation, "pivot.{$this->getOtherKey()}");
56
                }
57
            }
58
        }
59
    }
60
61 View Code Duplication
    public function setOriginal($data)
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...
62
    {
63
        $relations = array_get($data, $this->column);
0 ignored issues
show
Bug introduced by
It seems like $this->column can also be of type array; however, array_get() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
64
65
        if (is_string($relations)) {
66
            $this->original = explode(',', $relations);
67
        }
68
69
        if (is_array($relations)) {
70
            if (is_string(current($relations))) {
71
                $this->original = $relations;
72
            } else {
73
                foreach ($relations as $relation) {
74
                    $this->original[] = array_get($relation, "pivot.{$this->getOtherKey()}");
75
                }
76
            }
77
        }
78
    }
79
80
    public function prepare($value)
81
    {
82
        $value = (array) $value;
83
84
        return array_filter($value);
85
    }
86
}
87