| Conditions | 9 |
| Paths | 34 |
| Total Lines | 63 |
| Code Lines | 37 |
| 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 |
||
| 29 | public function updateUserPauseTraining($userId, $values) |
||
| 30 | { |
||
| 31 | $userInfo = api_get_user_info($userId); |
||
| 32 | if (empty($userInfo)) { |
||
| 33 | throw new Exception("User #$userId does not exists"); |
||
| 34 | } |
||
| 35 | |||
| 36 | $variables = [ |
||
| 37 | 'pause_formation', |
||
| 38 | 'start_pause_date', |
||
| 39 | 'end_pause_date', |
||
| 40 | 'allow_notifications', |
||
| 41 | ]; |
||
| 42 | |||
| 43 | $valuesToUpdate = [ |
||
| 44 | 'item_id' => $userId, |
||
| 45 | ]; |
||
| 46 | |||
| 47 | // Check if variables exist. |
||
| 48 | foreach ($variables as $variable) { |
||
| 49 | if (!isset($values[$variable])) { |
||
| 50 | throw new Exception("Variable '$variable' is missing. Cannot updated."); |
||
| 51 | } |
||
| 52 | |||
| 53 | $valuesToUpdate['extra_'.$variable] = $values[$variable]; |
||
| 54 | } |
||
| 55 | |||
| 56 | // Clean variables |
||
| 57 | $pause = (int) $valuesToUpdate['extra_pause_formation']; |
||
| 58 | if (empty($pause)) { |
||
| 59 | $valuesToUpdate['extra_pause_formation'] = 0; |
||
| 60 | } else { |
||
| 61 | $valuesToUpdate['extra_pause_formation'] = []; |
||
| 62 | $valuesToUpdate['extra_pause_formation']['extra_pause_formation'] = $pause; |
||
| 63 | } |
||
| 64 | |||
| 65 | $notification = (int) $valuesToUpdate['extra_allow_notifications']; |
||
| 66 | if (empty($notification)) { |
||
| 67 | $valuesToUpdate['extra_allow_notifications'] = 0; |
||
| 68 | } else { |
||
| 69 | $valuesToUpdate['extra_allow_notifications'] = []; |
||
| 70 | $valuesToUpdate['extra_allow_notifications']['extra_allow_notifications'] = $notification; |
||
| 71 | } |
||
| 72 | |||
| 73 | $check = DateTime::createFromFormat('Y-m-d H:i', $valuesToUpdate['extra_start_pause_date']); |
||
| 74 | |||
| 75 | if (false === $check) { |
||
| 76 | throw new Exception("start_pause_date is not valid date time format should be: Y-m-d H:i"); |
||
| 77 | } |
||
| 78 | |||
| 79 | $check = DateTime::createFromFormat('Y-m-d H:i', $valuesToUpdate['extra_end_pause_date']); |
||
| 80 | if (false === $check) { |
||
| 81 | throw new Exception("end_pause_date is not valid date time format should be: Y-m-d H:i"); |
||
| 82 | } |
||
| 83 | |||
| 84 | if (api_strtotime($valuesToUpdate['extra_start_pause_date']) > api_strtotime($valuesToUpdate['extra_end_pause_date'])) { |
||
| 85 | throw new Exception("end_pause_date must be bigger than start_pause_date"); |
||
| 86 | } |
||
| 87 | |||
| 88 | $extraField = new ExtraFieldValue('user'); |
||
| 89 | $extraField->saveFieldValues($valuesToUpdate, true, false, [], [], true); |
||
| 90 | |||
| 91 | return (int) $userId; |
||
| 92 | } |
||
| 195 |