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 |
||
| 21 | class DateToolbox |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Maps jQuery's datepicker format tokens to PHP's |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected static $_map = [ |
||
| 30 | 'date' => [ |
||
| 31 | 'dd' => 'd', |
||
| 32 | 'd' => 'j', |
||
| 33 | 'oo' => 'z', |
||
| 34 | 'DD' => 'l', |
||
| 35 | 'D' => 'D', |
||
| 36 | 'mm' => 'm', |
||
| 37 | 'm' => 'n', |
||
| 38 | 'MM' => 'F', |
||
| 39 | 'M' => 'M', |
||
| 40 | 'yy' => 'Y', |
||
| 41 | 'y' => 'y', |
||
| 42 | '@' => 'U', |
||
| 43 | ], |
||
| 44 | 'time' => [ |
||
| 45 | 'HH' => 'G', |
||
| 46 | 'H' => 'H', |
||
| 47 | 'hh' => 'h', |
||
| 48 | 'h' => 'g', |
||
| 49 | 'mm' => 'i', |
||
| 50 | 'ss' => 's', |
||
| 51 | 'tt' => 'a', |
||
| 52 | 'TT' => 'A', |
||
| 53 | ] |
||
| 54 | ]; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Formats the given DateField accordingly to current view-mode. |
||
| 58 | * |
||
| 59 | * @param \Field\Model\Entity\Field $field The field to be formatted |
||
| 60 | * @return string Formated date. e.g. `2036-01-01` |
||
| 61 | */ |
||
| 62 | public static function formatField(Field $field) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Converts the given $date to a valid PHP's DateTime object using a jQuery's |
||
| 70 | * date/time $format. |
||
| 71 | * |
||
| 72 | * @param string $format A jQuery's date/time format. e.g. `'today is:' yy-mm-dd` |
||
| 73 | * @param string $date A date formatted using $format. e.g. `today is: 2015-01-30` |
||
| 74 | * @return \DateTime|false Date object on success, false on error |
||
| 75 | */ |
||
| 76 | public static function createFromFormat($format, $date) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Formats then given UNIX timestamp using the given jQuery format. |
||
| 93 | * |
||
| 94 | * @param string $format jQuery format. e.g. `'today is:' yy-mm-dd` |
||
| 95 | * @param int $timestamp Date as UNIX timestamp |
||
| 96 | * @return string Formated date. e.g. `today is: 2018-09-09` |
||
| 97 | */ |
||
| 98 | public static function formatDate($format, $timestamp) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Converts jQuery's date/time format to PHP's. |
||
| 122 | * |
||
| 123 | * @param string $format Date format coming from jQuery's datepicker widget. |
||
| 124 | * e.g. yy-mm-dd hh:mm |
||
| 125 | * @return string A valid date/time format to use with PHP |
||
| 126 | */ |
||
| 127 | public static function normalizeFormat($format) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Validates a date format for jQuery's datepicker widget. |
||
| 159 | * |
||
| 160 | * @param string $format Format to validate. e.g. yy:mm:ddQ (invalid) |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | View Code Duplication | public static function validateDateFormat($format) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Validates a time format for jQuery's datepicker widget. |
||
| 175 | * |
||
| 176 | * @param string $format Format to validate. e.g. hh:mm:ssA (invalid) |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | View Code Duplication | public static function validateTimeFormat($format) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Given a DateField instance, gets its PHP's date-format. |
||
| 191 | * |
||
| 192 | * @param \Cake\Datasource\EntityInterface $field DateField instance |
||
| 193 | * @return string PHP date-format for later use with date() function |
||
| 194 | */ |
||
| 195 | public static function getPHPFormat(EntityInterface $field) |
||
| 211 | } |
||
| 212 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.