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 |
||
| 12 | class CmsHelperTest extends \PHPUnit_Framework_TestCase |
||
| 13 | { |
||
| 14 | public function testHandle() |
||
| 15 | { |
||
| 16 | $observer = $this->prophesize('\JaySDe\HandlebarsBundle\Helper\TranslationHelper'); |
||
| 17 | $observer->handle('test', [])->willReturn('lorem ipsum')->shouldBeCalled(); |
||
| 18 | $helper = new CmsHelper($observer->reveal()); |
||
| 19 | |||
| 20 | $test = $helper->handle('test', []); |
||
| 21 | $this->assertSame('lorem ipsum', $test); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function testHandleWithOptions() |
||
| 25 | { |
||
| 26 | $observer = $this->prophesize('\JaySDe\HandlebarsBundle\Helper\TranslationHelper'); |
||
| 27 | $observer->handle('test', ['foo' => 'bar'])->willReturn('lorem ipsum')->shouldBeCalled(); |
||
| 28 | $helper = new CmsHelper($observer->reveal()); |
||
| 29 | |||
| 30 | $test = $helper->handle('test', ['hash' => ['foo' => 'bar']]); |
||
| 31 | $this->assertSame('lorem ipsum', $test); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function testHandleWithBundle() |
||
| 35 | { |
||
| 36 | $observer = $this->prophesize('\JaySDe\HandlebarsBundle\Helper\TranslationHelper'); |
||
| 37 | $observer->handle('foo:test', [])->willReturn('lorem ipsum')->shouldBeCalled(); |
||
| 38 | $helper = new CmsHelper($observer->reveal()); |
||
| 39 | |||
| 40 | $test = $helper->handle('test', ['hash' => ['bundle' => 'foo']]); |
||
| 41 | $this->assertSame('lorem ipsum', $test); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function testHandleWithOptionsAndBundle() |
||
| 45 | { |
||
| 46 | $observer = $this->prophesize('\JaySDe\HandlebarsBundle\Helper\TranslationHelper'); |
||
| 47 | $observer->handle('foo:test', ['foo' => 'bar'])->willReturn('lorem ipsum')->shouldBeCalled(); |
||
| 48 | $helper = new CmsHelper($observer->reveal()); |
||
| 49 | |||
| 50 | $test = $helper->handle('test', ['hash' => ['foo' => 'bar', 'bundle' => 'foo']]); |
||
| 51 | $this->assertSame('lorem ipsum', $test); |
||
| 52 | } |
||
| 53 | } |
||
| 54 |