Completed
Push — master ( 6b7664...8cf912 )
by Alexey
05:42
created

UiController::formPopUpAction()   F

Complexity

Conditions 16
Paths 384

Size

Total Lines 42
Code Lines 34

Duplication

Lines 3
Ratio 7.14 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 16
eloc 34
c 1
b 1
f 0
nc 384
nop 0
dl 3
loc 42
rs 3.7109

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
/**
4
 * Ui controller
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class UiController extends Controller
12
{
13
    public function formPopUpAction()
2 ignored issues
show
Coding Style introduced by
formPopUpAction uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
formPopUpAction uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
14
    {
15
        if (strpos($_GET['item'], ':')) {
16
            $raw = explode(':', $_GET['item']);
17
            $modelName = $raw[0];
18
            $id = $raw[1];
19
            $model = $modelName::get($id, $modelName::index(), !empty($_GET['params']['dataManagerParams']) ? $_GET['params']['dataManagerParams'] : []);
20
        } else {
21
            $modelName = $_GET['item'];
22
            $model = new $modelName();
23
        }
24
        $params = [];
25
        if (!empty($_GET['params'])) {
26
            $params = $_GET['params'];
27
            if (!empty($params['preset'])) {
28
                $model->setParams($params['preset']);
29
            }
30
        }
31 View Code Duplication
        if (!empty($_GET['params']['dataManagerParams']['appType'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
32
            $params['appType'] = $_GET['params']['dataManagerParams']['appType'];
33
        }
34
35
        $formName = !empty($_GET['formName']) ? $_GET['formName'] : (!empty($_GET['params']['formName']) ? $_GET['params']['formName'] : 'manager');
36
        $form = new Ui\ActiveForm($model, $formName);
37
        if (!empty($_GET['_']) || !empty($_POST['_'])) {
38
            $return = new Server\Result();
39
            ob_start();
40
            $form->checkRequest($params, true);
41
            $_GET['item'] = get_class($form->model) . ($model->pk() ? ':' . $model->pk() : '');
42
            $form->action = (App::$cur->system ? '/' . App::$cur->name : '') . '/ui/formPopUp/?' . http_build_query($_GET);
43
            $form->draw($params, true);
44
            $return->content = ob_get_contents();
45
            ob_end_clean();
46
            $return->send();
47
        } else {
48
            $form->checkRequest($params);
49
            $_GET['item'] = get_class($form->model) . ($model->pk() ? ':' . $model->pk() : '');
50
            $form->action = (App::$cur->system ? '/' . App::$cur->name : '') . '/ui/formPopUp/?' . http_build_query($_GET);
51
            $this->view->setTitle(($model && $model->pk() ? 'Изменить ' : 'Создать ') . $form->header);
52
            $this->view->page(['content' => 'form', 'data' => compact('form', 'params')]);
53
        }
54
    }
55
56
    public function fastEditAction()
1 ignored issue
show
Coding Style introduced by
fastEditAction uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
57
    {
58
        $model = $_POST['model']::get($_POST['key']);
59
        if ($model && $model->checkAccess()) {
60
            $model->$_POST['col'] = $_POST['data'];
61
            $model->save();
62
        }
63
    }
64
65
}
66