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 |
||
| 10 | class TEnumTypeMemberTypeTest extends TestCase |
||
| 11 | { |
||
| 12 | public function testPreserveString() |
||
| 13 | { |
||
| 14 | $foo = new TEnumTypeMemberType(); |
||
| 15 | $expected = ' string '; |
||
| 16 | $actual = $foo->preserveString($expected); |
||
| 17 | $this->assertEquals($expected, $actual); |
||
| 18 | } |
||
| 19 | |||
| 20 | public function testSetDocumentationNotOk() |
||
| 21 | { |
||
| 22 | $expected = ''; |
||
| 23 | $actual = null; |
||
| 24 | $foo = new TEnumTypeMemberType(); |
||
| 25 | $documentation = m::mock(TDocumentationType::class); |
||
| 26 | $documentation->shouldReceive('isOK')->andReturn(false)->once(); |
||
| 27 | |||
| 28 | try { |
||
| 29 | $foo->setDocumentation($documentation); |
||
| 30 | } catch (\InvalidArgumentException $e) { |
||
| 31 | $actual = $e->getMessage(); |
||
| 32 | } |
||
| 33 | $this->assertEquals($expected, $actual); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function testIsExtensibilityElementOKNoGoodnik() |
||
| 37 | { |
||
| 38 | $expected = ''; |
||
| 39 | $actual = null; |
||
| 40 | $foo = new TEnumTypeMemberType(); |
||
| 41 | $documentation = m::mock(TDocumentationType::class); |
||
| 42 | $documentation->shouldReceive('isOK')->andReturn(true, false)->twice(); |
||
| 43 | |||
| 44 | $foo->setDocumentation($documentation); |
||
| 45 | $this->assertFalse($foo->isExtensibilityElementOK($actual)); |
||
| 46 | $this->assertEquals($expected, $actual); |
||
| 47 | } |
||
| 48 | } |
||
| 49 |