Test Setup Failed
Push — master ( d5d43f...0c1c6f )
by eXeCUT
13:02
created

GridView::_run()   D

Complexity

Conditions 16
Paths 64

Size

Total Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 4.8242
c 0
b 0
f 0
cc 16
nc 64
nop 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
 * User: execut
4
 * Date: 07.07.15
5
 * Time: 11:26
6
 */
7
8
namespace execut\actions\action\adapter;
9
10
11
use execut\actions\action\Adapter;
12
use execut\actions\action\adapter\viewRenderer\DynaGrid;
13
use execut\yii\helpers\Html;
14
use yii\base\Model;
15
use yii\data\ArrayDataProvider;
16
use yii\db\ActiveRecord;
17
use yii\web\Response;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, execut\actions\action\adapter\Response.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use yii\widgets\ActiveForm;
19
20
/**
21
 * Class GridView
22
 * @package execut\actions\action
23
 * @property Model $filter
24
 */
25
class GridView extends \execut\actions\action\adapter\Form
26
{
27
    public $attributes = null;
28
29
    protected $_isValidate = false;
30
31
    public $scenario = ActiveRecord::SCENARIO_DEFAULT;
32
33
    protected $handlers = [];
34
35
    protected function getAttributes() {
36
        $attributes = $this->attributes;
37
        if ($attributes === null) {
38
            $class = $this->model->className();
39
            $attributes = [
40
                'id' => current($class::primaryKey()),
41
                'text' => 'name',
42
            ];
43
        }
44
45
        return $attributes;
46
    }
47
48
    public function setHandlers($handlers) {
49
        foreach ($handlers as $key => $handler) {
50
            if (is_array($handler)) {
51
                $handlers[$key] = \yii::createObject($handler);
52
            }
53
        }
54
55
        $this->handlers = $handlers;
56
    }
57
58
    public function getHandlers() {
59
        return $this->handlers;
60
    }
61
62
    protected function _run() {
63
        /**
64
         * @var ActiveRecord $filter
65
         */
66
        $filter = $this->model;
67
//        if ($filter->getBehavior('relationsSaver')) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
//            $filter->detachBehavior('relationsSaver');
69
//        }
70
71
        if ($this->scenario !== ActiveRecord::SCENARIO_DEFAULT) {
72
            $filter->scenario = $this->scenario;
73
        }
74
75
        parent::_run();
76
77
        $isValid = $filter->validate();
78
        /**
79
         * @var ArrayDataProvider $dataProvider
80
         */
81
        $dataProvider = $filter->search();
82
        if (!$isValid) {
83
            $dataProvider->query->andWhere('false');
84
        }
85
86
        if (!empty($this->actionParams->get['handle'])) {
87
            $handlerKey = $this->actionParams->get['handle'];
88
            $handlers = $this->handlers;
89
            if (!empty($handlers[$handlerKey])) {
90
                $handler = $handlers[$handlerKey];
91
                $handler->dataProvider = $dataProvider;
92
                if (($result = $handler->run()) !== null) {
93
                    return $result;
94
                }
95
            }
96
        }
97
98
        $actionParams = $this->actionParams;
99
        $response = $this->getResponse();
100
        if ($actionParams->isAjax && !$actionParams->isPjax && !$this->isDisableAjax && $dataProvider) {
101
            if (isset($this->actionParams->get['depdrop_parents'])) {
102
                $key = 'output';
103
                $dataProvider->pagination->pageSize = 500;
104
            } else {
105
                $key = 'results';
106
            }
107
108
            $result = [];
109
            foreach ($dataProvider->models as $row) {
110
                $attributes = $this->getAttributes();
111
                $modelAttributes = array_values($attributes);
112
                if ($row instanceof Model) {
0 ignored issues
show
Bug introduced by
The class yii\base\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
113
                    $row = $row->getAttributes($modelAttributes);
114
                }
115
                $res = [];
116
                foreach ($attributes as $targetKey => $attribute) {
117
                    if (is_int($targetKey)) {
118
                        $targetKey = $attribute;
119
                    }
120
121
                    $res[$targetKey] = $row[$attribute];
122
                }
123
124
                $result[] = $res;
125
            }
126
127
            $pagination = $dataProvider->pagination;
128
            $response->content = [
129
                $key => $result,
130
                'pagination' => [
131
                    'more' => $pagination ? ($pagination->page < $pagination->pageCount - 1) : false,
132
                ],
133
            ];
134
135
            $response->format = Response::FORMAT_JSON;
136
        } else {
137
            $response->content = [
138
                'filter' => $filter,
139
                'dataProvider' => $dataProvider,
140
            ];
141
        }
142
143
        return $response;
144
    }
145
146
    public function getDefaultViewRendererConfig()
147
    {
148
        return [
149
            'class' => DynaGrid::className(),
150
//            'title' => $this->model->getModelLabelOld(2),
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
151
            'modelClass' => $this->model->className(),
152
        ];
153
    }
154
}