| Conditions | 12 |
| Paths | 24 |
| Total Lines | 42 |
| Code Lines | 25 |
| 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 |
||
| 69 | public function updateGradeBookListBySkill($skill_id, $gradebook_list) |
||
| 70 | { |
||
| 71 | $original_gradebook_list = $this->find( |
||
| 72 | 'all', |
||
| 73 | ['where' => ['skill_id = ?' => [$skill_id]]] |
||
| 74 | ); |
||
| 75 | $gradebooks_to_remove = []; |
||
| 76 | $gradebooks_to_add = []; |
||
| 77 | $original_gradebook_list_ids = []; |
||
| 78 | |||
| 79 | if (!empty($original_gradebook_list)) { |
||
| 80 | foreach ($original_gradebook_list as $gradebook) { |
||
| 81 | if (!in_array($gradebook['gradebook_id'], $gradebook_list)) { |
||
| 82 | $gradebooks_to_remove[] = $gradebook['id']; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | foreach ($original_gradebook_list as $gradebook_item) { |
||
| 86 | $original_gradebook_list_ids[] = $gradebook_item['gradebook_id']; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | if (!empty($gradebook_list)) { |
||
| 91 | foreach ($gradebook_list as $gradebook_id) { |
||
| 92 | if (!in_array($gradebook_id, $original_gradebook_list_ids)) { |
||
| 93 | $gradebooks_to_add[] = $gradebook_id; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | if (!empty($gradebooks_to_remove)) { |
||
| 99 | foreach ($gradebooks_to_remove as $id) { |
||
| 100 | $this->delete($id); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | if (!empty($gradebooks_to_add)) { |
||
| 105 | foreach ($gradebooks_to_add as $gradebook_id) { |
||
| 106 | $attributes = [ |
||
| 107 | 'skill_id' => $skill_id, |
||
| 108 | 'gradebook_id' => $gradebook_id, |
||
| 109 | ]; |
||
| 110 | $this->save($attributes); |
||
| 111 | } |
||
| 139 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: