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:
Complex classes like Date often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Date, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Date |
||
32 | { |
||
33 | /** |
||
34 | * set name of image |
||
35 | * |
||
36 | * @access public |
||
37 | * @param int $iWeek number of week |
||
38 | * @param int $iYear year |
||
39 | * @param string $sFormat |
||
40 | * @return Date |
||
41 | */ |
||
42 | public static function getWeek(int $iWeek, int $iYear, string $sFormat = "Y-m-d") : Date |
||
57 | |||
58 | /** |
||
59 | * set name of image |
||
60 | * |
||
61 | * @access public |
||
62 | * @return \Venus\lib\Date |
||
63 | */ |
||
64 | public static function getActualWeek() : Date |
||
68 | |||
69 | /** |
||
70 | * set name of image |
||
71 | * |
||
72 | * @access public |
||
73 | * @param string $sMonth number of week |
||
74 | * @param string $sLanguage language |
||
75 | * @return \Venus\lib\Date |
||
76 | */ |
||
77 | public static function getMonthInWord(string $sMonth, string $sLanguage = 'fr') : Date |
||
95 | |||
96 | /** |
||
97 | * set name of image |
||
98 | * |
||
99 | * @access public |
||
100 | * @param mixed $sDay number of day |
||
101 | * @param string $sLanguage language |
||
102 | * @return \Venus\lib\Date |
||
103 | */ |
||
104 | public static function getDayInWord(string $sDay, string $sLanguage = 'fr') : Date |
||
117 | |||
118 | /** |
||
119 | * get age by date |
||
120 | * |
||
121 | * @access public |
||
122 | * @param unknown $sBirthday |
||
123 | * @return int |
||
124 | */ |
||
125 | public static function getAgeByDate(string $sBirthday) : int |
||
150 | |||
151 | /** |
||
152 | * set name of image |
||
153 | * |
||
154 | * @access public |
||
155 | * @param int $iWeek number of week |
||
156 | * @param int $iYear year |
||
157 | * @return \Venus\lib\Date |
||
158 | */ |
||
159 | public static function getMiddleWeek(int $iWeek, int $iYear, string $sFormat = "Y-m-d") : array |
||
190 | |||
191 | /** |
||
192 | * set name of image |
||
193 | * |
||
194 | * @access public |
||
195 | * @param int $iWeek number of week |
||
196 | * @param int $iYear year |
||
197 | * @return array |
||
198 | */ |
||
199 | public static function getActualMiddleWeek() : array |
||
203 | |||
204 | /** |
||
205 | * get time of kind "X hour ago" |
||
206 | * |
||
207 | * @access public |
||
208 | * @param string $sDateTime datetime to convert |
||
209 | * @param string $sLanguage language |
||
210 | * @return string |
||
211 | */ |
||
212 | public static function getTimeAgoInString(string $sDateTime, string $sLanguage = 'fr') : string |
||
234 | } |
||
235 |
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.