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 |
||
8 | class DatePickerControllerTest extends WebTestCase |
||
9 | { |
||
10 | /** |
||
11 | * @param string $method |
||
12 | * @param string $url |
||
13 | * @return Crawler |
||
14 | */ |
||
15 | private function getCrawlerForRequest($method, $url) |
||
22 | |||
23 | public function testIfChoiceRenderedCorrectly() |
||
31 | |||
32 | public function testIfSingleTextRenderedCorrectly() |
||
33 | { |
||
34 | $crawler = $this->getCrawlerForRequest('GET', '/_tests/datepicker'); |
||
35 | |||
36 | $this->assertEquals(1, $crawler->filter('input#form_date_example2')->count()); |
||
37 | } |
||
38 | |||
39 | View Code Duplication | public function testIfSingleTextWithDatePickerAndDefaultDateRenderedCorrectly() |
|
|
|||
40 | { |
||
41 | $crawler = $this->getCrawlerForRequest('GET', '/_tests/datepicker'); |
||
42 | |||
43 | $element = $crawler->filter('input#form_date_example3'); |
||
44 | |||
45 | $this->assertEquals(1, $element->count()); |
||
46 | $this->assertEquals('20/06/1985', $element->attr('value')); |
||
47 | } |
||
48 | |||
49 | View Code Duplication | public function testIfSingleTextWithDatePickerRenderedCorrectly() |
|
50 | { |
||
51 | $crawler = $this->getCrawlerForRequest('GET', '/_tests/datepicker'); |
||
52 | $date = new \DateTime(); |
||
53 | |||
54 | $element = $crawler->filter('input#form_date_example4'); |
||
55 | |||
56 | $this->assertEquals(1, $element->count()); |
||
57 | |||
58 | $this->assertEquals($date->format('d/m/Y'), $element->attr('value')); |
||
59 | } |
||
60 | |||
61 | View Code Duplication | public function testIfSingleTextWithDatePickerAndOnlyDatesInTheFutureRenderedCorrectly() |
|
62 | { |
||
63 | $crawler = $this->getCrawlerForRequest('GET', '/_tests/datepicker'); |
||
64 | $date = new \DateTime(); |
||
65 | $startDate = new \DateTime('last monday'); |
||
66 | |||
67 | $element = $crawler->filter('input#form_date_example5'); |
||
68 | |||
69 | $this->assertEquals(1, $element->count()); |
||
70 | |||
71 | // check if it has all the required data-attributes |
||
72 | $this->assertEquals($startDate->format('d/m/Y'), $element->attr('data-date-start-date')); |
||
73 | |||
74 | // check if the actual element is hidden |
||
75 | $this->assertEquals($date->format('d/m/Y'), $element->attr('value')); |
||
76 | } |
||
77 | |||
78 | View Code Duplication | public function testIfSingleTextWithDatePickerAndOnlyDatesInThePastRenderedCorrectly() |
|
94 | |||
95 | public function testIfSingleTextWithDatePickerAndOnlyDatesBetweenCorrectly() |
||
113 | |||
114 | View Code Duplication | public function testIfSingleTextWithoutDefaultDateRenderedCorrectly() |
|
115 | { |
||
116 | $crawler = $this->getCrawlerForRequest('GET', '/_tests/datepicker'); |
||
117 | |||
125 | } |
||
126 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.