Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FiltererTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FiltererTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | final class FiltererTest extends TestCase |
||
19 | { |
||
20 | public function setUp() |
||
24 | |||
25 | /** |
||
26 | * @test |
||
27 | * @covers ::filter |
||
28 | * @covers ::execute |
||
29 | * @dataProvider provideValidFilterData |
||
30 | * |
||
31 | * @param array $spec The filter specification to be use. |
||
32 | * @param array $input The input to be filtered. |
||
33 | * @param array $options The filterer options |
||
34 | * @param array $expectedResult The expected filterer result. |
||
35 | */ |
||
36 | public function filter(array $spec, array $input, array $options, array $expectedResult) |
||
40 | |||
41 | public function provideValidFilterData() : array |
||
231 | |||
232 | /** |
||
233 | * @test |
||
234 | * @covers ::filter |
||
235 | * @covers ::execute |
||
236 | */ |
||
237 | public function filterReturnsResponseType() |
||
252 | |||
253 | /** |
||
254 | * @test |
||
255 | * @covers ::filter |
||
256 | * @covers ::execute |
||
257 | */ |
||
258 | public function filterReturnsResponseTypeWithErrors() |
||
273 | |||
274 | /** |
||
275 | * @test |
||
276 | * @covers ::filter |
||
277 | * @covers ::execute |
||
278 | * @covers ::setFilterAliases |
||
279 | */ |
||
280 | public function filterCustomShortNamePass() |
||
286 | |||
287 | /** |
||
288 | * @test |
||
289 | * @covers ::filter |
||
290 | * @covers ::execute |
||
291 | * @covers ::setFilterAliases |
||
292 | * @covers ::getFilterAliases |
||
293 | */ |
||
294 | public function filterGetSetKnownFilters() |
||
300 | |||
301 | /** |
||
302 | * @test |
||
303 | * @covers ::setFilterAliases |
||
304 | */ |
||
305 | public function setBadFilterAliases() |
||
319 | |||
320 | /** |
||
321 | * @test |
||
322 | * @covers ::filter |
||
323 | * @covers ::execute |
||
324 | * @expectedException Exception |
||
325 | * @expectedExceptionMessage Function 'boo' for field 'foo' is not callable |
||
326 | */ |
||
327 | public function notCallable() |
||
331 | |||
332 | /** |
||
333 | * @test |
||
334 | * @covers ::filter |
||
335 | * @covers ::execute |
||
336 | * @expectedException InvalidArgumentException |
||
337 | * @expectedExceptionMessage 'allowUnknowns' option was not a bool |
||
338 | */ |
||
339 | public function allowUnknownsNotBool() |
||
343 | |||
344 | /** |
||
345 | * @test |
||
346 | * @covers ::filter |
||
347 | * @covers ::execute |
||
348 | * @expectedException InvalidArgumentException |
||
349 | * @expectedExceptionMessage 'defaultRequired' option was not a bool |
||
350 | */ |
||
351 | public function defaultRequiredNotBool() |
||
355 | |||
356 | /** |
||
357 | * @test |
||
358 | * @covers ::filter |
||
359 | * @covers ::execute |
||
360 | */ |
||
361 | public function filterThrowsExceptionOnInvalidResponseType() |
||
368 | |||
369 | /** |
||
370 | * @test |
||
371 | * @covers ::filter |
||
372 | * @covers ::execute |
||
373 | * @expectedException InvalidArgumentException |
||
374 | * @expectedExceptionMessage filters for field 'boo' was not a array |
||
375 | */ |
||
376 | public function filtersNotArrayInLeftOverSpec() |
||
380 | |||
381 | /** |
||
382 | * @test |
||
383 | * @covers ::filter |
||
384 | * @covers ::execute |
||
385 | * @expectedException InvalidArgumentException |
||
386 | * @expectedExceptionMessage filters for field 'boo' was not a array |
||
387 | */ |
||
388 | public function filtersNotArrayWithInput() |
||
392 | |||
393 | /** |
||
394 | * @test |
||
395 | * @covers ::filter |
||
396 | * @covers ::execute |
||
397 | * @expectedException InvalidArgumentException |
||
398 | * @expectedExceptionMessage filter for field 'boo' was not a array |
||
399 | */ |
||
400 | public function filterNotArray() |
||
404 | |||
405 | /** |
||
406 | * @test |
||
407 | * @covers ::filter |
||
408 | * @covers ::execute |
||
409 | * @expectedException InvalidArgumentException |
||
410 | * @expectedExceptionMessage 'required' for field 'boo' was not a bool |
||
411 | */ |
||
412 | public function requiredNotBool() |
||
416 | |||
417 | /** |
||
418 | * @test |
||
419 | * @covers ::registerAlias |
||
420 | * @expectedException InvalidArgumentException |
||
421 | * @expectedExceptionMessage $alias was not a string or int |
||
422 | */ |
||
423 | public function registerAliasAliasNotString() |
||
427 | |||
428 | /** |
||
429 | * @test |
||
430 | * @covers ::registerAlias |
||
431 | * @expectedException Exception |
||
432 | * @expectedExceptionMessage Alias 'upper' exists |
||
433 | */ |
||
434 | public function registerExistingAliasOverwriteFalse() |
||
440 | |||
441 | /** |
||
442 | * @test |
||
443 | * @covers ::registerAlias |
||
444 | */ |
||
445 | public function registerExistingAliasOverwriteTrue() |
||
451 | |||
452 | public static function failingFilter() |
||
456 | |||
457 | public static function passingFilter($value) |
||
461 | |||
462 | /** |
||
463 | * Verify behavior of filter() when 'error' is not a string value. |
||
464 | * |
||
465 | * @test |
||
466 | * @covers ::filter |
||
467 | * @covers ::execute |
||
468 | * @expectedException InvalidArgumentException |
||
469 | * @expectedExceptionMessage error for field 'fieldOne' was not a non-empty string |
||
470 | * |
||
471 | * @return void |
||
472 | */ |
||
473 | View Code Duplication | public function filterWithNonStringError() |
|
480 | |||
481 | /** |
||
482 | * Verify behavior of filter() when 'error' is an empty string. |
||
483 | * |
||
484 | * @test |
||
485 | * @covers ::filter |
||
486 | * @covers ::execute |
||
487 | * @expectedException InvalidArgumentException |
||
488 | * @expectedExceptionMessage error for field 'fieldOne' was not a non-empty string |
||
489 | * |
||
490 | * @return void |
||
491 | */ |
||
492 | View Code Duplication | public function filterWithEmptyStringError() |
|
499 | |||
500 | /** |
||
501 | * @test |
||
502 | * @covers ::ofScalars |
||
503 | */ |
||
504 | public function ofScalars() |
||
508 | |||
509 | /** |
||
510 | * @test |
||
511 | * @covers ::ofScalars |
||
512 | */ |
||
513 | public function ofScalarsChained() |
||
517 | |||
518 | /** |
||
519 | * @test |
||
520 | * @covers ::ofScalars |
||
521 | */ |
||
522 | public function ofScalarsWithMeaninglessKeys() |
||
526 | |||
527 | /** |
||
528 | * @test |
||
529 | * @covers ::ofScalars |
||
530 | */ |
||
531 | View Code Duplication | public function ofScalarsFail() |
|
548 | |||
549 | /** |
||
550 | * @test |
||
551 | * @covers ::ofArrays |
||
552 | */ |
||
553 | public function ofArrays() |
||
558 | |||
559 | /** |
||
560 | * @test |
||
561 | * @covers ::ofArrays |
||
562 | */ |
||
563 | public function ofArraysChained() |
||
569 | |||
570 | /** |
||
571 | * @test |
||
572 | * @covers ::ofArrays |
||
573 | */ |
||
574 | View Code Duplication | public function ofArraysRequiredAndUnknown() |
|
584 | |||
585 | /** |
||
586 | * @test |
||
587 | * @covers ::ofArrays |
||
588 | */ |
||
589 | public function ofArraysFail() |
||
611 | |||
612 | /** |
||
613 | * @test |
||
614 | * @covers ::ofArray |
||
615 | */ |
||
616 | public function ofArray() |
||
622 | |||
623 | /** |
||
624 | * @test |
||
625 | * @covers ::ofArray |
||
626 | */ |
||
627 | public function ofArrayChained() |
||
633 | |||
634 | /** |
||
635 | * @test |
||
636 | * @covers ::ofArray |
||
637 | */ |
||
638 | public function ofArrayRequiredSuccess() |
||
644 | |||
645 | /** |
||
646 | * @test |
||
647 | * @covers ::ofArray |
||
648 | */ |
||
649 | View Code Duplication | public function ofArrayRequiredFail() |
|
659 | |||
660 | /** |
||
661 | * @test |
||
662 | * @covers ::ofArray |
||
663 | */ |
||
664 | View Code Duplication | public function ofArrayUnknown() |
|
674 | |||
675 | /** |
||
676 | * @test |
||
677 | * @covers ::getAliases |
||
678 | */ |
||
679 | public function getAliases() |
||
688 | |||
689 | /** |
||
690 | * @test |
||
691 | * @covers ::getAliases |
||
692 | */ |
||
693 | public function getAliasesReturnsStaticValueIfNull() |
||
700 | |||
701 | /** |
||
702 | * @test |
||
703 | * @covers ::getSpecification |
||
704 | */ |
||
705 | public function getSpecification() |
||
714 | |||
715 | /** |
||
716 | * @test |
||
717 | * @covers ::withAliases |
||
718 | */ |
||
719 | View Code Duplication | public function withAliases() |
|
729 | |||
730 | /** |
||
731 | * @test |
||
732 | * @covers ::withSpecification |
||
733 | */ |
||
734 | View Code Duplication | public function withSpecification() |
|
744 | } |
||
745 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: