GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SortableTrait   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 90
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
B getSortableBehavior() 0 15 5
A getSortablePosition() 0 4 1
A moveFirst() 0 4 1
A moveLast() 0 4 1
A moveTo() 0 4 1
A moveBefore() 0 4 1
A moveAfter() 0 4 1
A reorder() 0 4 1
1
<?php
2
/**
3
 * @link https://github.com/paulzi/yii2-sortable
4
 * @copyright Copyright (c) 2015 PaulZi <[email protected]>
5
 * @license MIT (https://github.com/paulzi/yii2-sortable/blob/master/LICENSE)
6
 */
7
8
namespace paulzi\sortable;
9
10
use yii\base\InvalidConfigException;
11
use yii\db\ActiveRecord;
12
13
/**
14
 * Sortable Trait
15
 * @author PaulZi <[email protected]>
16
 */
17
trait SortableTrait
18
{
19
    /**
20
     * @var SortableBehavior
21
     */
22
    private $_sortableBehavior;
23
24
25
    /**
26
     * @throws InvalidConfigException
27
     */
28
    private function getSortableBehavior()
29
    {
30
        if ($this->_sortableBehavior === null) {
31
            /** @var \yii\base\Component|self $this */
32
            foreach ($this->getBehaviors() as $behavior) {
0 ignored issues
show
Bug introduced by
It seems like getBehaviors() 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...
33
                if ($behavior instanceof SortableBehavior) {
34
                    $this->_sortableBehavior = $behavior;
35
                }
36
            }
37
            if ($this->_sortableBehavior === null) {
38
                throw new InvalidConfigException('SortableBehavior is not attached to model');
39
            }
40
        }
41
        return $this->_sortableBehavior;
42
    }
43
44
    /**
45
     * @return integer
46
     */
47
    public function getSortablePosition()
48
    {
49
        return $this->getSortableBehavior()->getSortablePosition();
50
    }
51
52
    /**
53
     * @return $this
54
     */
55
    public function moveFirst()
56
    {
57
        return $this->getSortableBehavior()->moveFirst();
58
    }
59
60
    /**
61
     * @return $this
62
     */
63
    public function moveLast()
64
    {
65
        return $this->getSortableBehavior()->moveLast();
66
    }
67
68
    /**
69
     * @param integer $position
70
     * @param bool $forward Move existing items to forward or backward
71
     * @return $this
72
     */
73
    public function moveTo($position, $forward = true)
74
    {
75
        return $this->getSortableBehavior()->moveTo($position, $forward);
76
    }
77
78
    /**
79
     * @param ActiveRecord $model
80
     * @return $this
81
     */
82
    public function moveBefore($model)
83
    {
84
        return $this->getSortableBehavior()->moveBefore($model);
85
    }
86
87
    /**
88
     * @param ActiveRecord $model
89
     * @return $this
90
     */
91
    public function moveAfter($model)
92
    {
93
        return $this->getSortableBehavior()->moveAfter($model);
94
    }
95
96
    /**
97
     * Reorders items with values of sortAttribute begin from zero.
98
     * @param bool $middle
99
     * @return integer
100
     * @throws \Exception
101
     */
102
    public function reorder($middle = true)
103
    {
104
        return $this->getSortableBehavior()->reorder($middle);
105
    }
106
}