Issues (28)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/SortableTrait.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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) {
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);
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();
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';
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
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
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
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()) {
136
            return $this;
137
        }
138
139
        $orderColumnName = $this->determineOrderColumnName();
140
141
        $this->$orderColumnName = $firstModel->$orderColumnName;
142
        $this->save();
0 ignored issues
show
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);
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
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())
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