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 MonthlyLastWorkdayDecrementTest extends \PHPUnit_Framework_TestCase |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | private $fixture = array( |
||
22 | |||
23 | // Start at one second after last workday of month, shift to previous day |
||
24 | |||
25 | array( |
||
26 | '2015-04-01', // Starting time |
||
27 | '2015-03-31' // Expected time |
||
28 | ), |
||
29 | |||
30 | // Start at last second of last workday of month, shift back by a month |
||
31 | |||
32 | array( |
||
33 | '2015-03-31 23:59:59', // Starting time |
||
34 | '2015-02-27', // Expected time |
||
35 | ), |
||
36 | |||
37 | // Shift over holidays |
||
38 | |||
39 | array( |
||
40 | '2015-04-01 15:12:24', // Starting time |
||
41 | '2015-03-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-05-29' // Expected time |
||
52 | ), |
||
53 | |||
54 | // Start one second before last working day of month |
||
55 | // Last working day of previous month is a Monday, but it's a holiday |
||
56 | // Shift over the weekend to Friday of last month |
||
57 | |||
58 | array( |
||
59 | '2015-09-29 23:59:59', // Starting time |
||
60 | '2015-08-28', // Expected time |
||
61 | array( |
||
62 | '2015-08-31' // Holidays |
||
63 | ) |
||
64 | ) |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * @dataProvider shiftProvider |
||
69 | * @param string $start |
||
70 | * @param string $expected |
||
71 | * @param string[] $holidays |
||
72 | */ |
||
73 | public function testShift($start, $expected, $holidays = array()) |
||
88 | |||
89 | /** |
||
90 | * @return array |
||
91 | */ |
||
92 | public function shiftProvider() |
||
96 | } |
||
97 |
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.