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 | class PhpTest extends \Codeception\TestCase\Test |
||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var UnitTester |
||
| 14 | */ |
||
| 15 | protected $tester; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * NOTE: Do not remove, part of test |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | public $value = 'property value'; |
||
| 22 | |||
| 23 | // tests |
||
| 24 | public function testIfWillPassVariableToView() |
||
| 25 | { |
||
| 26 | $var = 'New Variable'; |
||
| 27 | |||
| 28 | $view = new MiniView($this); |
||
| 29 | |||
| 30 | $result = $view->render('passVariable', ['var' => $var], true); |
||
| 31 | |||
| 32 | $this->assertSame($var, $result); |
||
| 33 | } |
||
| 34 | |||
| 35 | // tests |
||
| 36 | public function testIfWillPassVariableToViewWithRendererDetection() |
||
| 37 | { |
||
| 38 | $var = 'New Variable'; |
||
| 39 | |||
| 40 | $view = new MiniView($this); |
||
| 41 | $view->setRenderer(new PhpRenderer()); |
||
| 42 | $result = $view->render('passVariable.php', ['var' => $var], true); |
||
| 43 | |||
| 44 | $this->assertSame($var, $result); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testIfWillForwardMethodToOwner() |
||
| 48 | { |
||
| 49 | $view = new MiniView($this); |
||
| 50 | |||
| 51 | $result = $view->render('forwardMethod', [], true); |
||
| 52 | |||
| 53 | $this->assertSame($this->getValue(), $result); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function testIfWillForwardPropertyToOwner() |
||
| 57 | { |
||
| 58 | $view = new MiniView($this); |
||
| 59 | |||
| 60 | $result = $view->render('forwardProperty', [], true); |
||
| 61 | |||
| 62 | $this->assertSame($this->value, $result); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function getValue() |
||
| 66 | { |
||
| 67 | return 'my value from method'; |
||
| 68 | } |
||
| 69 | |||
| 70 | } |
||
| 71 |