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 | 48 | public function __construct() |
|
19 | |||
20 | |||
21 | /** |
||
22 | * Return extension name |
||
23 | * |
||
24 | * @return string |
||
25 | */ |
||
26 | 48 | public function getName() |
|
30 | |||
31 | /** |
||
32 | * Callback for Twig to get all the filters. |
||
33 | * |
||
34 | * @return \Twig_SimpleFilter[] |
||
35 | */ |
||
36 | 34 | public function getFilters() |
|
46 | |||
47 | /** |
||
48 | * Turn a value into a DateTime object |
||
49 | * |
||
50 | * @param string|int|\DateTime $date |
||
51 | * @return \DateTime |
||
52 | */ |
||
53 | 30 | protected function valueToDateTime($date) |
|
61 | |||
62 | /** |
||
63 | * Get configured intl date formatter. |
||
64 | * |
||
65 | * @param string|null $dateFormat |
||
66 | * @param string|null $timeFormat |
||
67 | * @param string $calendar |
||
68 | * @return \IntlDateFormatter |
||
69 | */ |
||
70 | 28 | protected function getDateFormatter($dateFormat, $timeFormat, $calendar) |
|
85 | |||
86 | /** |
||
87 | * Format the date/time value as a string based on the current locale |
||
88 | * |
||
89 | * @param string|false $format 'short', 'medium', 'long', 'full', 'none' or false |
||
90 | * @return int|null |
||
91 | */ |
||
92 | 28 | protected function getFormat($format) |
|
108 | |||
109 | /** |
||
110 | * Get the date/time pattern. |
||
111 | * |
||
112 | * @param int|string $datetype |
||
113 | * @param int|string $timetype |
||
114 | * @param string $calendar |
||
115 | * @return string |
||
116 | */ |
||
117 | 28 | protected function getDateTimePattern($datetype, $timetype, $calendar = \IntlDateFormatter::GREGORIAN) |
|
129 | |||
130 | /** |
||
131 | * Get the formatter to create a date and/or time pattern |
||
132 | * |
||
133 | * @param int|string $datetype |
||
134 | * @param int|string $timetype |
||
135 | * @param int $calendar |
||
136 | * @return string |
||
137 | */ |
||
138 | 6 | protected function getDatePatternFormatter($datetype, $timetype, $calendar = \IntlDateFormatter::GREGORIAN) |
|
148 | |||
149 | /** |
||
150 | * Get the date and/or time pattern |
||
151 | * Default date pattern is short date pattern with 4 digit year. |
||
152 | * |
||
153 | * @param int|string $datetype |
||
154 | * @param int|string $timetype |
||
155 | * @param int $calendar |
||
156 | * @return string |
||
157 | */ |
||
158 | 12 | protected function getDatePattern($datetype, $timetype, $calendar = \IntlDateFormatter::GREGORIAN) |
|
172 | |||
173 | /** |
||
174 | * Format the date and/or time value as a string based on the current locale |
||
175 | * |
||
176 | * @param \DateTime|int|string $value |
||
177 | * @param string $dateFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
178 | * @param string $timeFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
179 | * @param string $calendar 'gregorian' or 'traditional' |
||
180 | * @return string |
||
181 | */ |
||
182 | 31 | protected function formatLocal($value, $dateFormat, $timeFormat, $calendar = 'gregorian') |
|
193 | |||
194 | /** |
||
195 | * Format the date value as a string based on the current locale |
||
196 | * |
||
197 | * @param DateTime|int|string $date |
||
198 | * @param string $format null, 'short', 'medium', 'long', 'full' or pattern |
||
199 | * @param string $calendar 'gregorian' or 'traditional' |
||
200 | * @return string |
||
201 | */ |
||
202 | 11 | public function localDate($date, $format = null, $calendar = 'gregorian') |
|
206 | |||
207 | /** |
||
208 | * Format the time value as a string based on the current locale |
||
209 | * |
||
210 | * @param DateTime|int|string $date |
||
211 | * @param string $format 'short', 'medium', 'long', 'full' or pattern |
||
212 | * @param string $calendar 'gregorian' or 'traditional' |
||
213 | * @return string |
||
214 | */ |
||
215 | 11 | public function localTime($date, $format = 'short', $calendar = 'gregorian') |
|
219 | |||
220 | /** |
||
221 | * Format the date/time value as a string based on the current locale |
||
222 | * |
||
223 | * @param DateTime|int|string $date |
||
224 | * @param string $format date format, pattern or ['date'=>format, 'time'=>format) |
||
225 | * @param string $calendar 'gregorian' or 'traditional' |
||
226 | * @return string |
||
227 | */ |
||
228 | 9 | public function localDateTime($date, $format = null, $calendar = 'gregorian') |
|
240 | |||
241 | |||
242 | /** |
||
243 | * Split duration into seconds, minutes, hours, days, weeks and years. |
||
244 | * |
||
245 | * @param int $seconds |
||
246 | * @return array |
||
247 | */ |
||
248 | 13 | protected function splitDuration($seconds, $max) |
|
282 | |||
283 | /** |
||
284 | * Calculate duration from seconds. |
||
285 | * One year is seen as exactly 52 weeks. |
||
286 | * |
||
287 | * Use null to skip a unit. |
||
288 | * |
||
289 | * @param int $value Time in seconds |
||
290 | * @param array $units Time units (seconds, minutes, hours, days, weeks, years) |
||
291 | * @param string $seperator |
||
292 | * @return string |
||
293 | */ |
||
294 | 14 | public function duration($value, $units = ['s', 'm', 'h', 'd', 'w', 'y'], $seperator = ' ') |
|
330 | |||
331 | /** |
||
332 | * Get the age (in years) based on a date. |
||
333 | * |
||
334 | * @param DateTime|string $value |
||
335 | * @return int |
||
336 | */ |
||
337 | 3 | public function age($value) |
|
347 | } |
||
348 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: