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 date and/or time pattern |
||
| 132 | * Default date pattern is short date pattern with 4 digit year. |
||
| 133 | * |
||
| 134 | * @param int|string $datetype |
||
| 135 | * @param int|string $timetype |
||
| 136 | * @param int $calendar |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | 12 | protected function getDatePattern($datetype, $timetype, $calendar = \IntlDateFormatter::GREGORIAN) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Format the date and/or time value as a string based on the current locale |
||
| 169 | * |
||
| 170 | * @param \DateTime|int|string $value |
||
| 171 | * @param string $dateFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
| 172 | * @param string $timeFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
| 173 | * @param string $calendar 'gregorian' or 'traditional' |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | 31 | protected function formatLocal($value, $dateFormat, $timeFormat, $calendar = 'gregorian') |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Format the date value as a string based on the current locale |
||
| 190 | * |
||
| 191 | * @param DateTime|int|string $date |
||
| 192 | * @param string $format null, 'short', 'medium', 'long', 'full' or pattern |
||
| 193 | * @param string $calendar 'gregorian' or 'traditional' |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | 11 | public function localDate($date, $format = null, $calendar = 'gregorian') |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Format the time value as a string based on the current locale |
||
| 203 | * |
||
| 204 | * @param DateTime|int|string $date |
||
| 205 | * @param string $format 'short', 'medium', 'long', 'full' or pattern |
||
| 206 | * @param string $calendar 'gregorian' or 'traditional' |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | 11 | public function localTime($date, $format = 'short', $calendar = 'gregorian') |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Format the date/time value as a string based on the current locale |
||
| 216 | * |
||
| 217 | * @param DateTime|int|string $date |
||
| 218 | * @param string $format date format, pattern or ['date'=>format, 'time'=>format) |
||
| 219 | * @param string $calendar 'gregorian' or 'traditional' |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | 9 | public function localDateTime($date, $format = null, $calendar = 'gregorian') |
|
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Split duration into seconds, minutes, hours, days, weeks and years. |
||
| 238 | * |
||
| 239 | * @param int $seconds |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | 13 | protected function splitDuration($seconds, $max) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Calculate duration from seconds. |
||
| 279 | * One year is seen as exactly 52 weeks. |
||
| 280 | * |
||
| 281 | * Use null to skip a unit. |
||
| 282 | * |
||
| 283 | * @param int $value Time in seconds |
||
| 284 | * @param array $units Time units (seconds, minutes, hours, days, weeks, years) |
||
| 285 | * @param string $seperator |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | 14 | public function duration($value, $units = ['s', 'm', 'h', 'd', 'w', 'y'], $seperator = ' ') |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Get the age (in years) based on a date. |
||
| 327 | * |
||
| 328 | * @param DateTime|string $value |
||
| 329 | * @return int |
||
| 330 | */ |
||
| 331 | 3 | public function age($value) |
|
| 341 | } |
||
| 342 |
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: