| Conditions | 1 |
| Paths | 1 |
| Total Lines | 81 |
| Code Lines | 58 |
| 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 |
||
| 124 | public function testInputRejection() |
||
| 125 | { |
||
| 126 | // Disable security ID and permissions for this test |
||
| 127 | Config::modify()->set(SpellController::class, 'enable_security_token', false); |
||
| 128 | Config::modify()->set(SpellController::class, 'required_permission', false); |
||
| 129 | $invalidRequest = _t('SilverStripe\\SpellCheck\\Handling\\SpellController.InvalidRequest', 'Invalid request'); |
||
| 130 | |||
| 131 | // Test checkWords acceptance |
||
| 132 | $dataCheckWords = array( |
||
| 133 | 'id' => 'c0', |
||
| 134 | 'method' => 'checkWords', |
||
| 135 | 'params' => array( |
||
| 136 | 'en_NZ', |
||
| 137 | array('collor', 'colour', 'color', 'onee', 'correct') |
||
| 138 | ) |
||
| 139 | ); |
||
| 140 | $response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataCheckWords))); |
||
| 141 | $this->assertEquals(200, $response->getStatusCode()); |
||
| 142 | $jsonBody = json_decode($response->getBody()); |
||
| 143 | $this->assertEquals('c0', $jsonBody->id); |
||
| 144 | $this->assertEquals(array("collor", "color", "onee"), $jsonBody->result); |
||
| 145 | |||
| 146 | // Test getSuggestions acceptance |
||
| 147 | $dataGetSuggestions = array( |
||
| 148 | 'id' => '//c1//', // Should be reduced to only alphanumeric characters |
||
| 149 | 'method' => 'getSuggestions', |
||
| 150 | 'params' => array( |
||
| 151 | 'en_NZ', |
||
| 152 | 'collor' |
||
| 153 | |||
| 154 | ) |
||
| 155 | ); |
||
| 156 | $response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataGetSuggestions))); |
||
| 157 | $this->assertEquals(200, $response->getStatusCode()); |
||
| 158 | $jsonBody = json_decode($response->getBody()); |
||
| 159 | $this->assertEquals('c1', $jsonBody->id); |
||
| 160 | $this->assertEquals(array('collar', 'colour'), $jsonBody->result); |
||
| 161 | |||
| 162 | // Test non-ajax rejection |
||
| 163 | $response = $this->post('spellcheck', array('json_data' => json_encode($dataCheckWords))); |
||
| 164 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 165 | $jsonBody = json_decode($response->getBody()); |
||
| 166 | $this->assertEquals($invalidRequest, $jsonBody->error->errstr); |
||
| 167 | |||
| 168 | // Test incorrect method |
||
| 169 | $dataInvalidMethod = $dataCheckWords; |
||
| 170 | $dataInvalidMethod['method'] = 'validate'; |
||
| 171 | $response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataInvalidMethod))); |
||
| 172 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 173 | $jsonBody = json_decode($response->getBody()); |
||
| 174 | $this->assertEquals( |
||
| 175 | _t( |
||
| 176 | 'SilverStripe\\SpellCheck\\Handling\\.UnsupportedMethod', |
||
| 177 | "Unsupported method '{method}'", |
||
| 178 | array('method' => 'validate') |
||
| 179 | ), |
||
| 180 | $jsonBody->error->errstr |
||
| 181 | ); |
||
| 182 | |||
| 183 | // Test missing method |
||
| 184 | $dataNoMethod = $dataCheckWords; |
||
| 185 | unset($dataNoMethod['method']); |
||
| 186 | $response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataNoMethod))); |
||
| 187 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 188 | $jsonBody = json_decode($response->getBody()); |
||
| 189 | $this->assertEquals($invalidRequest, $jsonBody->error->errstr); |
||
| 190 | |||
| 191 | // Test unsupported locale |
||
| 192 | $dataWrongLocale = $dataCheckWords; |
||
| 193 | $dataWrongLocale['params'] = array( |
||
| 194 | 'de_DE', |
||
| 195 | array('collor', 'colour', 'color', 'onee', 'correct') |
||
| 196 | ); |
||
| 197 | $response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataWrongLocale))); |
||
| 198 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 199 | $jsonBody = json_decode($response->getBody()); |
||
| 200 | $this->assertEquals(_t( |
||
| 201 | 'SilverStripe\\SpellCheck\\Handling\\.InvalidLocale', |
||
| 202 | 'Not supported locale' |
||
| 203 | ), $jsonBody->error->errstr); |
||
| 204 | } |
||
| 205 | } |
||
| 206 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: