| Conditions | 8 |
| Paths | 25 |
| Total Lines | 64 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 | null, |
||
| 200 | new Expression("now()::timestamp") |
||
| 201 | ]; |
||
| 202 | $rows[] = $temp; |
||
| 203 | } |
||
| 204 | |||
| 205 | foreach($custom_bhvrs as $cb) { |
||
| 206 | $temp = [ |
||
| 207 | Yii::$app->user->id, |
||
| 208 | null, |
||
| 209 | null, |
||
| 210 | $cb['name'], |
||
| 211 | (int)$cb['category_id'], |
||
| 212 | new Expression("now()::timestamp") |
||
| 213 | ]; |
||
| 214 | $rows[] = $temp; |
||
| 215 | } |
||
| 216 | |||
| 217 | // this batchInsert() method properly escapes the inputted data |
||
| 218 | Yii::$app |
||
| 219 | ->db |
||
| 220 | ->createCommand() |
||
| 221 | ->batchInsert( |
||
| 222 | $user_behavior->tableName(), |
||
| 223 | ['user_id', 'behavior_id', 'category_id', 'custom_behavior', 'custom_behavior_cat', 'date'], |
||
| 224 | $rows |
||
| 225 | )->execute(); |
||
| 226 | |||
| 227 | // if the user has publicised their check-in graph, create the image |
||
| 228 | if(Yii::$app->user->identity->expose_graph) { |
||
| 229 | $checkins_last_month = $user_behavior->getCheckInBreakdown(); |
||
| 230 | |||
| 231 | if($checkins_last_month) { |
||
| 232 | Yii::$container |
||
| 233 | ->get(\common\components\Graph::class, [Yii::$app->user->identity]) |
||
| 234 | ->create($checkins_last_month, true); |
||
| 235 | } |
||
| 239 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.