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 |
||
9 | class GravityView_Field_Is_Fulfilled extends GravityView_Field { |
||
10 | |||
11 | var $name = 'is_fulfilled'; |
||
12 | |||
13 | var $is_searchable = true; |
||
14 | |||
15 | var $is_numeric = false; |
||
16 | |||
17 | var $search_operators = array( 'is', 'isnot' ); |
||
18 | |||
19 | var $group = 'pricing'; |
||
20 | |||
21 | var $_custom_merge_tag = 'is_fulfilled'; |
||
22 | |||
23 | /** |
||
24 | * @var int The value used by Gravity Forms when the order has not been fulfilled |
||
25 | */ |
||
26 | const NOT_FULFILLED = 0; |
||
27 | |||
28 | /** |
||
29 | * @var int The value used by Gravity Forms when the order has been fulfilled |
||
30 | */ |
||
31 | const FULFILLED = 1; |
||
32 | |||
33 | /** |
||
34 | * GravityView_Field_Is_Fulfilled constructor. |
||
35 | */ |
||
36 | public function __construct() { |
||
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, $field_settings, $field ) { |
||
65 | |||
66 | /** |
||
67 | * Get the string output based on the numeric value used by Gravity Forms |
||
68 | * |
||
69 | * @since 1.16 |
||
70 | * |
||
71 | * @param int|string $value Number value for the field |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | View Code Duplication | private function get_string_from_value( $value ) { |
|
90 | |||
91 | /** |
||
92 | * Add {is_fulfilled} merge tag |
||
93 | * |
||
94 | * @since 1.16 |
||
95 | ** |
||
96 | * @param array $matches Array of Merge Tag matches found in text by preg_match_all |
||
97 | * @param string $text Text to replace |
||
98 | * @param array $form Gravity Forms form array |
||
99 | * @param array $entry Entry array |
||
100 | * @param bool $url_encode Whether to URL-encode output |
||
101 | * |
||
102 | * @return string Original text if {is_fulfilled} isn't found. Otherwise, "Not Fulfilled" or "Fulfilled" |
||
103 | */ |
||
104 | public function replace_merge_tag( $matches = array(), $text = '', $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) { |
||
123 | } |
||
124 | |||
126 |
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.