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:
1 | <?php |
||
20 | class DateToolbox |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * Maps jQuery's datepicker format tokens to PHP's |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected static $_map = [ |
||
29 | 'date' => [ |
||
30 | 'dd' => 'd', |
||
31 | 'd' => 'j', |
||
32 | 'oo' => 'z', |
||
33 | 'DD' => 'l', |
||
34 | 'D' => 'D', |
||
35 | 'mm' => 'm', |
||
36 | 'm' => 'n', |
||
37 | 'MM' => 'F', |
||
38 | 'M' => 'M', |
||
39 | 'yy' => 'Y', |
||
40 | 'y' => 'y', |
||
41 | '@' => 'U', |
||
42 | ], |
||
43 | 'time' => [ |
||
44 | 'HH' => 'G', |
||
45 | 'H' => 'H', |
||
46 | 'hh' => 'h', |
||
47 | 'h' => 'g', |
||
48 | 'mm' => 'i', |
||
49 | 'ss' => 's', |
||
50 | 'tt' => 'a', |
||
51 | 'TT' => 'A', |
||
52 | ] |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * Converts the given $date to a valid PHP's DateTime object using a jQuery's |
||
57 | * date/time $format. |
||
58 | * |
||
59 | * @param string $format A jQuery's date/time format. e.g. `'today is:' yy-mm-dd` |
||
60 | * @param string $date A date formatted using $format. e.g. `today is: 2015-01-30` |
||
61 | * @return \DateTime|false Date object on success, false on error |
||
62 | */ |
||
63 | public static function createFromFormat($format, $date) |
||
77 | |||
78 | /** |
||
79 | * Formats then given UNIX timestamp date using que given jQuery format. |
||
80 | * |
||
81 | *@param string $format jQuery format. e.g. `'today is:' yy-mm-dd` |
||
82 | *@param int $timestamp Date as UNIX timestamp |
||
83 | */ |
||
84 | public static function formatDate($format, $timestamp) |
||
105 | |||
106 | /** |
||
107 | * Converts jQuery's date/time format to PHP's. |
||
108 | * |
||
109 | * @param string $format Date format coming from jQuery's datepicker widget. |
||
110 | * e.g. yy-mm-dd hh:mm |
||
111 | * @return string A valid date/time format to use with PHP |
||
112 | */ |
||
113 | public static function normalizeFormat($format) |
||
142 | |||
143 | /** |
||
144 | * Validates a date format for jQuery's datepicker widget. |
||
145 | * |
||
146 | * @param string $format Format to validate. e.g. yy:mm:ddQ (invalid) |
||
147 | * @return bool |
||
148 | */ |
||
149 | View Code Duplication | public static function validateDateFormat($format) |
|
158 | |||
159 | /** |
||
160 | * Validates a time format for jQuery's datepicker widget. |
||
161 | * |
||
162 | * @param string $format Format to validate. e.g. hh:mm:ssA (invalid) |
||
163 | * @return bool |
||
164 | */ |
||
165 | View Code Duplication | public static function validateTimeFormat($format) |
|
174 | |||
175 | /** |
||
176 | * Given a DateField instance, gets its PHP's date-format. |
||
177 | * |
||
178 | * @param \Cake\Datasource\EntityInterface $field DateField instance |
||
179 | * @return string PHP date-format for later use with date() function |
||
180 | */ |
||
181 | public static function getPHPFormat(EntityInterface $field) |
||
197 | } |
||
198 |