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 | * Format the date/time value as a string based on the current locale |
||
50 | * |
||
51 | * @param string $format null, 'short', 'medium', 'long', 'full' or pattern |
||
52 | * @param int $calendar |
||
53 | * @return array [format, pattern) |
||
54 | */ |
||
55 | protected function getFormat($format, $calendar = \IntlDateFormatter::GREGORIAN) |
||
76 | |||
77 | /** |
||
78 | * Default date pattern is short date pattern with 4 digit year |
||
79 | * |
||
80 | * @param int $calendar |
||
81 | * @return string |
||
82 | */ |
||
83 | protected function getDefaultDatePattern($calendar=\IntlDateFormatter::GREGORIAN) |
||
95 | |||
96 | /** |
||
97 | * Format the date and/or time value as a string based on the current locale |
||
98 | * |
||
99 | * @param DateTime|int|string $date |
||
100 | * @param string $dateFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
101 | * @param string $timeFormat null, 'short', 'medium', 'long', 'full' or pattern |
||
102 | * @param string $calendar 'gregorian' or 'traditional' |
||
103 | * @return string |
||
104 | */ |
||
105 | protected function formatLocal($date, $dateFormat, $timeFormat, $calendar = 'gregorian') |
||
131 | |||
132 | /** |
||
133 | * Format the date value as a string based on the current locale |
||
134 | * |
||
135 | * @param DateTime|int|string $date |
||
136 | * @param string $format null, 'short', 'medium', 'long', 'full' or pattern |
||
137 | * @param string $calendar 'gregorian' or 'traditional' |
||
138 | * @return string |
||
139 | */ |
||
140 | public function localDate($date, $format = null, $calendar = 'gregorian') |
||
144 | |||
145 | /** |
||
146 | * Format the time value as a string based on the current locale |
||
147 | * |
||
148 | * @param DateTime|int|string $date |
||
149 | * @param string $format 'short', 'medium', 'long', 'full' or pattern |
||
150 | * @param string $calendar 'gregorian' or 'traditional' |
||
151 | * @return string |
||
152 | */ |
||
153 | public function localTime($date, $format='short', $calendar='gregorian') |
||
157 | |||
158 | /** |
||
159 | * Format the date/time value as a string based on the current locale |
||
160 | * |
||
161 | * @param DateTime|int|string $date |
||
162 | * @param string $format date format, pattern or ['date'=>format, 'time'=>format) |
||
163 | * @param string $calendar 'gregorian' or 'traditional' |
||
164 | * @return string |
||
165 | */ |
||
166 | public function localDateTime($date, $format=null, $calendar='gregorian') |
||
180 | |||
181 | |||
182 | /** |
||
183 | * Split duration into seconds, minutes, hours, days, weeks and years. |
||
184 | * |
||
185 | * @param int $seconds |
||
186 | * @return array |
||
187 | */ |
||
188 | protected function splitDuration($seconds, $max) |
||
222 | |||
223 | /** |
||
224 | * Calculate duration from seconds. |
||
225 | * 1 year is seen as exactly 52 weeks. |
||
226 | * |
||
227 | * Use null to skip a unit. |
||
228 | * |
||
229 | * @param int $seconds Time in seconds |
||
230 | * @param array $units Time units (seconds, minutes, hours, days, weeks, years) |
||
231 | * @param string $separator |
||
232 | * @return string |
||
233 | */ |
||
234 | public function duration($seconds, $units = ['s', 'm', 'h', 'd', 'w', 'y'], $seperator = ' ') |
||
266 | |||
267 | /** |
||
268 | * Get the age (in years) based on a date. |
||
269 | * |
||
270 | * @param DateTime|string $date |
||
271 | * @return int |
||
272 | */ |
||
273 | public function age($date) |
||
281 | } |
||
282 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.