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:
| 1 | <?php |
||
| 7 | class PaginationTest extends \PHPUnit_Framework_TestCase |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var Pagination |
||
| 11 | */ |
||
| 12 | protected $object; |
||
| 13 | protected $adapter; |
||
| 14 | |||
| 15 | protected function setUp() |
||
| 22 | |||
| 23 | protected function tearDown() |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @covers Koch\Pagination\Pagination::setAdapter |
||
| 30 | */ |
||
| 31 | public function testSetAdapter() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @covers Koch\Pagination\Pagination::getAdapter |
||
| 42 | */ |
||
| 43 | public function testGetAdapter() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
||
| 50 | * @covers Koch\Pagination\Pagination::getMaxResultsPerPage |
||
| 51 | */ |
||
| 52 | public function testSetMaxResultsPerPage() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @expectedException InvalidArgumentException |
||
| 65 | * @expectedExceptionMessage There must be more than 1 MaxResultsPerPage. |
||
| 66 | */ |
||
| 67 | public function testSetMaxResultsPerPage_throwsException() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @covers Koch\Pagination\Pagination::setCurrentPage |
||
| 74 | * @covers Koch\Pagination\Pagination::getCurrentPage |
||
| 75 | */ |
||
| 76 | public function testSetCurrentPage() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @covers Koch\Pagination\Pagination::getTotalNumberOfResults |
||
| 94 | */ |
||
| 95 | public function getTotalNumberOfResults() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
||
| 109 | * @covers Koch\Pagination\Pagination::haveToPaginate |
||
| 110 | */ |
||
| 111 | public function testHaveToPaginate() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @covers Koch\Pagination\Pagination::getTotalNumberOfResults |
||
| 128 | * @covers Koch\Pagination\Pagination::setTotalNumberOfResults |
||
| 129 | */ |
||
| 130 | public function testSetTotalNumberOfResults() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @covers Koch\Pagination\Pagination::getNumberOfPages |
||
| 140 | * @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
||
| 141 | * @covers Koch\Pagination\Pagination::getLastPage |
||
| 142 | */ |
||
| 143 | public function testGetNumberOfPages() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
||
| 155 | * @covers Koch\Pagination\Pagination::getCurrentPageResults |
||
| 156 | * @covers Koch\Pagination\Pagination::getCurrentPage |
||
| 157 | */ |
||
| 158 | public function testGetCurrentPageResults() |
||
| 159 | { |
||
| 160 | $returnValues = [ |
||
| 161 | ['foo' => 'bar', 'bar' => 'foo'], |
||
| 162 | ['fanta', 'reiner', 'kristall', 'weizen'], |
||
| 163 | ]; |
||
| 164 | |||
| 165 | $this->adapter->expects($this->once())->method('getSlice') |
||
| 166 | ->with($this->equalTo(20), $this->equalTo(10)) |
||
| 167 | ->will($this->returnValue($returnValues[0])); |
||
| 168 | |||
| 169 | $this->object->setMaxResultsPerPage(10); |
||
| 170 | $this->object->setCurrentPage(3, true); |
||
| 171 | $this->assertSame($returnValues[0], $this->object->getCurrentPageResults()); |
||
| 172 | |||
| 173 | // cached |
||
| 174 | $this->assertSame($returnValues[0], $this->object->getCurrentPageResults()); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
||
| 179 | * @covers Koch\Pagination\Pagination::getCurrentPage |
||
| 180 | * @covers Koch\Pagination\Pagination::hasPreviousPage |
||
| 181 | * @covers Koch\Pagination\Pagination::getPreviousPage |
||
| 182 | * @covers Koch\Pagination\Pagination::hasNextPage |
||
| 183 | * @covers Koch\Pagination\Pagination::getNextPage |
||
| 184 | */ |
||
| 185 | public function testGetPreviousPage() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @covers Koch\Pagination\Pagination::render |
||
| 228 | * @covers Koch\Pagination\Pagination::setAdapter |
||
| 229 | * @covers Koch\Pagination\Pagination::setMaxResultsPerPage |
||
| 230 | */ |
||
| 231 | public function testRender() |
||
| 256 | } |
||
| 257 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.