SortAction::run()   C
last analyzed

Complexity

Conditions 13
Paths 36

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 48
ccs 0
cts 42
cp 0
rs 5.0877
c 2
b 0
f 0
cc 13
eloc 32
nc 36
nop 2
crap 182

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
     * Callable function signature: function($model){}, where $model is the current sorted model
32
     * @var array|callable
33
     */
34
    public $redirectUrl = ['index'];
35
36
    /**
37
     * Sort action
38
     * @param int $id model identifier
39
     * @param int|string $position new sort position
40
     * May be set as:
41
     *  top - move model to top of sorting
42
     *  bottom - move model to bottom of sorting
43
     *  u<number> - up to <number> positions
44
     *  d<number> - down to <number> positions
45
     *  -<number> - down to <number> positions
46
     *  <number> - move tto specific position
47
     * @return array|Response
48
     * @throws HttpException
49
     */
50
    public function run($id, $position)
51
    {
52
        if (Yii::$app->request->isAjax) {
53
            Yii::$app->response->format = Response::FORMAT_JSON;
54
        }
55
        if (empty($this->modelClass) || !class_exists($this->modelClass)) {
56
            throw new InvalidParamException('Define model class name for action ' . $this->controller->id . '::' . $this->id);
57
        }
58
        /** @var ActiveRecord $class */
59
        $class = $this->modelClass;
60
        /** @var SortableBehavior|ActiveRecord $model */
61
        $model = $class::findOne($id);
62
        if (!$model) {
63
            throw new HttpException(404, 'Model `' . $this->modelClass . '` to change sort not found');
64
        }
65
        switch ($position) {
66
            case 'top':
67
                // move model to top
68
                $model->moveToTop();
0 ignored issues
show
Bug introduced by
The method moveToTop does only exist in inblank\sortable\SortableBehavior, but not in yii\db\ActiveRecord.

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...
69
                break;
70
            case 'bottom':
71
                // move model to bottom
72
                $model->moveToBottom();
0 ignored issues
show
Bug introduced by
The method moveToBottom does only exist in inblank\sortable\SortableBehavior, but not in yii\db\ActiveRecord.

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...
73
                break;
74
            default:
75
                if (strpos('ud-', $position[0]) !== false) {
76
                    // relative position
77
                    $position = ($position[0] == 'u' ? '' : '-') . substr($position, 1);
78
                    if (is_numeric($position)) {
79
                        $model->sortChange($position);
0 ignored issues
show
Bug introduced by
The method sortChange does only exist in inblank\sortable\SortableBehavior, but not in yii\db\ActiveRecord.

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...
80
                    }
81
                } elseif (is_numeric($position)) {
82
                    // absolute position
83
                    $model->moveToPosition($position);
0 ignored issues
show
Bug introduced by
The method moveToPosition does only exist in inblank\sortable\SortableBehavior, but not in yii\db\ActiveRecord.

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...
84
                }
85
                break;
86
        }
87
        $sortField = $model->sortAttribute;
88
        return Yii::$app->response->format == Response::FORMAT_JSON ?
89
            [
90
                'status' => 200,
91
                'id' => $model->id,
92
                $sortField => $model->$sortField
93
            ] : $this->controller->redirect(
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...
94
                is_callable($this->redirectUrl)
95
                    ? call_user_func($this->redirectUrl, $model) : $this->redirectUrl
96
            );
97
    }
98
}
99