Complex classes like PagerfantaTest 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 PagerfantaTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class PagerfantaTest extends TestCase |
||
| 24 | { |
||
| 25 | private $adapter; |
||
| 26 | /** |
||
| 27 | * @var Pagerfanta |
||
| 28 | */ |
||
| 29 | private $pagerfanta; |
||
| 30 | |||
| 31 | protected function setUp() |
||
| 36 | |||
| 37 | private function setAdapterNbResultsAny($nbResults) |
||
| 41 | |||
| 42 | private function setAdapterNbResultsOnce($nbResults) |
||
| 46 | |||
| 47 | private function setAdapterNbResults($expects, $nbResults) |
||
| 54 | |||
| 55 | public function testGetAdapterShouldReturnTheAdapter() |
||
| 59 | |||
| 60 | public function testGetAllowOutOfRangePagesShouldBeFalseByDefault() |
||
| 64 | |||
| 65 | public function testSetAllowOutOfRangePagesShouldSetTrue() |
||
| 70 | |||
| 71 | public function testSetAllowOutOfRangePagesShouldSetFalse() |
||
| 76 | |||
| 77 | public function testSetAllowOutOfRangePagesShouldReturnThePagerfanta() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @expectedException \Pagerfanta\Exception\NotBooleanException |
||
| 84 | * @dataProvider notBooleanProvider |
||
| 85 | */ |
||
| 86 | public function testSetAllowOutOfRangePagesShouldThrowNotBooleanExceptionWhenNotBoolean($value) |
||
| 90 | |||
| 91 | public function testGetNormalizeOutOfRangePagesShouldBeFalseByDefault() |
||
| 95 | |||
| 96 | public function testSetNormalizeOutOfRangePagesShouldSetTrue() |
||
| 101 | |||
| 102 | public function testSetNormalizeOutOfRangePagesShouldSetFalse() |
||
| 107 | |||
| 108 | public function testSetNormalizeOutOfRangePagesShouldReturnThePagerfanta() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @expectedException \Pagerfanta\Exception\NotBooleanException |
||
| 115 | * @dataProvider notBooleanProvider |
||
| 116 | */ |
||
| 117 | public function testSetNormalizeOutOfRangePagesShouldThrowNotBooleanExceptionWhenNotBoolean($value) |
||
| 121 | |||
| 122 | public function notBooleanProvider() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @dataProvider setMaxPerPageShouldSetAnIntegerProvider |
||
| 133 | */ |
||
| 134 | public function testSetMaxPerPageShouldSetAnInteger($maxPerPage) |
||
| 140 | |||
| 141 | public function setMaxPerPageShouldSetAnIntegerProvider() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @dataProvider setMaxPerPageShouldConvertStringsToIntegersProvider |
||
| 152 | */ |
||
| 153 | public function testSetMaxPerPageShouldConvertStringsToIntegers($maxPerPage) |
||
| 158 | |||
| 159 | public function setMaxPerPageShouldConvertStringsToIntegersProvider() |
||
| 167 | |||
| 168 | public function testSetMaxPerPageShouldReturnThePagerfanta() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @expectedException \Pagerfanta\Exception\NotIntegerMaxPerPageException |
||
| 175 | * @dataProvider setMaxPerPageShouldThrowExceptionWhenInvalidProvider |
||
| 176 | */ |
||
| 177 | public function testSetMaxPerPageShouldThrowExceptionWhenInvalid($maxPerPage) |
||
| 181 | |||
| 182 | public function setMaxPerPageShouldThrowExceptionWhenInvalidProvider() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @expectedException \Pagerfanta\Exception\LessThan1MaxPerPageException |
||
| 194 | * @dataProvider setMaxPerPageShouldThrowExceptionWhenLessThan1Provider |
||
| 195 | */ |
||
| 196 | public function testSetMaxPerPageShouldThrowExceptionWhenLessThan1($maxPerPage) |
||
| 200 | |||
| 201 | public function setMaxPerPageShouldThrowExceptionWhenLessThan1Provider() |
||
| 208 | |||
| 209 | public function testSetMaxPerPageShouldResetCurrentPageResults() |
||
| 217 | |||
| 218 | public function testSetMaxPerPageShouldResetNbResults() |
||
| 226 | |||
| 227 | public function testSetMaxPerPageShouldResetNbPages() |
||
| 235 | |||
| 236 | private function prepareForResetNbResults() |
||
| 249 | |||
| 250 | public function testGetNbResultsShouldReturnTheNbResultsFromTheAdapter() |
||
| 256 | |||
| 257 | public function testGetNbResultsShouldCacheTheNbResultsFromTheAdapter() |
||
| 264 | |||
| 265 | public function testGetNbPagesShouldCalculateTheNumberOfPages() |
||
| 272 | |||
| 273 | public function testGetNbPagesShouldRoundToUp() |
||
| 280 | |||
| 281 | public function testGetNbPagesShouldReturn1WhenThereAreNoResults() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @dataProvider setCurrentPageShouldSetAnIntegerProvider |
||
| 290 | */ |
||
| 291 | public function testSetCurrentPageShouldSetAnInteger($currentPage) |
||
| 299 | |||
| 300 | public function setCurrentPageShouldSetAnIntegerProvider() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @dataProvider setCurrentPageShouldConvertStringsToIntegersProvider |
||
| 311 | */ |
||
| 312 | public function testSetCurrentPageShouldConvertStringsToIntegers($currentPage) |
||
| 320 | |||
| 321 | public function setCurrentPageShouldConvertStringsToIntegersProvider() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @expectedException Pagerfanta\Exception\NotIntegerCurrentPageException |
||
| 332 | * @dataProvider setCurrentPageShouldThrowExceptionWhenInvalidProvider |
||
| 333 | */ |
||
| 334 | public function testSetCurrentPageShouldThrowExceptionWhenInvalid($currentPage) |
||
| 338 | |||
| 339 | public function setCurrentPageShouldThrowExceptionWhenInvalidProvider() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @expectedException Pagerfanta\Exception\LessThan1CurrentPageException |
||
| 351 | * @dataProvider setCurrentPageShouldThrowExceptionWhenLessThan1Provider |
||
| 352 | */ |
||
| 353 | public function testCurrentPagePageShouldThrowExceptionWhenLessThan1($currentPage) |
||
| 357 | |||
| 358 | public function setCurrentPageShouldThrowExceptionWhenLessThan1Provider() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @expectedException Pagerfanta\Exception\OutOfRangeCurrentPageException |
||
| 368 | */ |
||
| 369 | public function testSetCurrentPageShouldThrowExceptionWhenThePageIsOutOfRange() |
||
| 375 | |||
| 376 | public function testSetCurrentPageShouldNotThrowExceptionWhenIndicatingAllowOurOfRangePages() |
||
| 385 | |||
| 386 | public function testSetCurrentPageShouldNotThrowExceptionWhenIndicatingAllowOurOfRangePagesWithOldBooleanArguments() |
||
| 394 | |||
| 395 | public function testSetCurrentPageShouldReturnThePagerfanta() |
||
| 402 | |||
| 403 | public function testSetCurrentPageShouldNormalizePageWhenOutOfRangePageAndIndicatingNormalizeOutOfRangePages() |
||
| 413 | |||
| 414 | public function testSetCurrentPageShouldNormalizePageWhenOutOfRangePageAndIndicatingNormalizeOutOfRangePagesWithDeprecatedBooleansArguments() |
||
| 422 | |||
| 423 | public function testSetCurrentPageShouldResetCurrentPageResults() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @dataProvider testGetCurrentPageResultsShouldReturnASliceFromTheAdapterDependingOnTheCurrentPageAndMaxPerPageProvider |
||
| 434 | */ |
||
| 435 | public function testGetCurrentPageResultsShouldReturnASliceFromTheAdapterDependingOnTheCurrentPageAndMaxPerPage($maxPerPage, $currentPage, $offset) |
||
| 451 | |||
| 452 | public function testGetCurrentPageResultsShouldReturnASliceFromTheAdapterDependingOnTheCurrentPageAndMaxPerPageProvider() |
||
| 461 | |||
| 462 | public function testGetCurrentPageResultsShouldCacheTheResults() |
||
| 478 | |||
| 479 | public function testGetCurrentPageOffsetStart() |
||
| 487 | |||
| 488 | public function testGetCurrentPageOffsetStartWith0NbResults() |
||
| 496 | |||
| 497 | public function testGetCurrentPageOffsetEnd() |
||
| 505 | |||
| 506 | public function testGetCurrentPageOffsetEndOnEndPage() |
||
| 514 | |||
| 515 | public function testHaveToPaginateReturnsTrueWhenTheNumberOfResultsIsGreaterThanTheMaxPerPage() |
||
| 522 | |||
| 523 | public function testHaveToPaginateReturnsFalseWhenTheNumberOfResultsIsEqualToMaxPerPage() |
||
| 530 | |||
| 531 | public function testHaveToPaginateReturnsFalseWhenTheNumberOfResultsIsLessThanMaxPerPage() |
||
| 538 | |||
| 539 | public function testHasPreviousPageShouldReturnTrueWhenTheCurrentPageIsGreaterThan1() |
||
| 548 | |||
| 549 | public function testHasPreviousPageShouldReturnFalseWhenTheCurrentPageIs1() |
||
| 556 | |||
| 557 | public function testGetPreviousPageShouldReturnThePreviousPage() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @expectedException Pagerfanta\Exception\LogicException |
||
| 570 | */ |
||
| 571 | public function testGetPreviousPageShouldThrowALogicExceptionIfThereIsNoPreviousPage() |
||
| 579 | |||
| 580 | public function testHasNextPageShouldReturnTrueIfTheCurrentPageIsNotTheLast() |
||
| 590 | |||
| 591 | public function testHasNextPageShouldReturnFalseIfTheCurrentPageIsTheLast() |
||
| 599 | |||
| 600 | public function testGetNextPageShouldReturnTheNextPage() |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @expectedException Pagerfanta\Exception\LogicException |
||
| 613 | */ |
||
| 614 | public function testGetNextPageShouldThrowALogicExceptionIfTheCurrentPageIsTheLast() |
||
| 622 | |||
| 623 | public function testCountShouldReturnNbResults() |
||
| 629 | |||
| 630 | public function testPagerfantaShouldImplementCountableInterface() |
||
| 634 | |||
| 635 | public function testGetIteratorShouldReturnCurrentPageResultsIfItIsAnIterator() |
||
| 643 | |||
| 644 | public function testGetIteratorShouldReturnTheIteratorOfCurrentPageResultsIfItIsAnIteratorAggregate() |
||
| 652 | |||
| 653 | public function testGetIteratorShouldReturnAnArrayIteratorIfCurrentPageResultsIsAnArray() |
||
| 661 | |||
| 662 | public function testJsonSerializeShouldReturnAnArrayOfCurrentPageResultsIfItIsAnIterator() |
||
| 670 | |||
| 671 | public function testJsonSerializeShouldReturnAnArrayOfCurrentPageResultsIfItIsAnIteratorAggregate() |
||
| 679 | |||
| 680 | public function testJsonSerializeShouldReturnAnArrayOfCurrentPageResultsIfCurrentPageResultsIsAnArray() |
||
| 688 | |||
| 689 | public function testJsonSerializeIsUsedOnJsonEncode() |
||
| 697 | |||
| 698 | private function setAdapterGetSlice($currentPageResults) |
||
| 705 | |||
| 706 | public function testPagerfantaShouldImplementIteratorAggregateInterface() |
||
| 710 | |||
| 711 | private function assertResetCurrentPageResults($callback) |
||
| 732 | |||
| 733 | public function testGetPageNumberForItemShouldReturnTheGoodPage() |
||
| 740 | |||
| 741 | /** |
||
| 742 | * @expectedException Pagerfanta\Exception\NotIntegerException |
||
| 743 | */ |
||
| 744 | public function testGetPageNumberForItemShouldThrowANotIntegerItemExceptionIfTheItemIsNotAnInteger() |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @expectedException \OutOfBoundsException |
||
| 753 | */ |
||
| 754 | public function testGetPageNumberForItemShouldThrowALogicExceptionIfTheItemIsMoreThanNbPage() |
||
| 760 | } |
||
| 761 |
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: