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 |
||
16 | View Code Duplication | class MonthlyLastWorkdayIncrementTest extends \PHPUnit_Framework_TestCase |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | private $fixture = array( |
||
22 | |||
23 | // Start at one second before last workday of month, shift to next day |
||
24 | |||
25 | array( |
||
26 | '2015-03-30 23:59:59', // Starting time |
||
27 | '2015-03-31' // Expected time |
||
28 | ), |
||
29 | |||
30 | // Start at first second of the day after last workday of month, shift forward by a month |
||
31 | |||
32 | array( |
||
33 | '2015-03-31 23:59:59', // Starting time |
||
34 | '2015-04-30', // Expected time |
||
35 | ), |
||
36 | |||
37 | // Shift over holidays |
||
38 | |||
39 | array( |
||
40 | '2015-03-30 15:12:24', // Starting time |
||
41 | '2015-04-30', // Expected time |
||
42 | array( |
||
43 | '2015-03-31' // Holidays |
||
44 | ) |
||
45 | ), |
||
46 | |||
47 | // Start in the middle of a month |
||
48 | |||
49 | array( |
||
50 | '2015-06-15', // Starting time |
||
51 | '2015-06-30' // Expected time |
||
52 | ), |
||
53 | |||
54 | // Start at first second of last working day of month |
||
55 | // Last working day of next month is a Monday, but it's a holiday |
||
56 | // Shift over the weekend to Friday of the next month |
||
57 | |||
58 | array( |
||
59 | '2015-07-31', // Starting time |
||
60 | '2015-08-28', // Expected time |
||
61 | array( |
||
62 | '2015-08-31' // Holidays |
||
63 | ) |
||
64 | ) |
||
65 | |||
66 | ); |
||
67 | |||
68 | /** |
||
69 | * @dataProvider shiftProvider |
||
70 | * @param string $start |
||
71 | * @param string $expected |
||
72 | * @param string[] $holidays |
||
73 | */ |
||
74 | public function testShift($start, $expected, $holidays = array()) |
||
89 | |||
90 | /** |
||
91 | * @return array |
||
92 | */ |
||
93 | public function shiftProvider() |
||
97 | } |
||
98 |
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.