Conditions | 1 |
Paths | 1 |
Total Lines | 61 |
Code Lines | 44 |
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 |
||
174 | public function testInputRejection() |
||
175 | { |
||
176 | // Disable security ID and permissions for this test |
||
177 | Config::modify()->set(SpellController::class, 'enable_security_token', false); |
||
178 | Config::modify()->set(SpellController::class, 'required_permission', false); |
||
179 | $invalidRequest = _t('SilverStripe\\SpellCheck\\Handling\\SpellController.InvalidRequest', 'Invalid request'); |
||
180 | |||
181 | // Test spellcheck acceptance |
||
182 | $mockData = [ |
||
183 | 'method' => 'spellcheck', |
||
184 | 'lang' => 'en_NZ', |
||
185 | 'text' => 'Collor is everywhere', |
||
186 | ]; |
||
187 | $response = $this->post('spellcheck', ['ajax' => true] + $mockData); |
||
188 | $this->assertEquals(200, $response->getStatusCode()); |
||
189 | $jsonBody = json_decode($response->getBody()); |
||
190 | $this->assertNotEmpty($jsonBody->words); |
||
191 | $this->assertNotEmpty($jsonBody->words->collor); |
||
192 | $this->assertEquals(['collar', 'colour'], $jsonBody->words->collor); |
||
193 | |||
194 | // Test non-ajax rejection |
||
195 | $response = $this->post('spellcheck', $mockData); |
||
196 | $this->assertEquals(400, $response->getStatusCode()); |
||
197 | $jsonBody = json_decode($response->getBody()); |
||
198 | $this->assertEquals($invalidRequest, $jsonBody->error); |
||
199 | |||
200 | // Test incorrect method |
||
201 | $dataInvalidMethod = $mockData; |
||
202 | $dataInvalidMethod['method'] = 'validate'; |
||
203 | $response = $this->post('spellcheck', ['ajax' => true] + $dataInvalidMethod); |
||
204 | $this->assertEquals(400, $response->getStatusCode()); |
||
205 | $jsonBody = json_decode($response->getBody()); |
||
206 | $this->assertEquals( |
||
207 | _t( |
||
208 | 'SilverStripe\\SpellCheck\\Handling\\.UnsupportedMethod', |
||
209 | "Unsupported method '{method}'", |
||
210 | array('method' => 'validate') |
||
211 | ), |
||
212 | $jsonBody->error |
||
213 | ); |
||
214 | |||
215 | // Test missing method |
||
216 | $dataNoMethod = $mockData; |
||
217 | unset($dataNoMethod['method']); |
||
218 | $response = $this->post('spellcheck', ['ajax' => true] + $dataNoMethod); |
||
219 | $this->assertEquals(400, $response->getStatusCode()); |
||
220 | $jsonBody = json_decode($response->getBody()); |
||
221 | $this->assertEquals($invalidRequest, $jsonBody->error); |
||
222 | |||
223 | // Test unsupported locale |
||
224 | $dataWrongLocale = $mockData; |
||
225 | $dataWrongLocale['lang'] = 'de_DE'; |
||
226 | |||
227 | $response = $this->post('spellcheck', ['ajax' => true] + $dataWrongLocale); |
||
228 | $this->assertEquals(400, $response->getStatusCode()); |
||
229 | $jsonBody = json_decode($response->getBody()); |
||
230 | $this->assertEquals(_t( |
||
231 | 'SilverStripe\\SpellCheck\\Handling\\.InvalidLocale', |
||
232 | 'Not supported locale' |
||
233 | ), $jsonBody->error); |
||
234 | } |
||
235 | } |
||
236 |
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: