| Conditions | 1 |
| Paths | 1 |
| Total Lines | 73 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 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 |
||
| 131 | public function testInputRejection() { |
||
| 132 | // Disable security ID and permissions for this test |
||
| 133 | Config::inst()->update('SpellController', 'enable_security_token', false); |
||
| 134 | Config::inst()->update('SpellController', 'required_permission', false); |
||
| 135 | $invalidRequest = _t('SpellController.InvalidRequest', 'Invalid request'); |
||
| 136 | |||
| 137 | // Test checkWords acceptance |
||
| 138 | $dataCheckWords = array( |
||
| 139 | 'id' => 'c0', |
||
| 140 | 'method' => 'checkWords', |
||
| 141 | 'params' => array( |
||
| 142 | 'en_NZ', |
||
| 143 | array('collor', 'colour', 'color', 'onee', 'correct') |
||
| 144 | ) |
||
| 145 | ); |
||
| 146 | $response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataCheckWords))); |
||
| 147 | $this->assertEquals(200, $response->getStatusCode()); |
||
| 148 | $jsonBody = json_decode($response->getBody()); |
||
| 149 | $this->assertEquals('c0', $jsonBody->id); |
||
| 150 | $this->assertEquals(array("collor", "color", "onee"), $jsonBody->result); |
||
| 151 | |||
| 152 | // Test getSuggestions acceptance |
||
| 153 | $dataGetSuggestions = array( |
||
| 154 | 'id' => '//c1//', // Should be reduced to only alphanumeric characters |
||
| 155 | 'method' => 'getSuggestions', |
||
| 156 | 'params' => array( |
||
| 157 | 'en_NZ', |
||
| 158 | 'collor' |
||
| 159 | |||
| 160 | ) |
||
| 161 | ); |
||
| 162 | $response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataGetSuggestions))); |
||
| 163 | $this->assertEquals(200, $response->getStatusCode()); |
||
| 164 | $jsonBody = json_decode($response->getBody()); |
||
| 165 | $this->assertEquals('c1', $jsonBody->id); |
||
| 166 | $this->assertEquals(array('collar', 'colour'), $jsonBody->result); |
||
| 167 | |||
| 168 | // Test non-ajax rejection |
||
| 169 | $response = $this->post('spellcheck', array('json_data' => json_encode($dataCheckWords))); |
||
| 170 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 171 | $jsonBody = json_decode($response->getBody()); |
||
| 172 | $this->assertEquals($invalidRequest, $jsonBody->error->errstr); |
||
| 173 | |||
| 174 | // Test incorrect method |
||
| 175 | $dataInvalidMethod = $dataCheckWords; |
||
| 176 | $dataInvalidMethod['method'] = 'validate'; |
||
| 177 | $response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataInvalidMethod))); |
||
| 178 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 179 | $jsonBody = json_decode($response->getBody()); |
||
| 180 | $this->assertEquals( |
||
| 181 | _t('SpellController.UnsupportedMethod', "Unsupported method '{method}'", array('method' => 'validate')), |
||
| 182 | $jsonBody->error->errstr |
||
| 183 | ); |
||
| 184 | |||
| 185 | // Test missing method |
||
| 186 | $dataNoMethod = $dataCheckWords; |
||
| 187 | unset($dataNoMethod['method']); |
||
| 188 | $response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataNoMethod))); |
||
| 189 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 190 | $jsonBody = json_decode($response->getBody()); |
||
| 191 | $this->assertEquals($invalidRequest, $jsonBody->error->errstr); |
||
| 192 | |||
| 193 | // Test unsupported locale |
||
| 194 | $dataWrongLocale = $dataCheckWords; |
||
| 195 | $dataWrongLocale['params'] = array( |
||
| 196 | 'de_DE', |
||
| 197 | array('collor', 'colour', 'color', 'onee', 'correct') |
||
| 198 | ); |
||
| 199 | $response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataWrongLocale))); |
||
| 200 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 201 | $jsonBody = json_decode($response->getBody()); |
||
| 202 | $this->assertEquals(_t('SpellController.InvalidLocale', 'Not supported locale'), $jsonBody->error->errstr); |
||
| 203 | } |
||
| 204 | } |
||
| 205 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.