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 |
||
| 9 | View Code Duplication | class xsFloatTest extends \PHPUnit_Framework_TestCase |
|
|
|
|||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @dataProvider testxsFloatValidDataProvider |
||
| 13 | */ |
||
| 14 | public function testxsFloatValid($input, $message) |
||
| 15 | { |
||
| 16 | try { |
||
| 17 | $d = new xsFloat($input); |
||
| 18 | $s = (string)$d; |
||
| 19 | } catch (\Exception $e) { |
||
| 20 | $this->fail($message . ' with Exception ' . $e->getMessage()); |
||
| 21 | } |
||
| 22 | } |
||
| 23 | |||
| 24 | public function testxsFloatValidDataProvider() |
||
| 25 | { |
||
| 26 | return array( |
||
| 27 | array('-3E2', 'any value valid for decimal is also valid for float'), |
||
| 28 | array('4268.22752E11', 'any value valid for decimal is also valid for float'), |
||
| 29 | array('+24.3e-3 ', 'any value valid for decimal is also valid for float'), |
||
| 30 | array('12', 'any value valid for decimal is also valid for float'), |
||
| 31 | array('+3.5 ', 'any value valid for decimal is also valid for float'), |
||
| 32 | array('-INF ', 'negative infinity'), |
||
| 33 | array('-0 ', 'Zero'), |
||
| 34 | array('NaN', 'Not a Number'), |
||
| 35 | |||
| 36 | ); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @dataProvider testxsFloatInvalidDataProvider |
||
| 41 | */ |
||
| 42 | public function testxsFloatInvalid($input, $message) |
||
| 52 | |||
| 53 | public function testxsFloatInvalidDataProvider() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Sets up the fixture, for example, opens a network connection. |
||
| 65 | * This method is called before a test is executed. |
||
| 66 | */ |
||
| 67 | protected function setUp() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Tears down the fixture, for example, closes a network connection. |
||
| 74 | * This method is called after a test is executed. |
||
| 75 | */ |
||
| 76 | protected function tearDown() |
||
| 80 | } |
||
| 81 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.