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 | * Turn a value into a DateTime object |
||
| 49 | * |
||
| 50 | * @param string|int|\DateTime $date |
||
| 51 | * @return \DateTime |
||
| 52 | */ |
||
| 53 | 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 | protected function getDateFormatter($dateFormat, $timeFormat, $calendar) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Format the date/time value as a string based on the current locale |
||
| 98 | * |
||
| 99 | * @param string|false $format 'short', 'medium', 'long', 'full', 'none' or false |
||
| 100 | * @return int|null |
||
| 101 | */ |
||
| 102 | protected function getFormat($format) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Default date pattern is short date pattern with 4 digit year |
||
| 121 | * |
||
| 122 | * @param int|string $datetype |
||
| 123 | * @param int|string $timetype |
||
| 124 | * @param int $calendar |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | protected function getDatePattern($datetype, $timetype, $calendar = \IntlDateFormatter::GREGORIAN) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Format the date and/or time value as a string based on the current locale |
||
| 157 | * |
||
| 158 | * @param \DateTime|int|string $value |
||
| 159 | * @param string $dateFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
| 160 | * @param string $timeFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
| 161 | * @param string $calendar 'gregorian' or 'traditional' |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | protected function formatLocal($value, $dateFormat, $timeFormat, $calendar = 'gregorian') |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Format the date value as a string based on the current locale |
||
| 178 | * |
||
| 179 | * @param DateTime|int|string $date |
||
| 180 | * @param string $format null, 'short', 'medium', 'long', 'full' or pattern |
||
| 181 | * @param string $calendar 'gregorian' or 'traditional' |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function localDate($date, $format = null, $calendar = 'gregorian') |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Format the time value as a string based on the current locale |
||
| 191 | * |
||
| 192 | * @param DateTime|int|string $date |
||
| 193 | * @param string $format 'short', 'medium', 'long', 'full' or pattern |
||
| 194 | * @param string $calendar 'gregorian' or 'traditional' |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function localTime($date, $format='short', $calendar='gregorian') |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Format the date/time value as a string based on the current locale |
||
| 204 | * |
||
| 205 | * @param DateTime|int|string $date |
||
| 206 | * @param string $format date format, pattern or ['date'=>format, 'time'=>format) |
||
| 207 | * @param string $calendar 'gregorian' or 'traditional' |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function localDateTime($date, $format = null, $calendar = 'gregorian') |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * Split duration into seconds, minutes, hours, days, weeks and years. |
||
| 226 | * |
||
| 227 | * @param int $seconds |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | protected function splitDuration($seconds, $max) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Calculate duration from seconds. |
||
| 267 | * One year is seen as exactly 52 weeks. |
||
| 268 | * |
||
| 269 | * Use null to skip a unit. |
||
| 270 | * |
||
| 271 | * @param int $value Time in seconds |
||
| 272 | * @param array $units Time units (seconds, minutes, hours, days, weeks, years) |
||
| 273 | * @param string $seperator |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function duration($value, $units = ['s', 'm', 'h', 'd', 'w', 'y'], $seperator = ' ') |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get the age (in years) based on a date. |
||
| 315 | * |
||
| 316 | * @param DateTime|string $value |
||
| 317 | * @return int |
||
| 318 | */ |
||
| 319 | public function age($value) |
||
| 329 | } |
||
| 330 |
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: