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 |
||
| 19 | class Rule extends atoum |
||
| 20 | { |
||
| 21 | public function testAllowAllAsDefault() |
||
| 22 | { |
||
| 23 | $this |
||
| 24 | ->given($sut = new SUT('')) |
||
| 25 | ->when($match = $sut->match('/some/url')) |
||
| 26 | ->then |
||
| 27 | ->boolean($match) |
||
| 28 | ->isTrue(); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function testAllow() |
||
| 32 | { |
||
| 33 | $this |
||
| 34 | ->given( |
||
| 35 | $sut = new SUT(''), |
||
| 36 | $sut->allow('/foo/bar') |
||
| 37 | ) |
||
| 38 | ->when($match = $sut->match('/foo/bar')) |
||
| 39 | ->then |
||
| 40 | ->boolean($match) |
||
| 41 | ->isTrue(); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function testDisallow() |
||
| 45 | { |
||
| 46 | $this |
||
| 47 | ->given( |
||
| 48 | $sut = new SUT(''), |
||
| 49 | $sut->disallow('/foo/bar'), |
||
| 50 | $sut->allow('/foo/bar/baz$') |
||
| 51 | ) |
||
| 52 | ->when($match = $sut->match('/foo/bar')) |
||
| 53 | ->then |
||
| 54 | ->boolean($match) |
||
| 55 | ->isFalse() |
||
| 56 | ->when($match = $sut->match('/foo/bar/baz/')) |
||
| 57 | ->then |
||
| 58 | ->boolean($match) |
||
| 59 | ->isFalse() |
||
| 60 | ->when($match = $sut->match('/foo/bar/baz')) |
||
| 61 | ->then |
||
| 62 | ->boolean($match) |
||
| 63 | ->isTrue(); |
||
| 64 | } |
||
| 65 | |||
| 66 | public function testUserAgent() |
||
| 67 | { |
||
| 68 | $this |
||
| 69 | ->given($sut = new SUT('UA')) |
||
| 70 | ->then |
||
| 71 | ->string($sut->getUserAgent()) |
||
| 72 | ->isEqualTo('UA'); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function testMultipleExpression() |
||
| 76 | { |
||
| 77 | $this |
||
| 78 | ->given( |
||
| 79 | $sut = new SUT(''), |
||
| 80 | $sut->disallow('/foo/bar'), |
||
| 81 | $sut->allow('/foo/bar/baz$'), |
||
| 82 | $sut->allow('/foo$') |
||
| 83 | ) |
||
| 84 | ->when($match = $sut->match('/foo/bar')) |
||
| 85 | ->then |
||
| 86 | ->boolean($match) |
||
| 87 | ->isFalse() |
||
| 88 | ->when($match = $sut->match('/foo/bar/baz/')) |
||
| 89 | ->then |
||
| 90 | ->boolean($match) |
||
| 91 | ->isFalse() |
||
| 92 | ->when($match = $sut->match('/foo/bar/baz')) |
||
| 93 | ->then |
||
| 94 | ->boolean($match) |
||
| 95 | ->isTrue(); |
||
| 96 | } |
||
| 97 | } |
||
| 98 |