Conditions | 1 |
Paths | 1 |
Total Lines | 139 |
Code Lines | 77 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
271 | public function testQueryWithMatchOnPath() |
||
272 | { |
||
273 | // Data Source: Object |
||
274 | // Return Type: ARRAY |
||
275 | // Operator: "#>" (Path) |
||
276 | // Expect: Array due to duplicate keys in different parts of the source data |
||
277 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
278 | $field->setReturnType('array'); |
||
279 | $field->setValue($this->getFixture('object')); |
||
280 | $this->assertEquals( |
||
281 | [['Subaru' => 'Impreza'],['Kawasaki' => 'KR1S250']], |
||
282 | $field->query('#>', '{"japanese":"fast"}') |
||
283 | ); |
||
284 | |||
285 | // Data Source: Object |
||
286 | // Return Type: JSON |
||
287 | // Operator: "#>" (Path) |
||
288 | // Expect: Array due to duplicate keys in different parts of the source data |
||
289 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
290 | $field->setReturnType('json'); |
||
291 | $field->setValue($this->getFixture('object')); |
||
292 | $this->assertEquals( |
||
293 | '[{"Subaru":"Impreza"},{"Kawasaki":"KR1S250"}]', |
||
294 | $field->query('#>', '{"japanese":"fast"}') |
||
295 | ); |
||
296 | |||
297 | // Data Source: Object |
||
298 | // Return Type: SILVERSTRIPE |
||
299 | // Operator: "#>" (Path) |
||
300 | // Expect: Array due to duplicate keys in different parts of the source data |
||
301 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
302 | $field->setReturnType('silverstripe'); |
||
303 | $field->setValue($this->getFixture('object')); |
||
304 | $this->assertInternalType('array', $field->query('#>', '{"japanese":"fast"}')); |
||
305 | $this->assertCount(2, $field->query('#>', '{"japanese":"fast"}')); |
||
306 | |||
307 | $one = $field->query('#>', '{"japanese":"fast"}')[0]; |
||
308 | $two = $field->query('#>', '{"japanese":"fast"}')[1]; |
||
309 | |||
310 | $this->assertInternalType('array', $one); |
||
311 | $this->assertInternalType('array', $two); |
||
312 | $this->assertInstanceOf('Varchar', array_values($one)[0]); |
||
313 | $this->assertInstanceOf('Varchar', array_values($two)[0]); |
||
314 | $this->assertEquals('Impreza', array_values($one)[0]->getValue()); |
||
315 | $this->assertEquals('KR1S250', array_values($two)[0]->getValue()); |
||
316 | |||
317 | // Data Source: Object |
||
318 | // Return Type: ARRAY |
||
319 | // Operator: "#>" (Path) |
||
320 | // Expect: Direct scalar comparison assertion |
||
321 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
322 | $field->setReturnType('array'); |
||
323 | $field->setValue($this->getFixture('object')); |
||
324 | $this->assertEquals(['airbus'], $field->query('#>', '{"planes":"french"}')); |
||
325 | |||
326 | // Data Source: Object |
||
327 | // Return Type: JSON |
||
328 | // Operator: "#>" (Path) |
||
329 | // Expect: Direct scalar comparison assertion |
||
330 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
331 | $field->setReturnType('json'); |
||
332 | $field->setValue($this->getFixture('object')); |
||
333 | $this->assertEquals('["airbus"]', $field->query('#>', '{"planes":"french"}')); |
||
334 | |||
335 | // Data Source: Object |
||
336 | // Return Type: SILVERSTRIPE |
||
337 | // Operator: "#>" (Path) |
||
338 | // Expect: Direct scalar comparison assertion (Varchar) |
||
339 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
340 | $field->setReturnType('silverstripe'); |
||
341 | $field->setValue($this->getFixture('object')); |
||
342 | $this->assertInternalType('array', $field->query('#>', '{"planes":"french"}')); |
||
343 | $this->assertInstanceOf('Varchar', $field->query('#>', '{"planes":"french"}')[0]); |
||
344 | $this->assertEquals('airbus', $field->query('#>', '{"planes":"french"}')[0]->getValue()); |
||
345 | |||
346 | // Data Source: Object |
||
347 | // Return Type: SILVERSTRIPE |
||
348 | // Operator: "#>" (Path) |
||
349 | // Expect: Direct scalar comparison assertion (Float) |
||
350 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
351 | $field->setReturnType('silverstripe'); |
||
352 | $field->setValue($this->getFixture('object')); |
||
353 | |||
354 | $res = $field->query('#>', '{"floats":"0"}'); |
||
355 | |||
356 | $this->assertInternalType('array', $res); |
||
357 | $this->assertInternalType('array', $res[0]); // Why? Because value of "floats" key is a JSON array |
||
358 | $this->assertInstanceOf('Float', array_values($res[0])[0]); |
||
359 | $this->assertEquals(99.99, array_values($res[0])[0]->getValue()); |
||
360 | |||
361 | // Data Source: Object |
||
362 | // Return Type: SILVERSTRIPE |
||
363 | // Operator: "#>" (Path) |
||
364 | // Expect: Direct scalar comparison assertion (Int) |
||
365 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
366 | $field->setReturnType('silverstripe'); |
||
367 | $field->setValue($this->getFixture('object')); |
||
368 | |||
369 | $res = $field->query('#>', '{"ints":"0"}'); |
||
370 | |||
371 | $this->assertInternalType('array', $res); |
||
372 | $this->assertInternalType('array', $res[0]); // Why? Because value of "floats" key is a JSON array |
||
373 | $this->assertInstanceOf('Int', array_values($res[0])[1]); |
||
374 | $this->assertEquals(6, array_values($res[0])[1]->getValue()); |
||
375 | |||
376 | // Data Source: Object |
||
377 | // Return Type: SILVERSTRIPE |
||
378 | // Operator: "#>" (Path) |
||
379 | // Expect: Direct scalar comparison assertion (Boolean) |
||
380 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
381 | $field->setReturnType('silverstripe'); |
||
382 | $field->setValue($this->getFixture('object')); |
||
383 | |||
384 | $res = $field->query('#>', '{"booleans":"0"}'); |
||
385 | |||
386 | $this->assertInternalType('array', $res); |
||
387 | $this->assertInternalType('array', $res[0]); // Why? Because value of "booleans" key is a JSON array |
||
388 | $this->assertInstanceOf('Boolean', array_values($res[0])[0]); |
||
389 | $this->assertEquals(1, array_values($res[0])[0]->getValue()); |
||
390 | |||
391 | // #1 Empty source data |
||
392 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
393 | $field->setReturnType('array'); |
||
394 | $field->setValue(''); |
||
395 | $this->assertEquals([], $field->query('#>', '{"japanese":"fast"}')); |
||
396 | |||
397 | // #2 JSON path not found |
||
398 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
399 | $field->setReturnType('silverstripe'); |
||
400 | $field->setValue($this->getFixture('object')); |
||
401 | $this->assertNull($field->query('#>', '{"ints":"1"}')); // The "ints" key only has a single array as a value |
||
402 | |||
403 | // #3 Invalid operand on RHS |
||
404 | $this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
||
405 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
406 | $field->setReturnType('array'); |
||
407 | $field->setValue($this->getFixture('object')); |
||
408 | $this->assertEquals(['Kawasaki' => 'KR1S250'], $field->query('#>', '{"japanese":"fast"')); |
||
409 | } |
||
410 | |||
424 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.