Completed
Push — master ( df4deb...f531b1 )
by Pavel
02:22
created

SortAction::run()   C

Complexity

Conditions 11
Paths 18

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 38
ccs 0
cts 32
cp 0
rs 5.2653
cc 11
eloc 25
nc 18
nop 2
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Controller's action to sort change in inblank\yii2-sortable behavior
4
 *
5
 * @link https://github.com/inblank/yii2-sortable
6
 * @copyright Copyright (c) 2016 Pavel Aleksandrov <[email protected]>
7
 * @license http://opensource.org/licenses/MIT
8
 */
9
namespace inblank\sortable;
10
11
use Yii;
12
use yii\base\Action;
13
use yii\base\InvalidParamException;
14
use yii\db\ActiveRecord;
15
use yii\web\HttpException;
16
use yii\web\Response;
17
18
/**
19
 * SortAction
20
 */
21
class SortAction extends Action
22
{
23
    /**
24
     * Sorting model class name
25
     * @var string
26
     */
27
    public $modelClass;
28
29
    /**
30
     * Redirect URL if request not by ajax
31
     * @var array
32
     */
33
    public $redirectUrl = ['index'];
34
35
    /**
36
     * Sort action
37
     * @param int $id model identifier
38
     * @param int|string $position new sort position
39
     * May be set as:
40
     *  top - move model to top of sorting
41
     *  bottom - move model to bottom of sorting
42
     *  u<number> - up to <number> positions
43
     *  d<number> - down to <number> positions
44
     *  -<number> - down to <number> positions
45
     *  <number> - move tto specific position
46
     * @return array|Response
47
     * @throws HttpException
48
     */
49
    public function run($id, $position)
50
    {
51
        if (empty($this->modelClass) || !class_exists($this->modelClass)) {
52
            throw new InvalidParamException('Define model class name for action ' . $this->controller->id . '::' . $this->id);
53
        }
54
        /** @var ActiveRecord $class */
55
        $class = $this->modelClass;
56
        /** @var SortableBehavior $model */
57
        $model = $class::findOne($id);
58
        if (!$model) {
59
            throw new HttpException(404, 'Model `' . $this->modelClass . '` to change sort not found');
60
        }
61
        switch ($position) {
62
            case 'top':
63
                // move model to top
64
                $model->moveToTop();
65
                break;
66
            case 'bottom':
67
                // move model to bottom
68
                $model->moveToBottom();
69
                break;
70
            default:
71
                if (strpos('ud-', $position[0]) !== false) {
72
                    // relative position
73
                    $position = ($position[0] == 'd' ? '-' : '') . substr($position, 1);
74
                    if (is_numeric($position)) {
75
                        $model->sortChange($position);
76
                    }
77
                } elseif (is_numeric($position)) {
78
                    // absolute position
79
                    $model->moveToPosition($position);
80
                }
81
                break;
82
        }
83
        return Yii::$app->response->format == Response::FORMAT_JSON ?
84
            ['status' => 200] :
85
            $this->controller->redirect($this->redirectUrl);
0 ignored issues
show
Bug introduced by
The method redirect does only exist in yii\web\Controller, but not in yii\base\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
86
    }
87
}
88