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 |
||
| 20 | class TransactionHandlerTest extends \PHPUnit\Framework\TestCase |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Transaction handler to test. |
||
| 24 | * |
||
| 25 | * @var \eZ\Publish\Core\Persistence\Legacy\TransactionHandler |
||
| 26 | */ |
||
| 27 | protected $transactionHandler; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler|\PHPUnit\Framework\MockObject\MockObject |
||
| 31 | */ |
||
| 32 | protected $dbHandlerMock; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var \eZ\Publish\SPI\Persistence\Content\Type\Handler|\PHPUnit\Framework\MockObject\MockObject |
||
| 36 | */ |
||
| 37 | protected $contentTypeHandlerMock; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler|\PHPUnit\Framework\MockObject\MockObject |
||
| 41 | */ |
||
| 42 | protected $languageHandlerMock; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::beginTransaction |
||
| 46 | */ |
||
| 47 | public function testBeginTransaction() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::commit |
||
| 65 | */ |
||
| 66 | public function testCommit() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::commit |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function testCommitException() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::rollback |
||
| 107 | */ |
||
| 108 | public function testRollback() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::rollback |
||
| 126 | */ |
||
| 127 | View Code Duplication | public function testRollbackException() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Returns a mock object for the Content Gateway. |
||
| 149 | * |
||
| 150 | * @return \eZ\Publish\Core\Persistence\Legacy\TransactionHandler |
||
| 151 | */ |
||
| 152 | protected function getTransactionHandler() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns a mock object for the Content Gateway. |
||
| 167 | * |
||
| 168 | * @return \eZ\Publish\Core\Persistence\Database\DatabaseHandler|\PHPUnit\Framework\MockObject\MockObject |
||
| 169 | */ |
||
| 170 | protected function getDatabaseHandlerMock() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Returns a mock object for the Content Type Handler. |
||
| 181 | * |
||
| 182 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler|\PHPUnit\Framework\MockObject\MockObject |
||
| 183 | */ |
||
| 184 | protected function getContentTypeHandlerMock() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Returns a mock object for the Content Language Gateway. |
||
| 195 | * |
||
| 196 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler|\PHPUnit\Framework\MockObject\MockObject |
||
| 197 | */ |
||
| 198 | protected function getLanguageHandlerMock() |
||
| 206 | } |
||
| 207 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.