UiController::formPopUpAction()   F
last analyzed

Complexity

Conditions 20
Paths 3072

Size

Total Lines 55
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
eloc 43
nc 3072
nop 0
dl 0
loc 55
rs 3.5146
c 1
b 0
f 0

How to fix   Long Method    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
12
namespace Inji\Ui;
13
14
use Inji\App;
15
use Inji\Controller;
16
use Inji\Server\Result;
17
18
class UiController extends Controller {
19
20
    public function formPopUpAction() {
21
        $id = false;
22
        if (strpos($_GET['item'], ':')) {
23
            $raw = explode(':', $_GET['item']);
24
            $modelName = $raw[0];
25
            $id = $raw[1];
26
        } else {
27
            $modelName = $_GET['item'];
28
        }
29
        $formName = !empty($_GET['formName']) ? $_GET['formName'] : (!empty($_GET['params']['formName']) ? $_GET['params']['formName'] : 'manager');
30
        $form = ActiveForm::forModel($modelName, $formName);
31
        if ($id) {
32
            $form->loadModelById($id, empty($_GET['params']['dataManagerParams']) ? $_GET['params']['dataManagerParams'] : []);
33
        }
34
        if (!$form->model) {
35
            $form->emptyModel();
36
        }
37
38
        $params = [];
39
        if (!empty($_GET['params'])) {
40
            $params = $_GET['params'];
41
            if (!empty($params['preset'])) {
42
                $model->setParams($params['preset']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $model does not exist. Did you maybe mean $modelName?
Loading history...
43
            }
44
        }
45
        if (!empty($_GET['params']['dataManagerParams']['appType'])) {
46
            $params['appType'] = $_GET['params']['dataManagerParams']['appType'];
47
        }
48
49
50
        if (!empty($_GET['_']) || !empty($_POST['_'])) {
51
            $return = new Result();
52
            ob_start();
53
            $form->checkRequest($params, true);
54
            $_GET['item'] = $form->modelName . ($form->model->pk() ? ':' . $form->model->pk() : '');
55
            $get = $_GET;
56
            if (isset($get['notSave'])) {
57
                unset($get['notSave']);
58
            }
59
            $this->view->widget('msgList');
0 ignored issues
show
Bug introduced by
The method widget() does not exist on Inji\Module. It seems like you code against a sub-type of Inji\Module such as Inji\View or Inji\Db. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
            $this->view->/** @scrutinizer ignore-call */ 
60
                         widget('msgList');
Loading history...
Bug Best Practice introduced by
The property view does not exist on Inji\Ui\UiController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method widget() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
            $this->view->/** @scrutinizer ignore-call */ 
60
                         widget('msgList');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
            $form->action = (App::$cur->system ? '/' . App::$cur->name : '') . '/ui/formPopUp/?' . http_build_query($get);
61
            $form->draw($params, true);
62
            $return->content = ob_get_contents();
63
            ob_end_clean();
64
            $return->send();
65
        } else {
66
            $form->checkRequest($params);
67
            $_GET['item'] = $form->modelName . ($form->model->pk() ? ':' . $form->model->pk() : '');
68
            $get = $_GET;
69
            if (isset($get['notSave'])) {
70
                unset($get['notSave']);
71
            }
72
            $form->action = (App::$cur->system ? '/' . App::$cur->name : '') . '/ui/formPopUp/?' . http_build_query($get);
73
            $this->view->setTitle(($form->model && $form->model->pk() ? 'Изменить ' : 'Создать ') . $form->label);
0 ignored issues
show
Bug introduced by
The method setTitle() does not exist on Inji\Module. It seems like you code against a sub-type of Inji\Module such as Inji\Db. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            $this->view->/** @scrutinizer ignore-call */ 
74
                         setTitle(($form->model && $form->model->pk() ? 'Изменить ' : 'Создать ') . $form->label);
Loading history...
74
            $this->view->page(['content' => 'form', 'data' => compact('form', 'params')]);
0 ignored issues
show
Bug introduced by
The method page() does not exist on Inji\Module. It seems like you code against a sub-type of Inji\Module such as Inji\View or Inji\Db. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
            $this->view->/** @scrutinizer ignore-call */ 
75
                         page(['content' => 'form', 'data' => compact('form', 'params')]);
Loading history...
75
        }
76
    }
77
78
    public function fastEditAction() {
79
        $model = $_POST['model']::get($_POST['key']);
80
        if ($model && $model->checkAccess()) {
81
            $model->{$_POST['col']} = $_POST['data'];
82
            $model->save();
83
        }
84
    }
85
86
    public function autocompleteAction() {
87
        $snippets = $this->module->getSnippets('autocomplete');
88
        if (!is_string($_GET['snippet']) || !isset($snippets[$_GET['snippet']])) {
89
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
90
        }
91
        $snippet = $snippets[$_GET['snippet']];
92
        $result = new \Server\Result();
0 ignored issues
show
Bug introduced by
The type Server\Result was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93
        $result->content = $snippet['find']($_GET['search'], $_GET['snippetParams']);
94
        $result->send();
95
    }
96
}
97