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 |
||
| 11 | class TestCase extends PHPUnit_Framework_TestCase |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Returns a test double for the specified class. |
||
| 15 | * |
||
| 16 | * @param string $originalClassName |
||
| 17 | * |
||
| 18 | * @throws \PHPUnit_Framework_Exception |
||
| 19 | * |
||
| 20 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
| 21 | */ |
||
| 22 | protected function createMock($originalClassName) |
||
| 23 | { |
||
| 24 | if (method_exists(PHPUnit_Framework_TestCase::class, 'createMock')) { |
||
| 25 | return parent::createMock($originalClassName); |
||
| 26 | } |
||
| 27 | |||
| 28 | return $this |
||
| 29 | ->getMockBuilder($originalClassName) |
||
| 30 | ->disableOriginalConstructor() |
||
| 31 | ->disableOriginalClone() |
||
| 32 | ->disableArgumentCloning() |
||
| 33 | ->getMock(); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Returns a partial test double for the specified class. |
||
| 38 | * |
||
| 39 | * @param string $originalClassName |
||
| 40 | * @param array $methods |
||
| 41 | * |
||
| 42 | * @throws \PHPUnit_Framework_Exception |
||
| 43 | * |
||
| 44 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
| 45 | */ |
||
| 46 | protected function createPartialMock($originalClassName, array $methods) |
||
| 47 | { |
||
| 48 | if (method_exists(PHPUnit_Framework_TestCase::class, 'createPartialMock')) { |
||
| 49 | return parent::createPartialMock($originalClassName, $methods); |
||
| 50 | } |
||
| 51 | |||
| 52 | return $this |
||
| 53 | ->getMockBuilder($originalClassName) |
||
| 54 | ->disableOriginalConstructor() |
||
| 55 | ->disableOriginalClone() |
||
| 56 | ->disableArgumentCloning() |
||
| 57 | ->setMethods($methods) |
||
| 58 | ->getMock(); |
||
| 59 | } |
||
| 60 | } |
||
| 61 |