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 Expression extends atoum |
||
20 | { |
||
21 | public function patternDataProvider() |
||
22 | { |
||
23 | return [ |
||
24 | ['/page-1.html', '\/page\-1\.html.*'], |
||
25 | ['/page-1.html$', '\/page\-1\.html'], |
||
26 | ['/page*/dossier.html', '\/page.*\/dossier\.html.*'], |
||
27 | ['/page*/dossier.html$', '\/page.*\/dossier\.html'], |
||
28 | ['/page+1$', '\/page\+1'], |
||
29 | ['/page+1?query=toto$', '\/page\+1\?query\=toto'], |
||
30 | ['/page+1?query[0]=foo&query[1]=bar$', '\/page\+1\?query\[0\]\=foo&query\[1\]\=bar'], |
||
31 | ]; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @dataProvider patternDataProvider |
||
36 | */ |
||
37 | public function testPattern($exp, $pattern) |
||
38 | { |
||
39 | $this |
||
40 | ->given($sut = new SUT($exp)) |
||
41 | ->then |
||
42 | ->string($sut->getRaw()) |
||
43 | ->isEqualTo($exp) |
||
44 | ->castToString($sut) |
||
45 | ->isEqualTo($pattern); |
||
46 | } |
||
47 | |||
48 | public function containsDataProvider() |
||
49 | { |
||
50 | return [ |
||
51 | ['/page-1', '/page-1/dossier.html', true], |
||
52 | ['/page-1$', '/page-1/dossier.html', false], |
||
53 | ['/page*', '/page-1/dossier.html', true], |
||
54 | ['/page*', '/page*/dossier.html', true], |
||
55 | ]; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @dataProvider containsDataProvider |
||
60 | */ |
||
61 | public function testContains($exp, $exp2, $contains) |
||
62 | { |
||
63 | $this |
||
64 | ->given( |
||
65 | $sut = new SUT($exp), |
||
66 | $sut2 = new SUT($exp2) |
||
67 | ) |
||
68 | ->then |
||
69 | ->boolean($sut->contains($sut2)) |
||
70 | ->isEqualTo($contains); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @dataProvider containsDataProvider |
||
75 | */ |
||
76 | public function testContained($exp, $exp2, $contained) |
||
77 | { |
||
78 | $this |
||
79 | ->given( |
||
80 | $sut = new SUT($exp), |
||
81 | $sut2 = new SUT($exp2) |
||
82 | ) |
||
83 | ->then |
||
84 | ->boolean($sut2->contained($sut)) |
||
85 | ->isEqualTo($contained); |
||
86 | } |
||
87 | } |
||
88 |