Conditions | 5 |
Paths | 17 |
Total Lines | 135 |
Code Lines | 98 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
122 | public function createUserAfterInitData(User $user): void |
||
123 | { |
||
124 | try { |
||
125 | $account = new Account(); |
||
126 | $account->setAttributes([ |
||
127 | 'name' => Yii::t('app', 'General Account'), |
||
128 | 'type' => AccountType::getName(AccountType::GENERAL_ACCOUNT), |
||
129 | 'user_id' => $user->id, |
||
130 | 'currency_balance' => 0, |
||
131 | 'default' => (bool)Account::DEFAULT, |
||
132 | 'currency_code' => $user->base_currency_code |
||
133 | ]); |
||
134 | if (!$account->save()) { |
||
135 | throw new DBException('Init Account fail ' . Setup::errorMessage($account->firstErrors)); |
||
136 | } |
||
137 | $items = [ |
||
138 | [ |
||
139 | 'name' => Yii::t('app', 'Food and drink'), |
||
140 | 'color' => ColorType::RED, |
||
141 | 'icon_name' => 'food', |
||
142 | 'transaction_type' => TransactionType::EXPENSE, |
||
143 | 'default' => Category::NOT_DEFAULT |
||
144 | ], |
||
145 | [ |
||
146 | 'name' => Yii::t('app', 'Home life'), |
||
147 | 'color' => ColorType::ORANGE, |
||
148 | 'icon_name' => 'home', |
||
149 | 'transaction_type' => TransactionType::EXPENSE, |
||
150 | 'default' => Category::NOT_DEFAULT |
||
151 | ], |
||
152 | [ |
||
153 | 'name' => Yii::t('app', 'Traffic'), |
||
154 | 'color' => ColorType::BLUE, |
||
155 | 'icon_name' => 'bus', |
||
156 | 'transaction_type' => TransactionType::EXPENSE, |
||
157 | 'default' => Category::NOT_DEFAULT |
||
158 | ], |
||
159 | [ |
||
160 | 'name' => Yii::t('app', 'Recreation'), |
||
161 | 'color' => ColorType::VOLCANO, |
||
162 | 'icon_name' => 'game', |
||
163 | 'transaction_type' => TransactionType::EXPENSE, |
||
164 | 'default' => Category::NOT_DEFAULT |
||
165 | ], |
||
166 | [ |
||
167 | 'name' => Yii::t('app', 'Health care'), |
||
168 | 'color' => ColorType::GREEN, |
||
169 | 'icon_name' => 'medicine-chest', |
||
170 | 'transaction_type' => TransactionType::EXPENSE, |
||
171 | 'default' => Category::NOT_DEFAULT |
||
172 | ], |
||
173 | [ |
||
174 | 'name' => Yii::t('app', 'Clothes'), |
||
175 | 'color' => ColorType::PURPLE, |
||
176 | 'icon_name' => 'clothes', |
||
177 | 'transaction_type' => TransactionType::EXPENSE, |
||
178 | 'default' => Category::NOT_DEFAULT |
||
179 | ], |
||
180 | [ |
||
181 | 'name' => Yii::t('app', 'Cultural education'), |
||
182 | 'color' => ColorType::CYAN, |
||
183 | 'icon_name' => 'education', |
||
184 | 'transaction_type' => TransactionType::EXPENSE, |
||
185 | 'default' => Category::NOT_DEFAULT |
||
186 | ], |
||
187 | [ |
||
188 | 'name' => Yii::t('app', 'Investment expenditure'), |
||
189 | 'color' => ColorType::GOLD, |
||
190 | 'icon_name' => 'investment', |
||
191 | 'transaction_type' => TransactionType::EXPENSE, |
||
192 | 'default' => Category::NOT_DEFAULT |
||
193 | ], |
||
194 | [ |
||
195 | 'name' => Yii::t('app', 'Childcare'), |
||
196 | 'color' => ColorType::LIME, |
||
197 | 'icon_name' => 'baby', |
||
198 | 'transaction_type' => TransactionType::EXPENSE, |
||
199 | 'default' => Category::NOT_DEFAULT |
||
200 | ], |
||
201 | [ |
||
202 | 'name' => Yii::t('app', 'Other expenses'), |
||
203 | 'color' => ColorType::GEEK_BLUE, |
||
204 | 'icon_name' => 'expenses', |
||
205 | 'transaction_type' => TransactionType::EXPENSE, |
||
206 | 'default' => Account::DEFAULT, |
||
207 | ], |
||
208 | [ |
||
209 | 'name' => Yii::t('app', 'Work income'), |
||
210 | 'color' => ColorType::BLUE, |
||
211 | 'icon_name' => 'work', |
||
212 | 'transaction_type' => TransactionType::INCOME, |
||
213 | 'default' => Category::NOT_DEFAULT |
||
214 | ], |
||
215 | [ |
||
216 | 'name' => Yii::t('app', 'Investment income'), |
||
217 | 'color' => ColorType::GOLD, |
||
218 | 'icon_name' => 'investment', |
||
219 | 'transaction_type' => TransactionType::INCOME, |
||
220 | 'default' => Category::NOT_DEFAULT |
||
221 | ], |
||
222 | [ |
||
223 | 'name' => Yii::t('app', 'Other income'), |
||
224 | 'color' => ColorType::MAGENTA, |
||
225 | 'icon_name' => 'income', |
||
226 | 'transaction_type' => TransactionType::INCOME, |
||
227 | 'default' => Category::DEFAULT, |
||
228 | ], |
||
229 | [ |
||
230 | 'name' => Yii::t('app', 'Transfer'), |
||
231 | 'color' => ColorType::GREEN, |
||
232 | 'icon_name' => 'transfer', |
||
233 | 'transaction_type' => TransactionType::TRANSFER, |
||
234 | 'default' => Category::NOT_DEFAULT, |
||
235 | ], |
||
236 | [ |
||
237 | 'name' => Yii::t('app', 'Adjust Balance'), |
||
238 | 'color' => ColorType::BLUE, |
||
239 | 'icon_name' => 'adjust', |
||
240 | 'transaction_type' => TransactionType::ADJUST, |
||
241 | 'default' => Category::NOT_DEFAULT, |
||
242 | ], |
||
243 | ]; |
||
244 | $time = date('Y-m-d H:i:s'); |
||
245 | $rows = []; |
||
246 | foreach ($items as $key => $value) { |
||
247 | $rows[$key] = $value; |
||
248 | $rows[$key]['user_id'] = $user->id; |
||
249 | $rows[$key]['created_at'] = $time; |
||
250 | $rows[$key]['updated_at'] = $time; |
||
251 | } |
||
252 | if (!ModelHelper::saveAll(Category::tableName(), $rows)) { |
||
253 | throw new DBException('Init Category fail'); |
||
254 | } |
||
255 | } catch (Exception $e) { |
||
256 | throw $e; |
||
257 | } |
||
306 |