SortableTrait   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 166
Duplicated Lines 18.07 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 1
dl 30
loc 166
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A bootSortableTrait() 0 8 3
A setHighestOrderNumber() 0 6 1
A getHighestOrderNumber() 0 4 1
A scopeOrdered() 0 4 1
A setNewOrder() 0 20 5
A setNewOrderByCustomColumn() 0 4 1
A determineOrderColumnName() 0 4 1
A shouldSortWhenCreating() 0 4 1
A moveOrderDown() 15 15 2
A moveOrderUp() 15 15 2
A swapOrderWithModel() 0 14 1
A swapOrder() 0 4 1
A moveToStart() 0 19 2
A moveToEnd() 0 21 2
A buildSortQuery() 0 4 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 Spatie\EloquentSortable;
4
5
use ArrayAccess;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\SoftDeletingScope;
8
use InvalidArgumentException;
9
10
trait SortableTrait
11
{
12
    public static function bootSortableTrait()
13
    {
14
        static::creating(function ($model) {
0 ignored issues
show
Bug introduced by
The method creating() does not exist on Spatie\EloquentSortable\SortableTrait. Did you maybe mean shouldSortWhenCreating()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
15
            if ($model instanceof Sortable && $model->shouldSortWhenCreating()) {
16
                $model->setHighestOrderNumber();
17
            }
18
        });
19
    }
20
21
    public function setHighestOrderNumber()
22
    {
23
        $orderColumnName = $this->determineOrderColumnName();
24
25
        $this->$orderColumnName = $this->getHighestOrderNumber() + 1;
26
    }
27
28
    public function getHighestOrderNumber(): int
29
    {
30
        return (int) $this->buildSortQuery()->max($this->determineOrderColumnName());
31
    }
32
33
    public function scopeOrdered(Builder $query, string $direction = 'asc')
34
    {
35
        return $query->orderBy($this->determineOrderColumnName(), $direction);
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
36
    }
37
38
    public static function setNewOrder($ids, int $startOrder = 1, string $primaryKeyColumn = null)
39
    {
40
        if (! is_array($ids) && ! $ids instanceof ArrayAccess) {
41
            throw new InvalidArgumentException('You must pass an array or ArrayAccess object to setNewOrder');
42
        }
43
44
        $model = new static;
45
46
        $orderColumnName = $model->determineOrderColumnName();
47
48
        if (is_null($primaryKeyColumn)) {
49
            $primaryKeyColumn = $model->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
50
        }
51
52
        foreach ($ids as $id) {
53
            static::withoutGlobalScope(SoftDeletingScope::class)
54
                ->where($primaryKeyColumn, $id)
55
                ->update([$orderColumnName => $startOrder++]);
56
        }
57
    }
58
59
    public static function setNewOrderByCustomColumn(string $primaryKeyColumn, $ids, int $startOrder = 1)
60
    {
61
        self::setNewOrder($ids, $startOrder, $primaryKeyColumn);
62
    }
63
64
    protected function determineOrderColumnName(): string
65
    {
66
        return $this->sortable['order_column_name'] ?? 'order_column';
0 ignored issues
show
Bug introduced by
The property sortable does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
    }
68
69
    /**
70
     * Determine if the order column should be set when saving a new model instance.
71
     */
72
    public function shouldSortWhenCreating(): bool
73
    {
74
        return $this->sortable['sort_when_creating'] ?? true;
75
    }
76
77 View Code Duplication
    public function moveOrderDown()
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
    {
79
        $orderColumnName = $this->determineOrderColumnName();
80
81
        $swapWithModel = $this->buildSortQuery()->limit(1)
82
            ->ordered()
83
            ->where($orderColumnName, '>', $this->$orderColumnName)
84
            ->first();
85
86
        if (! $swapWithModel) {
87
            return $this;
88
        }
89
90
        return $this->swapOrderWithModel($swapWithModel);
91
    }
92
93 View Code Duplication
    public function moveOrderUp()
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...
94
    {
95
        $orderColumnName = $this->determineOrderColumnName();
96
97
        $swapWithModel = $this->buildSortQuery()->limit(1)
98
            ->ordered('desc')
99
            ->where($orderColumnName, '<', $this->$orderColumnName)
100
            ->first();
101
102
        if (! $swapWithModel) {
103
            return $this;
104
        }
105
106
        return $this->swapOrderWithModel($swapWithModel);
107
    }
108
109
    public function swapOrderWithModel(Sortable $otherModel)
110
    {
111
        $orderColumnName = $this->determineOrderColumnName();
112
113
        $oldOrderOfOtherModel = $otherModel->$orderColumnName;
114
115
        $otherModel->$orderColumnName = $this->$orderColumnName;
116
        $otherModel->save();
117
118
        $this->$orderColumnName = $oldOrderOfOtherModel;
119
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
120
121
        return $this;
122
    }
123
124
    public static function swapOrder(Sortable $model, Sortable $otherModel)
125
    {
126
        $model->swapOrderWithModel($otherModel);
127
    }
128
129
    public function moveToStart()
130
    {
131
        $firstModel = $this->buildSortQuery()->limit(1)
132
            ->ordered()
133
            ->first();
134
135
        if ($firstModel->getKey() === $this->getKey()) {
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
136
            return $this;
137
        }
138
139
        $orderColumnName = $this->determineOrderColumnName();
140
141
        $this->$orderColumnName = $firstModel->$orderColumnName;
142
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
143
144
        $this->buildSortQuery()->where($this->getKeyName(), '!=', $this->getKey())->increment($orderColumnName);
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
145
146
        return $this;
147
    }
148
149
    public function moveToEnd()
150
    {
151
        $maxOrder = $this->getHighestOrderNumber();
152
153
        $orderColumnName = $this->determineOrderColumnName();
154
155
        if ($this->$orderColumnName === $maxOrder) {
156
            return $this;
157
        }
158
159
        $oldOrder = $this->$orderColumnName;
160
161
        $this->$orderColumnName = $maxOrder;
162
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
163
164
        $this->buildSortQuery()->where($this->getKeyName(), '!=', $this->getKey())
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
165
            ->where($orderColumnName, '>', $oldOrder)
166
            ->decrement($orderColumnName);
167
168
        return $this;
169
    }
170
171
    public function buildSortQuery()
172
    {
173
        return static::query();
174
    }
175
}
176