Move   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 47.06 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 8
c 3
b 2
f 1
lcom 1
cbo 3
dl 40
loc 85
ccs 34
cts 34
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 8 2
A moveDown() 20 20 3
A moveUp() 20 20 3

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
 * @link https://github.com/roboapp
4
 * @copyright Copyright (c) 2016 Roboapp
5
 * @license [MIT License](https://opensource.org/licenses/MIT)
6
 */
7
8
namespace roboapp\crud;
9
10
/**
11
 * Action switches custom sorting position of the existing record.
12
 *
13
 * @author iRipVanWinkle <[email protected]>
14
 * @since 1.0
15
 */
16
class Move extends BaseAction
17
{
18
    const UP = 1;
19
    const DOWN = 2;
20
21
    /**
22
     * Attribute in model
23
     * @var string
24
     */
25
    public $positionAttribute = 'position';
26
27
    /**
28
     * Direction to move
29
     * @var integer
30
     */
31
    public $direction;
32
33
    /**
34
     * Move model
35
     * @param mixed $id ID of the model to be moved
36
     * @return mixed
37
     */
38 6
    public function run($id)
39
    {
40 6
        if ((int)$this->direction === self::UP) {
41 3
            return $this->moveUp($id);
42
        } else {
43 3
            return $this->moveDown($id);
44
        }
45
    }
46
47
    /**
48
     * Move model to down
49
     * @param mixed $id ID of the model to be moved down
50
     * @return mixed
51
     * @throws \yii\web\NotFoundHttpException
52
     */
53 3 View Code Duplication
    protected function moveDown($id)
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...
54
    {
55 3
        $model = $this->getModel($id);
56 3
        $orderAttrName = $this->positionAttribute;
57 3
        $orderDir = SORT_ASC;
58
59 3
        $swapModel = $model::find()
60 3
            ->where(['>', $orderAttrName, $model->$orderAttrName])
61 3
            ->orderBy([$orderAttrName => $orderDir])
62 3
            ->one();
63
64 3
        if ($swapModel) {
65 3
            $newOrderNumeric = $swapModel->$orderAttrName;
66 3
            $swapModel->$orderAttrName = $model->$orderAttrName;
67 3
            $model->$orderAttrName = $newOrderNumeric;
68 3
            $swapModel->save() && $model->save();
69 3
        }
70
71 3
        return $this->redirect($model);
72
    }
73
74
    /**
75
     * Move model to up
76
     * @param mixed $id ID of the model to be moved up
77
     * @return mixed
78
     * @throws \yii\web\NotFoundHttpException
79
     */
80 3 View Code Duplication
    protected function moveUp($id)
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...
81
    {
82 3
        $model = $this->getModel($id);
83 3
        $orderAttrName = $this->positionAttribute;
84 3
        $orderDir = SORT_DESC;
85
86 3
        $swapModel = $model::find()
87 3
            ->where(['<', $orderAttrName, $model->$orderAttrName])
88 3
            ->orderBy([$orderAttrName => $orderDir])
89 3
            ->one();
90
91 3
        if ($swapModel) {
92 3
            $newOrderNumeric = $swapModel->$orderAttrName;
93 3
            $swapModel->$orderAttrName = $model->$orderAttrName;
94 3
            $model->$orderAttrName = $newOrderNumeric;
95 3
            $swapModel->save() && $model->save();
96 3
        }
97
98 3
        return $this->redirect($model);
99
    }
100
}
101