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 |
||
12 | View Code Duplication | class CronDayOfMonthFormatValidatorTest extends AbstractConstraintValidatorTest |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * {@inheritdoc} |
||
16 | */ |
||
17 | protected function createValidator() |
||
21 | |||
22 | /** |
||
23 | * @return array |
||
24 | */ |
||
25 | public function getValidValues() |
||
26 | { |
||
27 | return [ |
||
28 | [1], |
||
29 | [31], |
||
30 | ['2,15'], |
||
31 | ['4-15'], |
||
32 | ['1-9,15-25'], |
||
33 | ['5,15-30'], |
||
34 | ['*/10'], |
||
35 | ['*'], |
||
36 | ['*,7,*/10'], |
||
37 | ]; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @dataProvider getValidValues |
||
42 | * |
||
43 | * @param string $value |
||
44 | */ |
||
45 | public function testValidValue($value) |
||
51 | |||
52 | /** |
||
53 | * @return array |
||
54 | */ |
||
55 | public function getInvalidValues() |
||
56 | { |
||
57 | return [ |
||
58 | [-1], |
||
59 | [0], |
||
60 | [32], |
||
61 | ['5,35'], |
||
62 | ['3-35'], |
||
63 | ['-1'], |
||
64 | ['1-'], |
||
65 | ['3,20-35'], |
||
66 | ['1-10,20-45'], |
||
67 | ['25/*'], |
||
68 | ['2-10,25/*'], |
||
69 | ['**'], |
||
70 | ]; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @dataProvider getInvalidValues |
||
75 | * |
||
76 | * @param string $value |
||
77 | */ |
||
78 | public function testInvalidValues($value) |
||
86 | } |
||
87 |
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.