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