GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — action_getCatTree_order ( 77944b )
by
unknown
23:56
created

FormController::actionIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %
Metric Value
dl 13
loc 13
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace app\backend\controllers;
4
5
use app\models\Form;
6
use app\models\Object;
7
use app\models\ObjectPropertyGroup;
8
use app\models\PropertyGroup;
9
use app\models\Submission;
10
use app\properties\DynamicSearchModel;
11
use Yii;
12
use yii\db\Query;
13
use yii\filters\AccessControl;
14
use yii\helpers\ArrayHelper;
15
use yii\web\Controller;
16
use yii\web\NotFoundHttpException;
17
use yii\helpers\Url;
18
19
class FormController extends Controller
20
{
21
22
    public function behaviors()
23
    {
24
        return [
25
            'access' => [
26
                'class' => AccessControl::className(),
27
                'rules' => [
28
                    [
29
                        'allow' => true,
30
                        'roles' => ['form manage'],
31
                    ],
32
                ],
33
            ],
34
        ];
35
    }
36
37 View Code Duplication
    public function actionIndex()
0 ignored issues
show
Coding Style introduced by
actionIndex 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...
38
    {
39
        $searchModel = new Form();
40
        $dataProvider = $searchModel->search($_GET);
41
42
        return $this->render(
43
            'index',
44
            [
45
                'dataProvider' => $dataProvider,
46
                'searchModel' => $searchModel,
47
            ]
48
        );
49
    }
50
51
    public function actionEdit($id = null)
52
    {
53
        $model = new Form();
54
        if ($id > 0) {
55
            $model = Form::findOne($id);
56
        }
57
58
        /** @var \app\models\Object $object */
59
        $object = Object::getForClass(Form::className());
60
61
        $propIds = (new Query())->select('property_group_id')->from([ObjectPropertyGroup::tableName()])->where(
62
            [
63
                'and',
64
                'object_id = :object',
65
                'object_model_id = :id'
66
            ],
67
            [
68
                ':object' => $object->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<app\models\Object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
69
                ':id' => $id
70
            ]
71
        )->column();
72
73
        $post = \Yii::$app->request->post();
74
        $properties = isset($post['Form']['properties']) ? $post['Form']['properties'] : [];
75
76
        if ($model->load($post) && $model->validate()) {
77
            if ($model->save()) {
78
                $id = $model->id;
79
                $remove = [];
80
                $add = [];
81
82
                foreach ($propIds as $value) {
83
                    $key = array_search($value, $properties);
84
                    if ($key === false) {
85
                        $remove[] = $value;
86
                    } else {
87
                        unset($properties[$key]);
88
                    }
89
                }
90
                foreach ($properties as $value) {
91
                    $add[] = [
92
                        $value,
93
                        $object->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<app\models\Object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
94
                        $id
95
                    ];
96
                }
97
98
                Yii::$app->db->createCommand()->delete(
99
                    ObjectPropertyGroup::tableName(),
100
                    [
101
                        'and',
102
                        'object_id = :object',
103
                        'object_model_id = :id',
104
                        ['in', 'property_group_id', $remove]
105
                    ],
106
                    [
107
                        ':object' => $object->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<app\models\Object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
108
                        ':id' => $id,
109
                    ]
110
                )->execute();
111
112 View Code Duplication
                if (!empty($add)) {
113
                    Yii::$app->db->createCommand()->batchInsert(
114
                        ObjectPropertyGroup::tableName(),
115
                        ['property_group_id', 'object_id', 'object_model_id'],
116
                        $add
117
                    )->execute();
118
                }
119
120
                \Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved'));
121
                $returnUrl = Yii::$app->request->get('returnUrl', ['/backend/form/index', 'id' => $model->id]);
122
                switch (Yii::$app->request->post('action', 'save')) {
123
                    case 'next':
124
                        return $this->redirect(
125
                            [
126
                                '/backend/form/edit',
127
                                'returnUrl' => $returnUrl,
128
                            ]
129
                        );
130
                    case 'back':
131
                        return $this->redirect($returnUrl);
132
                    default:
133
                        return $this->redirect(
134
                            Url::toRoute(
135
                                [
136
                                    '/backend/form/edit',
137
                                    'id' => $model->id,
138
                                    'returnUrl' => $returnUrl,
139
                                ]
140
                            )
141
                        );
142
                }
143
            } else {
144
                \Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data'));
145
            }
146
        }
147
148
        $items = ArrayHelper::map(
149
            PropertyGroup::find()->where(
150
                'object_id = :object',
151
                [
152
                    ':object' => $object->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<app\models\Object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
153
                ]
154
            )->all(),
155
            'id',
156
            'name'
157
        );
158
159
        return $this->render(
160
            'edit',
161
            [
162
                'model' => $model,
163
                'items' => $items,
164
                'selected' => $propIds,
165
            ]
166
        );
167
    }
168
169
    public function actionView($id, $show_deleted = 0)
170
    {
171
        $form = Form::findById($id);
172
        $propertyGroups = $form->getPropertyGroups();
0 ignored issues
show
Documentation Bug introduced by
The method getPropertyGroups does not exist on object<app\models\Form>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
173
        $submission = new Submission();
174
175
        $dynamicModel = new DynamicSearchModel($submission, $propertyGroups);
176
        $data = $dynamicModel->search(Yii::$app->request->get());
177
        $data->query->andWhere('form_id = :form_id', [':form_id' => $form->id]);
0 ignored issues
show
Unused Code introduced by
The call to QueryInterface::andWhere() has too many arguments starting with array(':form_id' => $form->id).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
178
        $data->query->andWhere(['is_deleted' => $show_deleted]);
179
        $data->query->andFilterWhere(['sending_status' => $dynamicModel->sending_status]);
0 ignored issues
show
Documentation introduced by
The property sending_status does not exist on object<app\properties\DynamicSearchModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
180
        $data->query->andFilterWhere(['like', 'ip', $dynamicModel->ip]);
0 ignored issues
show
Documentation introduced by
The property ip does not exist on object<app\properties\DynamicSearchModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
181
        $data->query->andFilterWhere(['like', 'user_agent', $dynamicModel->user_agent]);
0 ignored issues
show
Documentation introduced by
The property user_agent does not exist on object<app\properties\DynamicSearchModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
182
183
184
        return $this->render(
185
            'view',
186
            [
187
                'searchModel' => $dynamicModel,
188
                'dataProvider' => $data,
189
                'form' => $form,
190
            ]
191
        );
192
    }
193
    
194
    public function actionViewSubmission($id)
195
    {
196
        $submission = Submission::findOne($id);
197
        if ($submission === null) {
198
            throw new NotFoundHttpException('Submission not found');
199
        }
200
201
        return $this->render(
202
            'view-submission',
203
            [
204
                'submission' => $submission,
205
            ]
206
        );
207
    }
208
209
    public function actionDeleteSubmission($id)
210
    {
211
        $submission = Submission::findOne($id);
212
        $form_id = $submission->form_id;
213
        if ($submission === null) {
214
            throw new NotFoundHttpException('Submission not found');
215
        }
216
        $res = false;
217
        if ($submission->is_deleted == 1) {
218
            if ($submission->delete()) {
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $submission (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
219
                $res = true;
220
            }
221
        } else {
222
            $submission->setAttribute('is_deleted', 1);
0 ignored issues
show
Bug introduced by
The method setAttribute cannot be called on $submission (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
223
            if ($submission->save()) {
0 ignored issues
show
Bug introduced by
The method save cannot be called on $submission (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
224
                $res = true;
225
            }
226
        }
227 View Code Duplication
        if ($res === false) {
228
            Yii::$app->session->setFlash('error', Yii::t('app', 'Object not removed'));
229
        } else {
230
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed'));
231
        }
232
        return $this->redirect(Url::toRoute(['view', 'id' => $form_id]));
233
    }
234
235
    public function actionRestoreSubmission($id)
236
    {
237
        $submission = Submission::findOne($id);
238
        if ($submission === null) {
239
            throw new NotFoundHttpException('Submission not found');
240
        }
241
        $submission->setAttribute('is_deleted', 0);
0 ignored issues
show
Bug introduced by
The method setAttribute cannot be called on $submission (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
242
        $submission->save();
0 ignored issues
show
Bug introduced by
The method save cannot be called on $submission (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
243
        Yii::$app->session->setFlash('success', Yii::t('app', 'Object successfully restored'));
244
245
        return $this->redirect(Url::toRoute(['view', 'id' => $submission->form_id]));
246
    }
247
248 View Code Duplication
    public function actionDownload($key, $submissionId)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
249
    {
250
        $submission = Submission::findOne($submissionId);
251
        if ($submission === null) {
252
            throw new NotFoundHttpException('Submission not found');
253
        }
254
        $prop = $submission->getPropertyValuesByKey($key);
0 ignored issues
show
Bug introduced by
The method getPropertyValuesByKey cannot be called on $submission (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
255
        return \Yii::$app->response->sendFile(
256
            Yii::getAlias(Yii::$app->getModule('core')->visitorsFileUploadPath) .
257
            DIRECTORY_SEPARATOR .
258
            $prop->values[0]['value']
259
        );
260
    }
261
262 View Code Duplication
    public function actionDelete($id = null)
263
    {
264
        if ((null === $id) || (null === $model = Form::findOne($id))) {
265
            throw new NotFoundHttpException;
266
        }
267
268
        if (!$model->delete()) {
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
269
            Yii::$app->session->setFlash('error', Yii::t('app', 'Object not removed'));
270
        } else {
271
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed'));
272
        }
273
274
        return $this->redirect(Url::toRoute('index'));
275
    }
276
277
    public function actionRemoveAll()
278
    {
279
        $items = Yii::$app->request->post('items', []);
280
        if (!empty($items)) {
281
            $items = Form::find()->where(['in', 'id', $items])->all();
282
            foreach ($items as $item) {
283
                $item->delete();
284
            }
285
        }
286
287
        return $this->redirect(['index']);
288
    }
289
}
290