| 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 |
||
| 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 spellcheck acceptance |
||
| 132 | $mockData = [ |
||
| 133 | 'method' => 'spellcheck', |
||
| 134 | 'lang' => 'en_NZ', |
||
| 135 | 'text' => 'Collor is everywhere', |
||
| 136 | ]; |
||
| 137 | $response = $this->post('spellcheck', ['ajax' => true] + $mockData); |
||
| 138 | $this->assertEquals(200, $response->getStatusCode()); |
||
| 139 | $jsonBody = json_decode($response->getBody()); |
||
| 140 | $this->assertNotEmpty($jsonBody->words); |
||
| 141 | $this->assertNotEmpty($jsonBody->words->collor); |
||
| 142 | $this->assertEquals(['collar', 'colour'], $jsonBody->words->collor); |
||
| 143 | |||
| 144 | // Test non-ajax rejection |
||
| 145 | $response = $this->post('spellcheck', $mockData); |
||
| 146 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 147 | $jsonBody = json_decode($response->getBody()); |
||
| 148 | $this->assertEquals($invalidRequest, $jsonBody->error); |
||
| 149 | |||
| 150 | // Test incorrect method |
||
| 151 | $dataInvalidMethod = $mockData; |
||
| 152 | $dataInvalidMethod['method'] = 'validate'; |
||
| 153 | $response = $this->post('spellcheck', ['ajax' => true] + $dataInvalidMethod); |
||
| 154 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 155 | $jsonBody = json_decode($response->getBody()); |
||
| 156 | $this->assertEquals( |
||
| 157 | _t( |
||
| 158 | 'SilverStripe\\SpellCheck\\Handling\\.UnsupportedMethod', |
||
| 159 | "Unsupported method '{method}'", |
||
| 160 | array('method' => 'validate') |
||
| 161 | ), |
||
| 162 | $jsonBody->error |
||
| 163 | ); |
||
| 164 | |||
| 165 | // Test missing method |
||
| 166 | $dataNoMethod = $mockData; |
||
| 167 | unset($dataNoMethod['method']); |
||
| 168 | $response = $this->post('spellcheck', ['ajax' => true] + $dataNoMethod); |
||
| 169 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 170 | $jsonBody = json_decode($response->getBody()); |
||
| 171 | $this->assertEquals($invalidRequest, $jsonBody->error); |
||
| 172 | |||
| 173 | // Test unsupported locale |
||
| 174 | $dataWrongLocale = $mockData; |
||
| 175 | $dataWrongLocale['lang'] = 'de_DE'; |
||
| 176 | |||
| 177 | $response = $this->post('spellcheck', ['ajax' => true] + $dataWrongLocale); |
||
| 178 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 179 | $jsonBody = json_decode($response->getBody()); |
||
| 180 | $this->assertEquals(_t( |
||
| 181 | 'SilverStripe\\SpellCheck\\Handling\\.InvalidLocale', |
||
| 182 | 'Not supported locale' |
||
| 183 | ), $jsonBody->error); |
||
| 184 | } |
||
| 185 | } |
||
| 186 |
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: