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

UiController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 55
Duplicated Lines 5.45 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 3
loc 55
rs 10
wmc 19
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fastEditAction() 0 8 3
F formPopUpAction() 3 42 16

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
/**
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