| Conditions | 1 |
| Paths | 1 |
| Total Lines | 109 |
| Code Lines | 68 |
| 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 |
||
| 25 | public function test() |
||
| 26 | { |
||
| 27 | // create 2 extra fields |
||
| 28 | $extraFieldModel = new ExtraField('session'); |
||
| 29 | $firstExtraFieldName = 'firstExtraField'.time(); |
||
| 30 | $firstExtraFieldId = $extraFieldModel->save( |
||
| 31 | [ |
||
| 32 | 'field_type' => ExtraField::FIELD_TYPE_TEXT, |
||
| 33 | 'variable' => $firstExtraFieldName, |
||
| 34 | 'display_text' => $firstExtraFieldName, |
||
| 35 | 'visible_to_self' => 1, |
||
| 36 | 'visible_to_others' => 1, |
||
| 37 | 'changeable' => 1, |
||
| 38 | 'filter' => 1, |
||
| 39 | ] |
||
| 40 | ); |
||
| 41 | $secondExtraFieldName = 'secondExtraField'.time(); |
||
| 42 | $secondExtraFieldId = $extraFieldModel->save( |
||
| 43 | [ |
||
| 44 | 'field_type' => ExtraField::FIELD_TYPE_TEXT, |
||
| 45 | 'variable' => $secondExtraFieldName, |
||
| 46 | 'display_text' => $secondExtraFieldName, |
||
| 47 | 'visible_to_self' => 1, |
||
| 48 | 'visible_to_others' => 1, |
||
| 49 | 'changeable' => 1, |
||
| 50 | 'filter' => 1, |
||
| 51 | ] |
||
| 52 | ); |
||
| 53 | |||
| 54 | // create 2 sessions |
||
| 55 | $firstSessionId = SessionManager::create_session( |
||
| 56 | 'First session'.time(), |
||
| 57 | '2019-01-01 00:00', |
||
| 58 | '2019-08-31 00:00', |
||
| 59 | '2019-01-01 00:00', |
||
| 60 | '2019-08-31 00:00', |
||
| 61 | '2019-01-01 00:00', |
||
| 62 | '2019-08-31 00:00', |
||
| 63 | null, |
||
| 64 | null |
||
| 65 | ); |
||
| 66 | $secondSessionId = SessionManager::create_session( |
||
| 67 | 'Second session'.time(), |
||
| 68 | '2019-09-01 00:00', |
||
| 69 | '2019-12-31 00:00', |
||
| 70 | '2019-09-01 00:00', |
||
| 71 | '2019-12-31 00:00', |
||
| 72 | '2019-09-01 00:00', |
||
| 73 | '2019-12-31 00:00', |
||
| 74 | null, |
||
| 75 | null |
||
| 76 | ); |
||
| 77 | |||
| 78 | // assign unique distinct value in first field to each session |
||
| 79 | SessionManager::update_session_extra_field_value($firstSessionId, $firstExtraFieldName, $firstSessionId); |
||
| 80 | SessionManager::update_session_extra_field_value($secondSessionId, $firstExtraFieldName, $secondSessionId); |
||
| 81 | |||
| 82 | // assign the same value in second field to all sessions |
||
| 83 | $commonValue = 'common value'; |
||
| 84 | SessionManager::update_session_extra_field_value($firstSessionId, $secondExtraFieldName, $commonValue); |
||
| 85 | SessionManager::update_session_extra_field_value($secondSessionId, $secondExtraFieldName, $commonValue); |
||
| 86 | |||
| 87 | // assert that the correct session id is returned using each unique value |
||
| 88 | $this->assertSame( |
||
| 89 | $firstSessionId, |
||
| 90 | $this->integer( |
||
| 91 | [ |
||
| 92 | 'field_name' => $firstExtraFieldName, |
||
| 93 | 'field_value' => $firstSessionId, |
||
| 94 | ] |
||
| 95 | ) |
||
| 96 | ); |
||
| 97 | $this->assertSame( |
||
| 98 | $secondSessionId, |
||
| 99 | $this->integer( |
||
| 100 | [ |
||
| 101 | 'field_name' => $firstExtraFieldName, |
||
| 102 | 'field_value' => $secondSessionId, |
||
| 103 | ] |
||
| 104 | ) |
||
| 105 | ); |
||
| 106 | |||
| 107 | // assert search for common value in second field generates the right error message |
||
| 108 | $this->assertSame( |
||
| 109 | get_lang('MoreThanOneSessionMatched'), |
||
| 110 | $this->errorMessageString( |
||
| 111 | [ |
||
| 112 | 'field_name' => $secondExtraFieldName, |
||
| 113 | 'field_value' => $commonValue, |
||
| 114 | ] |
||
| 115 | ) |
||
| 116 | ); |
||
| 117 | |||
| 118 | // assert search for unknown value generates the right error message |
||
| 119 | $this->assertSame( |
||
| 120 | get_lang('NoSessionMatched'), |
||
| 121 | $this->errorMessageString( |
||
| 122 | [ |
||
| 123 | 'field_name' => $secondExtraFieldName, |
||
| 124 | 'field_value' => 'non-existent value', |
||
| 125 | ] |
||
| 126 | ) |
||
| 127 | ); |
||
| 128 | |||
| 129 | // clean up |
||
| 130 | SessionManager::delete($firstSessionId); |
||
| 131 | SessionManager::delete($secondSessionId); |
||
| 132 | $extraFieldModel->delete($firstExtraFieldId); |
||
| 133 | $extraFieldModel->delete($secondExtraFieldId); |
||
| 134 | } |
||
| 136 |