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 |
||
| 26 | final class DoubleUtilityTest extends PHPUnit_Framework_TestCase { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Tests the parseString() method. |
||
| 30 | * |
||
| 31 | * @return void |
||
| 32 | */ |
||
| 33 | public function testParseString() { |
||
| 34 | |||
| 35 | try { |
||
| 36 | DoubleUtility::parseString("exception"); |
||
| 37 | } catch (Exception $ex) { |
||
| 38 | $this->assertInstanceOf(DoubleArgumentException::class, $ex); |
||
| 39 | $this->assertEquals("The argument \"exception\" is not a double", $ex->getMessage()); |
||
| 40 | } |
||
| 41 | |||
| 42 | try { |
||
| 43 | DoubleUtility::parseString("1A"); |
||
| 44 | } catch (Exception $ex) { |
||
| 45 | $this->assertInstanceOf(DoubleArgumentException::class, $ex); |
||
| 46 | $this->assertEquals("The argument \"1A\" is not a double", $ex->getMessage()); |
||
| 47 | } |
||
| 48 | |||
| 49 | $this->assertEquals(null, DoubleUtility::parseString(null)); |
||
| 50 | $this->assertEquals(1.0, DoubleUtility::parseString("1")); |
||
| 51 | $this->assertEquals(1.0, DoubleUtility::parseString("1.")); |
||
| 52 | $this->assertEquals(1.0, DoubleUtility::parseString("1.0")); |
||
| 53 | } |
||
| 54 | |||
| 55 | } |
||
| 56 |