Conditions | 8 |
Paths | 25 |
Total Lines | 62 |
Code Lines | 44 |
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 |
||
171 | public function save() { |
||
172 | if(empty($this->compiled_behaviors) && empty($this->custom_behaviors)) { |
||
173 | // maybe we haven't compiled the behaviors yet |
||
174 | $exit = !!!$this->compileBehaviors(); |
||
175 | if($exit) return false; |
||
176 | } |
||
177 | |||
178 | $custom_bhvrs = \common\models\CustomBehavior::find() |
||
179 | ->where([ |
||
180 | 'user_id' => Yii::$app->user->id, |
||
181 | 'id' => (array)$this->getCustomBehaviors() |
||
182 | ]) |
||
183 | ->asArray() |
||
184 | ->all(); |
||
185 | |||
186 | $user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class); |
||
187 | |||
188 | $rows = []; |
||
189 | // this can be condensed....but we are optimizing for readability |
||
190 | foreach($this->compiled_behaviors as $behavior_id) { |
||
191 | $behavior_id = (int)$behavior_id; |
||
192 | $behavior = \common\models\Behavior::getBehavior('id', $behavior_id); |
||
193 | $category_id = $behavior['category_id']; |
||
194 | $temp = [ |
||
195 | Yii::$app->user->id, |
||
196 | (int)$behavior_id, |
||
197 | (int)$category_id, |
||
198 | null, |
||
199 | new Expression("now()::timestamp") |
||
200 | ]; |
||
201 | $rows[] = $temp; |
||
202 | } |
||
203 | |||
204 | foreach($custom_bhvrs as $cb) { |
||
205 | $temp = [ |
||
206 | Yii::$app->user->id, |
||
207 | null, |
||
208 | (int)$cb['category_id'], |
||
209 | $cb['name'], |
||
210 | new Expression("now()::timestamp") |
||
211 | ]; |
||
212 | $rows[] = $temp; |
||
213 | } |
||
214 | |||
215 | // this batchInsert() method properly escapes the inputted data |
||
216 | Yii::$app |
||
217 | ->db |
||
218 | ->createCommand() |
||
219 | ->batchInsert( |
||
220 | $user_behavior->tableName(), |
||
221 | ['user_id', 'behavior_id', 'category_id', 'custom_behavior', 'date'], |
||
222 | $rows |
||
223 | )->execute(); |
||
224 | |||
225 | // if the user has publicised their check-in graph, create the image |
||
226 | if(Yii::$app->user->identity->expose_graph) { |
||
227 | $checkins_last_month = $user_behavior->getCheckInBreakdown(); |
||
228 | |||
229 | if($checkins_last_month) { |
||
230 | Yii::$container |
||
231 | ->get(\common\components\Graph::class, [Yii::$app->user->identity]) |
||
232 | ->create($checkins_last_month, true); |
||
233 | } |
||
237 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.