| Conditions | 10 |
| Paths | 54 |
| Total Lines | 42 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 53 | public function AreManagersValidForCreate($draft_id, $managersArray) { |
||
| 54 | $valid = true; |
||
| 55 | $errors = array(); |
||
| 56 | $current_manager_count = $this->app['phpdraft.ManagerRepository']->GetNumberOfCurrentManagers($draft_id); |
||
| 57 | $numberOfNewManagers = count($managersArray); |
||
| 58 | $maxNumberOfManagers = 20 - $current_manager_count; |
||
| 59 | $numberOfManagersOver = $numberOfNewManagers - $maxNumberOfManagers; |
||
| 60 | $uniqueManagersCount = count(array_unique($managersArray)); |
||
| 61 | |||
| 62 | if ($numberOfNewManagers > $maxNumberOfManagers) { |
||
| 63 | $manager_noun = $numberOfManagersOver > 1 ? "managers" : "manager"; |
||
| 64 | $errors[] = "Unable to add $numberOfNewManagers new managers - the maximum number of managers is 20. Draft currently has $current_manager_count, so please remove $numberOfManagersOver $manager_noun to continue."; |
||
| 65 | $valid = false; |
||
| 66 | } |
||
| 67 | |||
| 68 | if ($numberOfNewManagers != $uniqueManagersCount) { |
||
| 69 | $errors[] = "Two or more managers had the same name - please ensure all managers have unique names."; |
||
| 70 | $valid = false; |
||
| 71 | } |
||
| 72 | |||
| 73 | $manager_number = 1; |
||
| 74 | foreach ($managersArray as $manager) { |
||
| 75 | if (empty($manager->draft_id) |
||
| 76 | || empty($manager->manager_name)) { |
||
| 77 | $errors[] = "One or more missing fields for manager #$manager_number."; |
||
| 78 | $valid = false; |
||
| 79 | } |
||
| 80 | |||
| 81 | if (!empty($manager->manager_name) && strlen($manager->manager_name) > 255) { |
||
| 82 | $errors[] = "Manager #$manager_number's name is above the maximum length."; |
||
| 83 | $valid = false; |
||
| 84 | } |
||
| 85 | |||
| 86 | if (!$this->app['phpdraft.ManagerRepository']->NameIsUnique($manager->manager_name, $draft_id)) { |
||
| 87 | $errors[] = "Manager name '$manager->manager_name' already exists - please choose another name."; |
||
| 88 | $valid = false; |
||
| 89 | } |
||
| 90 | |||
| 91 | $manager_number++; |
||
| 92 | } |
||
| 93 | |||
| 94 | return new PhpDraftResponse($valid, $errors); |
||
| 95 | } |
||
| 160 | } |