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 |
||
8 | class GravityView_Field_Date_Created extends GravityView_Field { |
||
9 | |||
10 | var $name = 'date_created'; |
||
11 | |||
12 | var $search_operators = array( 'less_than', 'greater_than', 'is', 'isnot' ); |
||
13 | |||
14 | var $group = 'meta'; |
||
15 | |||
16 | var $contexts = array( 'single', 'multiple', 'export' ); |
||
17 | |||
18 | var $_custom_merge_tag = 'date_created'; |
||
19 | |||
20 | /** |
||
21 | * GravityView_Field_Date_Created constructor. |
||
22 | */ |
||
23 | View Code Duplication | public function __construct() { |
|
32 | |||
33 | function field_options( $field_options, $template_id, $field_id, $context, $input_type ) { |
||
43 | |||
44 | /** |
||
45 | * Filter the value of the field |
||
46 | * |
||
47 | * @todo Consider how to add to parent class |
||
48 | * |
||
49 | * @since 1.16 |
||
50 | * |
||
51 | * @param string $output HTML value output |
||
52 | * @param array $entry The GF entry array |
||
53 | * @param array $field_settings Settings for the particular GV field |
||
54 | * @param array $field Current field being displayed |
||
55 | * |
||
56 | * @return String values for this field based on the numeric values used by Gravity Forms |
||
57 | */ |
||
58 | public function get_content( $output = '', $entry = array(), $field_settings = array(), $field = array() ) { |
||
65 | |||
66 | /** |
||
67 | * Add {date_created} merge tag and format the values using format_date |
||
68 | * |
||
69 | * @since 1.16 |
||
70 | * |
||
71 | * @see http://docs.gravityview.co/article/281-the-createdby-merge-tag for usage information |
||
72 | * |
||
73 | * @param array $matches Array of Merge Tag matches found in text by preg_match_all |
||
74 | * @param string $text Text to replace |
||
75 | * @param array $form Gravity Forms form array |
||
76 | * @param array $entry Entry array |
||
77 | * @param bool $url_encode Whether to URL-encode output |
||
78 | * |
||
79 | * @return string Original text if {date_created} isn't found. Otherwise, replaced text. |
||
80 | */ |
||
81 | public function replace_merge_tag( $matches = array(), $text = '', $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) { |
||
100 | |||
101 | } |
||
102 | |||
103 | new GravityView_Field_Date_Created; |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.