Rule   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 68
c 2
b 1
f 0
dl 0
loc 144
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 6 1
B beforeSave() 0 18 8
A rules() 0 11 1
A tableName() 0 3 1
A getThenCategory() 0 3 1
A attributeLabels() 0 18 1
A fields() 0 45 2
1
<?php
2
3
namespace app\core\models;
4
5
use app\core\exceptions\InvalidArgumentException;
6
use app\core\types\ReimbursementStatus;
7
use app\core\types\RuleStatus;
8
use app\core\types\TransactionStatus;
9
use app\core\types\TransactionType;
10
use Yii;
11
use yii\behaviors\TimestampBehavior;
12
use yiier\helpers\DateHelper;
13
use yiier\validators\ArrayValidator;
14
15
/**
16
 * This is the model class for table "{{%rule}}".
17
 *
18
 * @property int $id
19
 * @property int $user_id
20
 * @property string $name
21
 * @property string|array $if_keywords Multiple choice use,
22
 * @property int $then_transaction_type
23
 * @property int|null $then_category_id
24
 * @property int|null $then_from_account_id
25
 * @property int|null $then_to_account_id
26
 * @property int|null $then_transaction_status
27
 * @property int|null $then_reimbursement_status
28
 * @property string|null|array $then_tags Multiple choice use,
29
 * @property int|string $status
30
 * @property int|null $sort
31
 * @property string|null $created_at
32
 * @property string|null $updated_at
33
 *
34
 * @property-read Category $thenCategory
35
 */
