Test Failed
Push — master ( 8cb4c8...40f127 )
by Alexey
04:43
created

ActiveFormController::searchAction()   C

Complexity

Conditions 16
Paths 64

Size

Total Lines 65
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 49
nc 64
nop 0
dl 0
loc 65
rs 5.9197
c 0
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
 * Active form 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 ActiveFormController extends Controller {
12
13
    public function searchAction() {
14
        $result = new Server\Result();
15
        $searchString = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_STRING);
16
        $searchString = trim(preg_replace('![^A-zА-я0-9@-_\. ]!iSu', ' ', urldecode($searchString)));
17
        if (!$searchString) {
18
            $result->content = [];
19
            $result->send();
20
        }
21
        try {
22
            $modelName = trim(filter_input(INPUT_GET, 'modelName', FILTER_SANITIZE_STRING));
23
            if (!$modelName) {
24
                throw new Exception('Не указана модель');
25
            }
26
            $model = new $modelName;
27
            if (!$model || !is_subclass_of($model, 'Model')) {
28
                throw new Exception('Модель не найдена');
29
            }
30
            $formName = trim(filter_input(INPUT_GET, 'formName', FILTER_SANITIZE_STRING));
31
            if (!$formName) {
32
                throw new Exception('Не указано название формы');
33
            }
34
            if (empty($modelName::$forms[$formName])) {
35
                throw new Exception('Не существует указанной формы');
36
            }
37
            $activeForm = new Ui\ActiveForm($model, $formName);
38
            $inputs = $activeForm->getInputs();
39
            $inputName = trim(filter_input(INPUT_GET, 'inputName', FILTER_SANITIZE_STRING));
40
            if (empty($inputs[$inputName])) {
41
                throw new Exception('У формы нет такого поля');
42
            }
43
        } catch (Exception $exc) {
44
            $result->success = false;
45
            $result->content = $exc->getMessage();
46
            return $result->send();
47
        }
48
        $options = [
49
            'where' => [
50
            ]
51
        ];
52
53
        $searchArr = [];
54
        foreach (explode(' ', $searchString) as $part) {
55
            $colWhere = [];
56
            $first = true;
57
            foreach ($inputs[$inputName]['cols'] as $col) {
58
                $part = trim($part);
59
                if ($part && strlen($part) > 2) {
60
                    $colWhere[] = [$col, '%' . $part . '%', 'LIKE', $first ? 'AND' : 'OR'];
61
                    $first = false;
62
                }
63
            }
64
            if ($colWhere) {
65
                $searchArr[] = $colWhere;
66
            }
67
        }
68
        if ($searchArr) {
69
            $options['where'][] = $searchArr;
70
        } else {
71
            $result->content = [];
72
            $result->send();
73
        }
74
        $list = $activeForm->getOptionsList($inputs[$inputName], ['noEmptyValue' => true], $modelName, 'aditional', $options,$model);
0 ignored issues
show
Documentation introduced by
$model is of type object, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
        $result->content = $list;
76
        $result->send();
77
    }
78
79
}
80