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 |
||
32 | class DateOperator extends RangeOperator |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * Default configuration for this operator. |
||
37 | * |
||
38 | * ### Options: |
||
39 | * |
||
40 | * - field: Name of the table column which should be scoped, defaults to |
||
41 | * `created`. This column should be of type Date or DateTime. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $_defaultConfig = [ |
||
46 | 'field' => 'created', |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * Parses and extracts lower and upper date values from the given range given |
||
51 | * as `lower..upper`. |
||
52 | * |
||
53 | * Dates must be in YEAR-MONTH-DATE format. e.g. `2014-12-30`. It automatically |
||
54 | * reorder dates if they are given in inversed order (upper..lower). |
||
55 | * |
||
56 | * @param string $value A date range given as `<dateLower>..<dateUpper>`. For |
||
57 | * instance. `2014-12-30..2015-12-30` |
||
58 | * @return array Associative array with two keys: `lower` and `upper`, returned |
||
59 | * dates are fully PHP compliant |
||
60 | */ |
||
61 | protected function _parseRange($value) |
||
75 | |||
76 | /** |
||
77 | * Normalizes the given date. |
||
78 | * |
||
79 | * @param string $date Date to normalize |
||
80 | * @return string Date formated as `Y-m-d` |
||
81 | */ |
||
82 | protected function _normalize($date) |
||
113 | } |
||
114 |