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 |
||
19 | final class FiltererTest extends TestCase |
||
20 | { |
||
21 | public function setUp() |
||
25 | |||
26 | /** |
||
27 | * @test |
||
28 | * @covers ::filter |
||
29 | * @covers ::execute |
||
30 | * @dataProvider provideValidFilterData |
||
31 | * |
||
32 | * @param array $spec The filter specification to be use. |
||
33 | * @param array $input The input to be filtered. |
||
34 | * @param array $options The filterer options |
||
35 | * @param array $expectedResult The expected filterer result. |
||
36 | */ |
||
37 | public function filter(array $spec, array $input, array $options, array $expectedResult) |
||
41 | |||
42 | public function provideValidFilterData() : array |
||
232 | |||
233 | /** |
||
234 | * @test |
||
235 | * @covers ::filter |
||
236 | * @covers ::execute |
||
237 | */ |
||
238 | public function filterReturnsResponseType() |
||
253 | |||
254 | /** |
||
255 | * @test |
||
256 | * @covers ::filter |
||
257 | * @covers ::execute |
||
258 | */ |
||
259 | public function filterReturnsResponseTypeWithErrors() |
||
274 | |||
275 | /** |
||
276 | * @test |
||
277 | * @covers ::filter |
||
278 | * @covers ::execute |
||
279 | * @covers ::setFilterAliases |
||
280 | */ |
||
281 | public function filterCustomShortNamePass() |
||
287 | |||
288 | /** |
||
289 | * @test |
||
290 | * @covers ::filter |
||
291 | * @covers ::execute |
||
292 | * @covers ::setFilterAliases |
||
293 | * @covers ::getFilterAliases |
||
294 | */ |
||
295 | public function filterGetSetKnownFilters() |
||
301 | |||
302 | /** |
||
303 | * @test |
||
304 | * @covers ::setFilterAliases |
||
305 | */ |
||
306 | public function setBadFilterAliases() |
||
320 | |||
321 | /** |
||
322 | * @test |
||
323 | * @covers ::filter |
||
324 | * @covers ::execute |
||
325 | * @expectedException Exception |
||
326 | * @expectedExceptionMessage Function 'boo' for field 'foo' is not callable |
||
327 | */ |
||
328 | public function notCallable() |
||
332 | |||
333 | /** |
||
334 | * @test |
||
335 | * @covers ::filter |
||
336 | * @covers ::execute |
||
337 | * @expectedException InvalidArgumentException |
||
338 | * @expectedExceptionMessage 'allowUnknowns' option was not a bool |
||
339 | */ |
||
340 | public function allowUnknownsNotBool() |
||
344 | |||
345 | /** |
||
346 | * @test |
||
347 | * @covers ::filter |
||
348 | * @covers ::execute |
||
349 | * @expectedException InvalidArgumentException |
||
350 | * @expectedExceptionMessage 'defaultRequired' option was not a bool |
||
351 | */ |
||
352 | public function defaultRequiredNotBool() |
||
356 | |||
357 | /** |
||
358 | * @test |
||
359 | * @covers ::filter |
||
360 | * @covers ::execute |
||
361 | */ |
||
362 | public function filterThrowsExceptionOnInvalidResponseType() |
||
369 | |||
370 | /** |
||
371 | * @test |
||
372 | * @covers ::filter |
||
373 | * @covers ::execute |
||
374 | * @expectedException InvalidArgumentException |
||
375 | * @expectedExceptionMessage filters for field 'boo' was not a array |
||
376 | */ |
||
377 | public function filtersNotArrayInLeftOverSpec() |
||
381 | |||
382 | /** |
||
383 | * @test |
||
384 | * @covers ::filter |
||
385 | * @covers ::execute |
||
386 | * @expectedException InvalidArgumentException |
||
387 | * @expectedExceptionMessage filters for field 'boo' was not a array |
||
388 | */ |
||
389 | public function filtersNotArrayWithInput() |
||
393 | |||
394 | /** |
||
395 | * @test |
||
396 | * @covers ::filter |
||
397 | * @covers ::execute |
||
398 | * @expectedException InvalidArgumentException |
||
399 | * @expectedExceptionMessage filter for field 'boo' was not a array |
||
400 | */ |
||
401 | public function filterNotArray() |
||
405 | |||
406 | /** |
||
407 | * @test |
||
408 | * @covers ::filter |
||
409 | * @covers ::execute |
||
410 | * @expectedException InvalidArgumentException |
||
411 | * @expectedExceptionMessage 'required' for field 'boo' was not a bool |
||
412 | */ |
||
413 | public function requiredNotBool() |
||
417 | |||
418 | /** |
||
419 | * @test |
||
420 | * @covers ::registerAlias |
||
421 | * @expectedException InvalidArgumentException |
||
422 | * @expectedExceptionMessage $alias was not a string or int |
||
423 | */ |
||
424 | public function registerAliasAliasNotString() |
||
428 | |||
429 | /** |
||
430 | * @test |
||
431 | * @covers ::registerAlias |
||
432 | * @expectedException Exception |
||
433 | * @expectedExceptionMessage Alias 'upper' exists |
||
434 | */ |
||
435 | public function registerExistingAliasOverwriteFalse() |
||
441 | |||
442 | /** |
||
443 | * @test |
||
444 | * @covers ::registerAlias |
||
445 | */ |
||
446 | public function registerExistingAliasOverwriteTrue() |
||
452 | |||
453 | public static function failingFilter() |
||
457 | |||
458 | public static function passingFilter($value) |
||
462 | |||
463 | /** |
||
464 | * Verify behavior of filter() when 'error' is not a string value. |
||
465 | * |
||
466 | * @test |
||
467 | * @covers ::filter |
||
468 | * @covers ::execute |
||
469 | * @expectedException InvalidArgumentException |
||
470 | * @expectedExceptionMessage error for field 'fieldOne' was not a non-empty string |
||
471 | * |
||
472 | * @return void |
||
473 | */ |
||
474 | View Code Duplication | public function filterWithNonStringError() |
|
481 | |||
482 | /** |
||
483 | * Verify behavior of filter() when 'error' is an empty string. |
||
484 | * |
||
485 | * @test |
||
486 | * @covers ::filter |
||
487 | * @covers ::execute |
||
488 | * @expectedException InvalidArgumentException |
||
489 | * @expectedExceptionMessage error for field 'fieldOne' was not a non-empty string |
||
490 | * |
||
491 | * @return void |
||
492 | */ |
||
493 | View Code Duplication | public function filterWithEmptyStringError() |
|
500 | |||
501 | /** |
||
502 | * @test |
||
503 | * @covers ::ofScalars |
||
504 | */ |
||
505 | public function ofScalars() |
||
509 | |||
510 | /** |
||
511 | * @test |
||
512 | * @covers ::ofScalars |
||
513 | */ |
||
514 | public function ofScalarsChained() |
||
518 | |||
519 | /** |
||
520 | * @test |
||
521 | * @covers ::ofScalars |
||
522 | */ |
||
523 | public function ofScalarsWithMeaninglessKeys() |
||
527 | |||
528 | /** |
||
529 | * @test |
||
530 | * @covers ::ofScalars |
||
531 | */ |
||
532 | View Code Duplication | public function ofScalarsFail() |
|
549 | |||
550 | /** |
||
551 | * @test |
||
552 | * @covers ::ofArrays |
||
553 | */ |
||
554 | public function ofArrays() |
||
559 | |||
560 | /** |
||
561 | * @test |
||
562 | * @covers ::ofArrays |
||
563 | */ |
||
564 | public function ofArraysChained() |
||
570 | |||
571 | /** |
||
572 | * @test |
||
573 | * @covers ::ofArrays |
||
574 | */ |
||
575 | View Code Duplication | public function ofArraysRequiredAndUnknown() |
|
585 | |||
586 | /** |
||
587 | * @test |
||
588 | * @covers ::ofArrays |
||
589 | */ |
||
590 | public function ofArraysFail() |
||
612 | |||
613 | /** |
||
614 | * @test |
||
615 | * @covers ::ofArray |
||
616 | */ |
||
617 | public function ofArray() |
||
623 | |||
624 | /** |
||
625 | * @test |
||
626 | * @covers ::ofArray |
||
627 | */ |
||
628 | public function ofArrayChained() |
||
634 | |||
635 | /** |
||
636 | * @test |
||
637 | * @covers ::ofArray |
||
638 | */ |
||
639 | public function ofArrayRequiredSuccess() |
||
645 | |||
646 | /** |
||
647 | * @test |
||
648 | * @covers ::ofArray |
||
649 | */ |
||
650 | View Code Duplication | public function ofArrayRequiredFail() |
|
660 | |||
661 | /** |
||
662 | * @test |
||
663 | * @covers ::ofArray |
||
664 | */ |
||
665 | View Code Duplication | public function ofArrayUnknown() |
|
675 | |||
676 | /** |
||
677 | * @test |
||
678 | * @covers ::__invoke |
||
679 | * @dataProvider provideInvoke |
||
680 | * |
||
681 | * @param array $filter |
||
682 | * @param array $options |
||
683 | * @param array|null $filterAliases |
||
684 | * @param mixed $value |
||
685 | * @param array $expected |
||
686 | */ |
||
687 | public function invoke(array $filter, array $options, $filterAliases, $value, array $expected) |
||
694 | |||
695 | /** |
||
696 | * @returns array |
||
697 | */ |
||
698 | public function provideInvoke() : array |
||
724 | |||
725 | /** |
||
726 | * @test |
||
727 | * @covers ::__invoke |
||
728 | */ |
||
729 | public function invokeThrowsFilterException() |
||
741 | } |
||
742 |
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: