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 |
||
13 | View Code Duplication | class FirstEasterDay extends AbstractSpecialDate |
|
|
|||
14 | { |
||
15 | protected function generate() |
||
36 | |||
37 | /** |
||
38 | * Deel het jaartal door 19, neem de rest, en tel er 1 bij op (zoals Dionysius). Noem dit getal G. Voor het jaar 1991 geldt G = 16. |
||
39 | * |
||
40 | * @return int |
||
41 | */ |
||
42 | protected function calculateGNumber() |
||
46 | |||
47 | /** |
||
48 | * Geheeldeel het jaartal door 100 en tel daar 1 bij op. Noem dit getal C. Voor het jaar 1991 geldt C = 20. |
||
49 | * |
||
50 | * @return int |
||
51 | */ |
||
52 | protected function calculateCNumber() |
||
56 | |||
57 | /** |
||
58 | * Vermenigvuldig C met 3, geheeldeel het resultaat door 4 en trek er 12 van af. Noem dit getal X. Voor de twintigste en eenentwintigste eeuw geldt X = 3. |
||
59 | * |
||
60 | * @param int $c |
||
61 | * @return float |
||
62 | */ |
||
63 | protected function calculateXNumber(int $c) |
||
67 | |||
68 | /** |
||
69 | * Neem 8 maal C, tel er 5 bij op, geheeldeel dit door 25 en trek er 5 vanaf. Noem dit getal Y. Voor de twintigste en eenentwintigste eeuw geldt: Y = 1. |
||
70 | * |
||
71 | * @param int $c |
||
72 | * @return float |
||
73 | */ |
||
74 | protected function calculateYNumber(int $c) |
||
78 | |||
79 | /** |
||
80 | * Vermenigvuldig het jaartal met 5, geheeldeel de uitkomst door 4, trek er X en 10 vanaf, en noem dit getal Z. Voor 1991 geldt: Z = 2475. |
||
81 | * |
||
82 | * @param int $x |
||
83 | * @return float |
||
84 | */ |
||
85 | protected function calculateZNumber(int $x) |
||
89 | |||
90 | /** |
||
91 | * 11 maal G + 20 + Y. Trek daarvan X af, geheeldeel het resultaat door 30 en noem de rest E. Als E gelijk is aan 24, of als E gelijk is aan 25 en het gulden getal is groter dan 11, tel dan 1 bij E op. De Epacta voor 1991 is 14. |
||
92 | * |
||
93 | * @param int $x |
||
94 | * @return float |
||
95 | */ |
||
96 | protected function calculateENumber(int $g, int $y, int $x) |
||
106 | |||
107 | /** |
||
108 | * Trek E af van 44. Noem dit getal N. Als N kleiner is dan 21, tel er dan 30 bij op. Voor 1991 geldt: N = 30 |
||
109 | * |
||
110 | * @param int $e |
||
111 | * |
||
112 | * @return int |
||
113 | */ |
||
114 | protected function calculateNNumber(int $e) |
||
124 | |||
125 | /** |
||
126 | * Tel Z en N op. Geheeldeel het resultaat door 7 en trek de rest af van N+7. Noem dit getal P. Voor 1991 geldt: P = 31. |
||
127 | * |
||
128 | * @param int $z |
||
129 | * @param int $n |
||
130 | * @return int |
||
131 | */ |
||
132 | protected function calculatePNumber(int $z, int $n) |
||
136 | |||
137 | /** |
||
138 | |||
139 | |||
140 | Paasdatum: Als P groter is dan 31, trek er dan 31 vanaf, en de paasdatum valt in april. Anders is de paasdag P in maart. Zo wordt voor 1991 gevonden 31 maart. |
||
141 | */ |
||
142 | } |
||
143 |
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.