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 |
||
11 | class DateExtension extends AbstractExtension |
||
12 | { |
||
13 | 48 | /** |
|
14 | * Class constructor |
||
15 | 48 | */ |
|
16 | public function __construct() |
||
22 | |||
23 | |||
24 | /** |
||
25 | * Return extension name |
||
26 | 48 | * |
|
27 | * @return string |
||
28 | 48 | */ |
|
29 | public function getName() |
||
33 | |||
34 | /** |
||
35 | * Callback for Twig to get all the filters. |
||
36 | 34 | * |
|
37 | * @return \Twig\TwigFilter[] |
||
38 | */ |
||
39 | 34 | public function getFilters() |
|
49 | |||
50 | /** |
||
51 | * Turn a value into a DateTime object |
||
52 | * |
||
53 | 30 | * @param string|int|\DateTime $date |
|
54 | * @return \DateTime |
||
55 | 30 | */ |
|
56 | 30 | protected function valueToDateTime($date) |
|
64 | |||
65 | /** |
||
66 | * Get configured intl date formatter. |
||
67 | * |
||
68 | * @param string|null $dateFormat |
||
69 | * @param string|null $timeFormat |
||
70 | 28 | * @param string $calendar |
|
71 | * @return \IntlDateFormatter |
||
72 | 28 | */ |
|
73 | 28 | protected function getDateFormatter($dateFormat, $timeFormat, $calendar) |
|
88 | |||
89 | /** |
||
90 | * Format the date/time value as a string based on the current locale |
||
91 | * |
||
92 | 28 | * @param string|false $format 'short', 'medium', 'long', 'full', 'none' or false |
|
93 | * @return int|null |
||
94 | 28 | */ |
|
95 | 22 | protected function getFormat($format) |
|
111 | |||
112 | /** |
||
113 | * Get the date/time pattern. |
||
114 | * |
||
115 | * @param int|string $datetype |
||
116 | * @param int|string $timetype |
||
117 | 28 | * @param int $calendar |
|
118 | * @return string |
||
119 | 28 | */ |
|
120 | 16 | protected function getDateTimePattern($datetype, $timetype, $calendar = \IntlDateFormatter::GREGORIAN) |
|
132 | |||
133 | /** |
||
134 | * Get the formatter to create a date and/or time pattern |
||
135 | * |
||
136 | * @param int|string $datetype |
||
137 | * @param int|string $timetype |
||
138 | 6 | * @param int $calendar |
|
139 | * @return \IntlDateFormatter |
||
140 | 6 | */ |
|
141 | 6 | protected function getDatePatternFormatter($datetype, $timetype, $calendar = \IntlDateFormatter::GREGORIAN) |
|
151 | |||
152 | /** |
||
153 | * Get the date and/or time pattern |
||
154 | * Default date pattern is short date pattern with 4 digit year. |
||
155 | * |
||
156 | * @param int|string $datetype |
||
157 | * @param int|string $timetype |
||
158 | 12 | * @param int $calendar |
|
159 | * @return string |
||
160 | */ |
||
161 | 12 | protected function getDatePattern($datetype, $timetype, $calendar = \IntlDateFormatter::GREGORIAN) |
|
175 | |||
176 | /** |
||
177 | * Format the date and/or time value as a string based on the current locale |
||
178 | * |
||
179 | * @param \DateTime|int|string $value |
||
180 | * @param string $dateFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
181 | * @param string $timeFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
182 | 31 | * @param string $calendar 'gregorian' or 'traditional' |
|
183 | * @return string |
||
184 | 31 | */ |
|
185 | 3 | protected function formatLocal($value, $dateFormat, $timeFormat, $calendar = 'gregorian') |
|
196 | |||
197 | /** |
||
198 | * Format the date value as a string based on the current locale |
||
199 | * |
||
200 | * @param DateTime|int|string $date |
||
201 | * @param string $format null, 'short', 'medium', 'long', 'full' or pattern |
||
202 | 11 | * @param string $calendar 'gregorian' or 'traditional' |
|
203 | * @return string |
||
204 | 11 | */ |
|
205 | public function localDate($date, $format = null, $calendar = 'gregorian') |
||
209 | |||
210 | /** |
||
211 | * Format the time value as a string based on the current locale |
||
212 | * |
||
213 | * @param DateTime|int|string $date |
||
214 | * @param string $format 'short', 'medium', 'long', 'full' or pattern |
||
215 | 11 | * @param string $calendar 'gregorian' or 'traditional' |
|
216 | * @return string |
||
217 | 11 | */ |
|
218 | public function localTime($date, $format = 'short', $calendar = 'gregorian') |
||
222 | |||
223 | /** |
||
224 | * Format the date/time value as a string based on the current locale |
||
225 | * |
||
226 | * @param DateTime|int|string $date |
||
227 | * @param string $format date format, pattern or ['date'=>format, 'time'=>format) |
||
228 | 9 | * @param string $calendar 'gregorian' or 'traditional' |
|
229 | * @return string |
||
230 | 9 | */ |
|
231 | 6 | public function localDateTime($date, $format = null, $calendar = 'gregorian') |
|
243 | |||
244 | |||
245 | /** |
||
246 | * Split duration into seconds, minutes, hours, days, weeks and years. |
||
247 | * |
||
248 | 13 | * @param int $seconds |
|
249 | * @return array |
||
250 | 13 | */ |
|
251 | 1 | protected function splitDuration($seconds, $max) |
|
285 | |||
286 | /** |
||
287 | * Calculate duration from seconds. |
||
288 | * One year is seen as exactly 52 weeks. |
||
289 | * |
||
290 | * Use null to skip a unit. |
||
291 | * |
||
292 | * @param int $value Time in seconds |
||
293 | * @param array $units Time units (seconds, minutes, hours, days, weeks, years) |
||
294 | 14 | * @param string $separator |
|
295 | * @return string |
||
296 | 14 | */ |
|
297 | 1 | public function duration($value, $units = ['s', 'm', 'h', 'd', 'w', 'y'], $separator = ' ') |
|
333 | |||
334 | /** |
||
335 | * Get the age (in years) based on a date. |
||
336 | * |
||
337 | 3 | * @param DateTime|string $value |
|
338 | * @return int |
||
339 | 3 | */ |
|
340 | 1 | public function age($value) |
|
350 | } |
||
351 |
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: