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.
Passed
Push — master ( c721cc...3ff35f )
by Ivan
09:59
created

Property::rules()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 0
1
<?php
2
namespace app\models;
3
4
use app\modules\shop\models\FilterSets;
5
use app\properties\HasProperties;
6
use app\properties\PropertyHandlers;
7
use app\traits\GetImages;
8
use Yii;
9
use yii\behaviors\AttributeBehavior;
10
use yii\caching\TagDependency;
11
use yii\data\ActiveDataProvider;
12
use yii\db\ActiveRecord;
13
use yii\helpers\Json;
14
use \devgroup\TagDependencyHelper\ActiveRecordHelper;
15
16
17
/**
18
 * This is the model class for table "{{%property}}".
19
 * @property integer $id
20
 * @property integer $property_group_id
21
 * @property string $name
22
 * @property string $key
23
 * @property string $value_type
24
 * @property integer $property_handler_id
25
 * @property integer $has_static_values
26
 * @property integer $has_slugs_in_values
27
 * @property integer $is_eav
28
 * @property integer $is_column_type_stored
29
 * @property integer $multiple
30
 * @property integer $sort_order
31
 * @property string $handler_additional_params
32
 * @property integer $display_only_on_depended_property_selected
33
 * @property integer $depends_on_property_id
34
 * @property string $depended_property_values
35
 * @property integer $depends_on_category_group_id
36
 * @property boolean $dont_filter
37
 * @property integer $required
38
 * @property integer $interpret_as
39
 * @property integer $captcha
40
 * @property string $mask
41
 * @property integer $alias
42
 * @property PropertyGroup $group
43
 */
44
class Property extends ActiveRecord
45
{
46
    use GetImages;
47
48
    public static $identity_map = [];
49
    public static $group_id_to_property_ids = [];
50
    private $handlerAdditionalParams = [];
51
    public $required;
52
    public $interpret_as;
53
    public $captcha;
54
55
    /**
56
     * @inheritdoc
57
     */
58 View Code Duplication
    public function behaviors()
59
    {
60
        return [
61
            [
62
                'class' => AttributeBehavior::className(),
63
                'attributes' => [
64
                    ActiveRecord::EVENT_BEFORE_INSERT => 'sort_order',
65
                ],
66
                'value' => 0,
67
            ],
68
            [
69
                'class' => ActiveRecordHelper::className(),
70
            ],
71
            [
72
                'class' => HasProperties::className(),
73
            ],
74
        ];
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public static function tableName()
81
    {
82
        return '{{%property}}';
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function rules()
89
    {
90
        return [
91
            [['property_group_id', 'name', 'property_handler_id', 'handler_additional_params', 'key'], 'required'],
92
            [
93
                [
94
                    'property_group_id',
95
                    'property_handler_id',
96
                    'has_static_values',
97
                    'has_slugs_in_values',
98
                    'is_eav',
99
                    'is_column_type_stored',
100
                    'multiple',
101
                    'sort_order',
102
                    'alias',
103
                ],
104
                'integer'
105
            ],
106
            [
107
                [
108
                    'display_only_on_depended_property_selected',
109
                    'depends_on_property_id',
110
                    'depends_on_category_group_id',
111
                    'hide_other_values_if_selected'
112
                ],
113
                'integer'
114
            ],
115
            [['interpret_as'], 'string'],
116
            [['name', 'handler_additional_params', 'depended_property_values', 'value_type', 'mask'], 'string'],
117
            [['key'], 'string', 'max' => 20],
118
            [['key'], 'match', 'pattern' => '#^[\w]+$#'],
119
            [['depends_on_property_id', 'depends_on_category_group_id'], 'default', 'value' => 0],
120
            [['required', 'captcha'], 'integer', 'min' => 0, 'max' => 1],
121
            [['dont_filter'], 'safe'],
122
            [['key'], 'unique', 'targetAttribute' => ['key', 'property_group_id']],
123
        ];
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129
    public function attributeLabels()
130
    {
131
        return [
132
            'id' => Yii::t('app', 'ID'),
133
            'property_group_id' => Yii::t('app', 'Property Group ID'),
134
            'name' => Yii::t('app', 'Name'),
135
            'key' => Yii::t('app', 'Key'),
136
            'value_type' => Yii::t('app', 'Value Type'),
137
            'property_handler_id' => Yii::t('app', 'Property Handler ID'),
138
            'has_static_values' => Yii::t('app', 'Has Static Values'),
139
            'has_slugs_in_values' => Yii::t('app', 'Has Slugs In Values'),
140
            'is_eav' => Yii::t('app', 'Is Eav'),
141
            'is_column_type_stored' => Yii::t('app', 'Is Column Type Stored'),
142
            'multiple' => Yii::t('app', 'Multiple'),
143
            'sort_order' => Yii::t('app', 'Sort Order'),
144
            'handler_additional_params' => Yii::t('app', 'Handler Additional Params'),
145
            'required' => Yii::t('app', 'Required'),
146
            'interpret_as' => Yii::t('app', 'Interpret Field As'),
147
            'captcha' => Yii::t('app', 'Captcha'),
148
            'dont_filter' => Yii::t('app', 'Don\'t use in filtration'),
149
            'hide_other_values_if_selected' => Yii::t('app', 'Hide Other Values If Selected'),
150
            'display_only_on_depended_property_selected' => Yii::t('app', 'Display Only On Depended Property Selected'),
151
            'depends_on_property_id' => Yii::t('app', 'Depends On Property Id'),
152
            'depended_property_values' => Yii::t('app', 'Depended Property Values'),
153
            'depends_on_category_group_id' => Yii::t('app', 'Depends On Category Group Id'),
154
            'mask' => Yii::t('app', 'Mask'),
155
            'alias' => Yii::t('app', 'Alias'),
156
        ];
157
    }
158
159
    /**
160
     * Search tasks
161
     * @param $params
162
     * @return ActiveDataProvider
163
     */
164
    public function search($params)
165
    {
166
        /* @var $query \yii\db\ActiveQuery */
167
        $query = static::find()->where(['property_group_id' => $this->property_group_id]);
168
        $dataProvider = new ActiveDataProvider([
169
            'query' => $query,
170
            'pagination' => [
171
                'pageSize' => 10,
172
            ],
173
        ]);
174
        if (!($this->load($params))) {
175
            return $dataProvider;
176
        }
177
        $query->andFilterWhere(['id' => $this->id]);
178
        $query->andFilterWhere(['like', 'name', $this->name]);
179
        $query->andFilterWhere(['like', 'key', $this->key]);
180
        $query->andFilterWhere(['property_handler_id' => $this->property_handler_id]);
181
        $query->andFilterWhere(['has_static_values' => $this->has_static_values]);
182
        $query->andFilterWhere(['has_slugs_in_values' => $this->has_slugs_in_values]);
183
        $query->andFilterWhere(['is_eav' => $this->is_eav]);
184
        $query->andFilterWhere(['is_column_type_stored' => $this->is_column_type_stored]);
185
        $query->andFilterWhere(['multiple' => $this->multiple]);
186
        return $dataProvider;
187
    }
188
189
    /**
190
     * @return \yii\db\ActiveQuery
191
     */
192
    public function getGroup()
193
    {
194
        return $this->hasOne(PropertyGroup::className(), ['id' => 'property_group_id']);
195
    }
196
197
    /**
198
     * @return PropertyHandler
199
     */
200
    public function getHandler()
201
    {
202
//        return $this->hasOne(PropertyHandler::className(), ['id' => 'property_handler_id']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
203
        return PropertyHandler::findById($this->property_handler_id);
204
    }
205
206
    /**
207
     * Возвращает модель по ID с использованием IdentityMap
208
     * @param int $id
209
     * @return null|Property
210
     */
211 View Code Duplication
    public static function findById($id)
212
    {
213
        if (!isset(static::$identity_map[$id])) {
214
            $cacheKey = "Property:$id";
215
            if (false === $prop = Yii::$app->cache->get($cacheKey)) {
216
                if (null === $prop = static::findOne($id)) {
217
                    return null;
218
                }
219
                Yii::$app->cache->set(
220
                    $cacheKey,
221
                    $prop,
222
                    0,
223
                    new TagDependency([
224
                        'tags' => [
225
                            ActiveRecordHelper::getObjectTag($prop, $id)
226
                        ],
227
                    ])
228
                );
229
            }
230
            static::$identity_map[$id] = $prop;
231
        }
232
        return static::$identity_map[$id];
233
    }
234
235
    /**
236
     * @param $group_id
237
     * @return null|Property[]
238
     */
239
    public static function getForGroupId($group_id)
240
    {
241
        if (!isset(static::$group_id_to_property_ids[$group_id])) {
242
            $cacheKey = "PropsForGroup:$group_id";
243
            if (false === $props = Yii::$app->cache->get($cacheKey)) {
244
                if (null !== $props = static::find()->where(['property_group_id' => $group_id])->orderBy(
245
                        'sort_order'
246
                    )->all()
247
                ) {
248
                    Yii::$app->cache->set(
249
                        $cacheKey,
250
                        $props,
251
                        0,
252
                        new TagDependency([
253
                            'tags' => [
254
                                ActiveRecordHelper::getObjectTag(
255
                                    PropertyGroup::className(),
256
                                    $group_id
257
                                )
258
                            ],
259
                        ])
260
                    );
261
                }
262
            }
263
            static::$group_id_to_property_ids[$group_id] = [];
264
            foreach ($props as $property) {
265
                static::$identity_map[$property->id] = $property;
266
                static::$group_id_to_property_ids[$group_id][] = $property->id;
267
            }
268
            return $props;
269
        }
270
        $properties = [];
271
        foreach (static::$group_id_to_property_ids[$group_id] as $property_id) {
272
            $properties[] = static::findById($property_id);
273
        }
274
        return $properties;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $properties; (array) is incompatible with the return type documented by app\models\Property::getForGroupId of type null|app\models\Property[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
275
    }
276
277
    /**
278
     * @param $form
279
     * @param $model
280
     * @param $values
281
     * @param string $renderType
282
     * @return string
283
     */
284
    public function handler($form, $model, $values, $renderType = 'frontend_render_view')
285
    {
286
        $handler = $this->handler;
0 ignored issues
show
Bug introduced by
The property handler does not seem to exist. Did you mean handlerAdditionalParams?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
287
        if (null === $handler) {
288
            return '';
289
        }
290
        $handler = PropertyHandlers::createHandler($handler);
291
        if (null === $handler) {
292
            return '';
293
        }
294
        return $handler->render($this, $model, $values, $form, $renderType);
295
    }
296
297
    /**
298
     * @inheritdoc
299
     */
300
    public function afterFind()
301
    {
302
        parent::afterFind();
303
        $this->handlerAdditionalParams = Json::decode($this->handler_additional_params);
0 ignored issues
show
Documentation Bug introduced by
It seems like \yii\helpers\Json::decod...dler_additional_params) of type * is incompatible with the declared type array of property $handlerAdditionalParams.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
304
        $this->required = isset($this->handlerAdditionalParams['rules']) && is_array(
305
                $this->handlerAdditionalParams['rules']
306
            ) && in_array('required', $this->handlerAdditionalParams['rules']);
307
        $this->interpret_as = isset($this->handlerAdditionalParams['interpret_as']) ? $this->handlerAdditionalParams['interpret_as'] : 0;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
308
        if (isset($this->handlerAdditionalParams['rules']) && is_array($this->handlerAdditionalParams['rules'])) {
309
            foreach ($this->handlerAdditionalParams['rules'] as $rule) {
310
                if (is_array($rule)) {
311
                    if (in_array('captcha', $rule, true)) {
312
                        $this->captcha = true;
313
                    }
314
                } else {
315
                    switch ($rule) {
316
                        case 'required':
317
                            $this->required = true;
318
                            break;
319
                    }
320
                }
321
            }
322
        }
323
    }
324
325
    /**
326
     * @inheritdoc
327
     * @param bool $insert
328
     * @return bool
329
     */
330
    public function beforeSave($insert)
331
    {
332
        if (!parent::beforeSave($insert)) {
333
            return false;
334
        }
335
        $handlerAdditionalParams = $this->isNewRecord ? [] : Json::decode($this->handler_additional_params);
336
        $handlerRules = [];
337
        if (1 === intval($this->required)) {
338
            $handlerRules[] = 'required';
339
        }
340
        if (PropertyHandler::findByName('File') === intval($this->property_handler_id)) {
341
            if (1 === intval($this->multiple)) {
342
                $handlerRules[] = ['file', 'maxFiles' => 0];
343
            } else {
344
                $handlerRules[] = ['file', 'maxFiles' => 1];
345
            }
346
        }
347
        if (1 === intval($this->captcha)) {
348
            $handlerRules[] = ['captcha', 'captchaAction' => '/default/captcha'];
349
        }
350
        $handlerAdditionalParams['interpret_as'] = $this->interpret_as;
351
        $handlerAdditionalParams['rules'] = $handlerRules;
352
        $this->handlerAdditionalParams = $handlerAdditionalParams;
0 ignored issues
show
Documentation Bug introduced by
It seems like $handlerAdditionalParams of type * is incompatible with the declared type array of property $handlerAdditionalParams.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
353
        $this->handler_additional_params = Json::encode($handlerAdditionalParams);
354
        return true;
355
    }
356
357
    /**
358
     *
359
     */
360
    public function invalidateModelCache()
361
    {
362
        TagDependency::invalidate(
363
            Yii::$app->cache,
364
            [
365
                ActiveRecordHelper::getObjectTag(
366
                    PropertyGroup::className(),
367
                    $this->property_group_id
368
                ),
369
                ActiveRecordHelper::getObjectTag(Property::className(), $this->id)
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
370
            ]
371
        );
372
    }
373
374
    /**
375
     * @param bool $insert
376
     * @param array $changedAttributes
377
     */
378
    public function afterSave($insert, $changedAttributes)
379
    {
380
        // @todo clear table schema
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
381
        $this->invalidateModelCache();
382
        parent::afterSave($insert, $changedAttributes);
383
    }
384
385
    /**
386
     * @return bool
387
     */
388
    public function beforeDelete()
389
    {
390
        $this->invalidateModelCache();
391
        return parent::beforeDelete();
392
    }
393
394
    public function afterDelete()
395
    {
396
        $object = Object::findById($this->group->object_id);
397
        $staticValues = PropertyStaticValues::find()->where(['property_id' => $this->id])->all();
398
        foreach ($staticValues as $psv) {
399
            $psv->delete();
400
        }
401
        if (null !== $object) {
402
            if ($this->is_eav) {
403
                Yii::$app->db->createCommand()->delete(
404
                    $object->eav_table_name,
405
                    ['key' => $this->key, 'property_group_id' => $this->group->id]
406
                )->execute();
407
            }
408
            if ($this->is_column_type_stored) {
409
                Yii::$app->db->createCommand()->dropColumn($object->column_properties_table_name, $this->key)->execute();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
410
                //                if ($object->object_class == Form::className()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
411
                //                    $submissionObject = Object::getForClass(Submission::className());
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
412
                //                    Yii::$app->db->createCommand()
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
413
                //                        ->dropColumn($submissionObject->column_properties_table_name, $this->key)
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
414
                //                        ->execute();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
415
                //                }
416
            }
417
        }
418
        FilterSets::deleteAll(['property_id' => $this->id]);
419
        parent::afterDelete();
420
    }
421
422
    /**
423
     * @param $name
424
     * @return null|mixed
425
     */
426
    public function getAdditionalParam($name)
427
    {
428
        if (isset($this->handlerAdditionalParams[$name])) {
429
            return $this->handlerAdditionalParams[$name];
430
        }
431
        return null;
432
    }
433
434
    /**
435
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
436
     */
437
    public static function getAliases()
438
    {
439
        return [
440
            0 => Yii::t('app', 'Not selected'),
441
            1 => 'date',
442
            2 => 'ip',
443
            3 => 'url',
444
            4 => 'email',
445
        ];
446
    }
447
}
448