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 DateExtension 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 DateExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class DateExtension extends \Twig_Extension |
||
9 | { |
||
10 | /** |
||
11 | * Class constructor |
||
12 | */ |
||
13 | public function __construct() |
||
19 | |||
20 | |||
21 | /** |
||
22 | * Return extension name |
||
23 | * |
||
24 | * @return string |
||
25 | */ |
||
26 | public function getName() |
||
30 | |||
31 | /** |
||
32 | * Callback for Twig to get all the filters. |
||
33 | * |
||
34 | * @return \Twig_Filter[] |
||
35 | */ |
||
36 | public function getFilters() |
||
46 | |||
47 | |||
48 | /** |
||
49 | * Get configured intl date formatter. |
||
50 | * |
||
51 | * @param string|null $dateFormat |
||
52 | * @param string|null $timeFormat |
||
53 | * @param string $calendar |
||
54 | * @return \IntlDateFormatter |
||
55 | */ |
||
56 | protected function getDateFormatter($dateFormat, $timeFormat, $calendar) |
||
81 | |||
82 | /** |
||
83 | * Format the date/time value as a string based on the current locale |
||
84 | * |
||
85 | * @param string $format 'short', 'medium', 'long', 'full' |
||
86 | * @return int|null |
||
87 | */ |
||
88 | protected function getFormat($format) |
||
101 | |||
102 | /** |
||
103 | * Default date pattern is short date pattern with 4 digit year |
||
104 | * |
||
105 | * @param int|string $datetype |
||
106 | * @param int|string $timetype |
||
107 | * @param int $calendar |
||
108 | * @return string |
||
109 | */ |
||
110 | protected function getDatePattern($datetype, $timetype, $calendar = \IntlDateFormatter::GREGORIAN) |
||
137 | |||
138 | /** |
||
139 | * Format the date and/or time value as a string based on the current locale |
||
140 | * |
||
141 | * @param DateTime|int|string $date |
||
142 | * @param string $dateFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
143 | * @param string $timeFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
144 | * @param string $calendar 'gregorian' or 'traditional' |
||
145 | * @return string |
||
146 | */ |
||
147 | protected function formatLocal($date, $dateFormat, $timeFormat, $calendar = 'gregorian') |
||
161 | |||
162 | /** |
||
163 | * Format the date value as a string based on the current locale |
||
164 | * |
||
165 | * @param DateTime|int|string $date |
||
166 | * @param string $format null, 'short', 'medium', 'long', 'full' or pattern |
||
167 | * @param string $calendar 'gregorian' or 'traditional' |
||
168 | * @return string |
||
169 | */ |
||
170 | public function localDate($date, $format = null, $calendar = 'gregorian') |
||
174 | |||
175 | /** |
||
176 | * Format the time value as a string based on the current locale |
||
177 | * |
||
178 | * @param DateTime|int|string $date |
||
179 | * @param string $format 'short', 'medium', 'long', 'full' or pattern |
||
180 | * @param string $calendar 'gregorian' or 'traditional' |
||
181 | * @return string |
||
182 | */ |
||
183 | public function localTime($date, $format='short', $calendar='gregorian') |
||
187 | |||
188 | /** |
||
189 | * Format the date/time value as a string based on the current locale |
||
190 | * |
||
191 | * @param DateTime|int|string $date |
||
192 | * @param string $format date format, pattern or ['date'=>format, 'time'=>format) |
||
193 | * @param string $calendar 'gregorian' or 'traditional' |
||
194 | * @return string |
||
195 | */ |
||
196 | public function localDateTime($date, $format = null, $calendar = 'gregorian') |
||
208 | |||
209 | |||
210 | /** |
||
211 | * Split duration into seconds, minutes, hours, days, weeks and years. |
||
212 | * |
||
213 | * @param int $seconds |
||
214 | * @return array |
||
215 | */ |
||
216 | protected function splitDuration($seconds, $max) |
||
250 | |||
251 | /** |
||
252 | * Calculate duration from seconds. |
||
253 | * One year is seen as exactly 52 weeks. |
||
254 | * |
||
255 | * Use null to skip a unit. |
||
256 | * |
||
257 | * @param int $value Time in seconds |
||
258 | * @param array $units Time units (seconds, minutes, hours, days, weeks, years) |
||
259 | * @param string $seperator |
||
260 | * @return string |
||
261 | */ |
||
262 | public function duration($value, $units = ['s', 'm', 'h', 'd', 'w', 'y'], $seperator = ' ') |
||
298 | |||
299 | /** |
||
300 | * Get the age (in years) based on a date. |
||
301 | * |
||
302 | * @param DateTime|string $date |
||
303 | * @return int |
||
304 | */ |
||
305 | public function age($date) |
||
317 | } |
||
318 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.