36
class Rule extends \yii\db\ActiveRecord
37
{
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function tableName()
42
    {
43
        return '{{%rule}}';
44
    }
45
46
    /**
47
     * @inheritdoc
48
     * @throws \yii\base\InvalidConfigException
49
     */
50
    public function behaviors()
51
    {
52
        return [
53
            [
54
                'class' => TimestampBehavior::class,
55
                'value' => Yii::$app->formatter->asDatetime('now')
56
            ],
57
        ];
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function rules()
64
    {
65
        return [
66
            [['name', 'if_keywords', 'then_transaction_type'], 'required'],
67
            [['user_id', 'then_category_id', 'then_from_account_id', 'then_to_account_id', 'sort'], 'integer'],
68
            ['status', 'in', 'range' => RuleStatus::names()],
69
            ['then_transaction_type', 'in', 'range' => TransactionType::names()],
70
            ['then_reimbursement_status', 'in', 'range' => ReimbursementStatus::names()],
71
            ['then_transaction_status', 'in', 'range' => TransactionStatus::names()],
72
            [['if_keywords', 'then_tags'], ArrayValidator::class],
73
            [['name'], 'string', 'max' => 255],
74
        ];
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function attributeLabels()
81
    {
82
        return [
83
            'id' => Yii::t('app', 'ID'),
84
            'user_id' => Yii::t('app', 'User ID'),
85
            'name' => Yii::t('app', 'Name'),
86
            'if_keywords' => Yii::t('app', 'If Keywords'),
87
            'then_transaction_type' => Yii::t('app', 'Then Transaction Type'),
88
            'then_category_id' => Yii::t('app', 'Then Category ID'),
89
            'then_from_account_id' => Yii::t('app', 'Then From Account ID'),
90
            'then_to_account_id' => Yii::t('app', 'Then To Account ID'),
91
            'then_transaction_status' => Yii::t('app', 'Then Transaction Status'),
92
            'then_reimbursement_status' => Yii::t('app', 'Then Reimbursement Status'),
93
            'then_tags' => Yii::t('app', 'Then Tags'),
94
            'status' => Yii::t('app', 'Status'),
95
            'sort' => Yii::t('app', 'Sort'),
96
            'created_at' => Yii::t('app', 'Created At'),
97
            'updated_at' => Yii::t('app', 'Updated At'),
98
        ];
99
    }
100
101
    /**
102
     * @param bool $insert
103
     * @return bool
104
     * @throws InvalidArgumentException
105
     */
106
    public function beforeSave($insert)
107
    {
108
        if (parent::beforeSave($insert)) {
109
            if ($insert) {
110
                $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...
111
            }
112
            $this->then_reimbursement_status = is_null($this->then_reimbursement_status) ?
113
                ReimbursementStatus::NONE : ReimbursementStatus::toEnumValue($this->then_reimbursement_status);
114
            $this->then_transaction_status = is_null($this->then_transaction_status) ?
115
                TransactionStatus::DONE : TransactionStatus::toEnumValue($this->then_transaction_status);
116
117
            $this->status = is_null($this->status) ? RuleStatus::ACTIVE : RuleStatus::toEnumValue($this->status);
0 ignored issues
show
introduced by
The condition is_null($this->status) is always false.
Loading history...
118
            $this->then_transaction_type = TransactionType::toEnumValue($this->then_transaction_type);
119
            $this->if_keywords = $this->if_keywords ? implode(',', $this->if_keywords) : null;
0 ignored issues
show
Bug introduced by
It seems like $this->if_keywords can also be of type string; however, parameter $pieces of implode() does only seem to accept array, 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

119
            $this->if_keywords = $this->if_keywords ? implode(',', /** @scrutinizer ignore-type */ $this->if_keywords) : null;
Loading history...
120
            $this->then_tags = $this->then_tags ? implode(',', $this->then_tags) : null;
121
            return true;
122
        } else {
123
            return false;
124
        }
125
    }
126
127
    public function getThenCategory()
128
    {
129
        return $this->hasOne(Category::class, ['id' => 'then_category_id']);
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function fields()
136
    {
137
        $fields = parent::fields();
138
        unset($fields['user_id']);
139
140
        $fields['then_transaction_type'] = function (self $model) {
141
            return TransactionType::getName($model->then_transaction_type);
142
        };
143
144
        $fields['then_transaction_type_text'] = function (self $model) {
145
            return data_get(TransactionType::texts(), $model->then_transaction_type);
146
        };
147
148
        $fields['then_tags'] = function (self $model) {
149
            return $model->then_tags ? explode(',', $model->then_tags) : [];
0 ignored issues
show
Bug introduced by
It seems like $model->then_tags can also be of type array; however, parameter $string of explode() 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

149
            return $model->then_tags ? explode(',', /** @scrutinizer ignore-type */ $model->then_tags) : [];
Loading history...
150
        };
151
        $fields['thenCategory'] = function (self $model) {
152
            return $model->thenCategory;
153
        };
154
155
        $fields['if_keywords'] = function (self $model) {
156
            return explode(',', $model->if_keywords);
0 ignored issues
show
Bug introduced by
It seems like $model->if_keywords can also be of type array; however, parameter $string of explode() 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

156
            return explode(',', /** @scrutinizer ignore-type */ $model->if_keywords);
Loading history...
157
        };
158
159
        $fields['status'] = function (self $model) {
160
            return RuleStatus::getName($model->status);
0 ignored issues
show
Bug introduced by
It seems like $model->status can also be of type string; however, parameter $v of app\core\types\BaseType::getName() does only seem to accept integer, 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

160
            return RuleStatus::getName(/** @scrutinizer ignore-type */ $model->status);
Loading history...
161
        };
162
163
        $fields['then_reimbursement_status'] = function (self $model) {
164
            return ReimbursementStatus::getName($model->then_reimbursement_status);
0 ignored issues
show
Bug introduced by
It seems like $model->then_reimbursement_status can also be of type null; however, parameter $v of app\core\types\BaseType::getName() does only seem to accept integer, 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

164
            return ReimbursementStatus::getName(/** @scrutinizer ignore-type */ $model->then_reimbursement_status);
Loading history...
165
        };
166
167
        $fields['then_transaction_status'] = function (self $model) {
168
            return TransactionStatus::getName($model->then_transaction_status);
0 ignored issues
show
Bug introduced by
It seems like $model->then_transaction_status can also be of type null; however, parameter $v of app\core\types\BaseType::getName() does only seem to accept integer, 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

168
            return TransactionStatus::getName(/** @scrutinizer ignore-type */ $model->then_transaction_status);
Loading history...
169
        };
170
171
        $fields['created_at'] = function (self $model) {
172
            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

172
            return DateHelper::datetimeToIso8601(/** @scrutinizer ignore-type */ $model->created_at);
Loading history...
173
        };
174
175
        $fields['updated_at'] = function (self $model) {
176
            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

176
            return DateHelper::datetimeToIso8601(/** @scrutinizer ignore-type */ $model->updated_at);
Loading history...
177
        };
178
179
        return $fields;
180
    }
181
}
182