Conditions | 1 |
Paths | 1 |
Total Lines | 74 |
Code Lines | 52 |
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 |
||
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: