Category   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 52
c 2
b 0
f 0
dl 0
loc 121
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 6 1
A tableName() 0 3 1
A attributeLabels() 0 14 1
A beforeSave() 0 11 4
A rules() 0 13 1
A beforeValidate() 0 7 2
A fields() 0 22 1
1
<?php
2
3
namespace app\core\models;
4
5
use app\core\exceptions\InvalidArgumentException;
6
use app\core\types\ColorType;
7
use app\core\types\TransactionType;
8
use Yii;
9
use yii\behaviors\TimestampBehavior;
10
use yiier\helpers\DateHelper;
11
12
/**
13
 * This is the model class for table "{{%category}}".
14
 *
15
 * @property int $id
16
 * @property int $user_id
17
 * @property int $transaction_type
18
 * @property string $name
19
 * @property string $color
20
 * @property string $icon_name
21
 * @property int|null $status
22
 * @property int $default
23
 * @property int|null $sort
24
 * @property string|null $created_at
25
 * @property string|null $updated_at
26
 */
27
class Category extends \yii\db\ActiveRecord
28
{
29
    public const NOT_DEFAULT = 0;
30
    public const DEFAULT = 1;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public static function tableName()
36
    {
37
        return '{{%category}}';
38
    }
39
40
    /**
41
     * @inheritdoc
42
     * @throws \yii\base\InvalidConfigException
43
     */
44
    public function behaviors()
45
    {
46
        return [
47
            [
48
                'class' => TimestampBehavior::class,
49
                'value' => Yii::$app->formatter->asDatetime('now')
50
            ],
51
        ];
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function rules()
58
    {
59
        return [
60
            [['transaction_type', 'name', 'icon_name'], 'required'],
61
            [['user_id', 'status', 'default', 'sort'], 'integer'],
62
            ['transaction_type', 'in', 'range' => TransactionType::names()],
63
            [['name', 'icon_name'], 'string', 'max' => 120],
64
            ['color', 'in', 'range' => ColorType::items()],
65
            [
66
                'name',
67
                'unique',
68
                'targetAttribute' => ['user_id', 'name'],
69
                'message' => Yii::t('app', 'The {attribute} has been used.')
70
            ],
71
        ];
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function attributeLabels()
78
    {
79
        return [
80
            'id' => Yii::t('app', 'ID'),
81
            'user_id' => Yii::t('app', 'User ID'),
82
            'transaction_type' => Yii::t('app', 'Transaction Type'),
83
            'name' => Yii::t('app', 'Name'),
84
            'color' => Yii::t('app', 'Color'),
85
            'icon_name' => Yii::t('app', 'Icon Name'),
86
            'status' => Yii::t('app', 'Status'),
87
            'default' => Yii::t('app', 'Default'),
88
            'sort' => Yii::t('app', 'Sort'),
89
            'created_at' => Yii::t('app', 'Created At'),
90
            'updated_at' => Yii::t('app', 'Updated At'),
91
        ];
92
    }
93
94
    public function beforeValidate()
95
    {
96
        if (parent::beforeValidate()) {
97
            $this->user_id = Yii::$app->user->id;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->user->id can also be of type string. However, the property $user_id is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
98
            return true;
99
        }
100
        return false;
101
    }
102
103
104
    /**
105
     * @param bool $insert
106
     * @return bool
107
     * @throws InvalidArgumentException
108
     */
109
    public function beforeSave($insert)
110
    {
111
        if (parent::beforeSave($insert)) {
112
            if ($insert) {
113
                $ran = ColorType::items();
114
                $this->color = $this->color ?: $ran[mt_rand(0, count($ran) - 1)];
115
            }
116
            $this->transaction_type = TransactionType::toEnumValue($this->transaction_type);
117
            return true;
118
        } else {
119
            return false;
120
        }
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function fields()
127
    {
128
        $fields = parent::fields();
129
        unset($fields['user_id']);
130
131
        $fields['transaction_type'] = function (self $model) {
132
            return TransactionType::getName($model->transaction_type);
133
        };
134
135
        $fields['transaction_type_text'] = function (self $model) {
136
            return data_get(TransactionType::texts(), $model->transaction_type);
137
        };
138
139
        $fields['created_at'] = function (self $model) {
140
            return DateHelper::datetimeToIso8601($model->created_at);
0 ignored issues
show
Bug introduced by
It seems like $model->created_at can also be of type null; however, parameter $dateStr of yiier\helpers\DateHelper::datetimeToIso8601() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

140
            return DateHelper::datetimeToIso8601(/** @scrutinizer ignore-type */ $model->created_at);
Loading history...
141
        };
142
143
        $fields['updated_at'] = function (self $model) {
144
            return DateHelper::datetimeToIso8601($model->updated_at);
0 ignored issues
show
Bug introduced by
It seems like $model->updated_at can also be of type null; however, parameter $dateStr of yiier\helpers\DateHelper::datetimeToIso8601() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

144
            return DateHelper::datetimeToIso8601(/** @scrutinizer ignore-type */ $model->updated_at);
Loading history...
145
        };
146
147
        return $fields;
148
    }
149
}
150