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