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 |
||
28 | class EE_Brewing_Regular extends EE_BASE { |
||
29 | |||
30 | /** |
||
31 | * @var \EventEspresso\core\services\database\TableAnalysis $table_analysis |
||
32 | */ |
||
33 | protected $_table_analysis; |
||
34 | |||
35 | /** |
||
36 | * EE_Brewing_Regular constructor. |
||
37 | */ |
||
38 | public function __construct( TableAnalysis $table_analysis ) { |
||
39 | $this->_table_analysis = $table_analysis; |
||
40 | if ( defined( 'EE_CAFF_PATH' ) ) { |
||
41 | // activation |
||
42 | add_action( 'AHEE__EEH_Activation__initialize_db_content', array( $this, 'initialize_caf_db_content' ) ); |
||
43 | // load caff init |
||
44 | add_action( 'AHEE__EE_System__set_hooks_for_core', array( $this, 'caffeinated_init' ) ); |
||
45 | // remove the "powered by" credit link from receipts and invoices |
||
46 | add_filter( 'FHEE_EE_Html_messenger__add_powered_by_credit_link_to_receipt_and_invoice', '__return_false' ); |
||
47 | // add caffeinated modules |
||
48 | add_filter( |
||
49 | 'FHEE__EE_Config__register_modules__modules_to_register', |
||
50 | array( $this, 'caffeinated_modules_to_register' ) |
||
51 | ); |
||
52 | // load caff scripts |
||
53 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_caffeinated_scripts' ), 10 ); |
||
54 | add_filter( 'FHEE__EE_Registry__load_helper__helper_paths', array( $this, 'caf_helper_paths' ), 10 ); |
||
55 | add_filter( |
||
56 | 'FHEE__EE_Payment_Method_Manager__register_payment_methods__payment_methods_to_register', |
||
57 | array( $this, 'caf_payment_methods' ) |
||
58 | ); |
||
59 | // caffeinated constructed |
||
60 | do_action( 'AHEE__EE_Brewing_Regular__construct__complete' ); |
||
61 | //seeing how this is caf, which isn't put on WordPress.org, we can have affiliate links without a disclaimer |
||
62 | add_filter( 'FHEE__ee_show_affiliate_links', '__return_false' ); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | |||
67 | |||
68 | /** |
||
69 | * callback for the FHEE__EE_Registry__load_helper__helper_paths filter to add the caffeinated paths |
||
70 | * |
||
71 | * @param array $paths original helper paths array |
||
72 | * @return array new array of paths |
||
73 | */ |
||
74 | public function caf_helper_paths( $paths ) { |
||
78 | |||
79 | |||
80 | |||
81 | /** |
||
82 | * Upon brand-new activation, if this is a new activation of CAF, we want to add |
||
83 | * some global prices that will show off EE4's capabilities. However, if they're upgrading |
||
84 | * from 3.1, or simply EE4.x decaf, we assume they don't want us to suddenly introduce these extra prices. |
||
85 | * This action should only be called when EE 4.x.0.P is initially activated. |
||
86 | * Right now the only CAF content are these global prices. If there's more in the future, then |
||
87 | * we should probably create a caf file to contain it all instead just a function like this. |
||
88 | * Right now, we ASSUME the only price types in the system are default ones |
||
89 | * |
||
90 | * @global wpdb $wpdb |
||
91 | */ |
||
92 | public function initialize_caf_db_content(){ |
||
174 | |||
175 | |||
176 | |||
177 | /** |
||
178 | * caffeinated_modules_to_register |
||
179 | * |
||
180 | * @access public |
||
181 | * @param array $modules_to_register |
||
182 | * @return array |
||
183 | */ |
||
184 | public function caffeinated_modules_to_register( $modules_to_register = array() ) { |
||
193 | |||
194 | |||
195 | |||
196 | public function caffeinated_init() { |
||
205 | |||
206 | |||
207 | |||
208 | public function enqueue_caffeinated_scripts() { |
||
211 | |||
212 | |||
213 | |||
214 | /** |
||
215 | * callbacks below here |
||
216 | * |
||
217 | * @param array $taxonomy_array |
||
218 | * @return array |
||
219 | */ |
||
220 | public function filter_taxonomies( array $taxonomy_array ) { |
||
224 | |||
225 | |||
226 | |||
227 | /** |
||
228 | * @param array $cpt_array |
||
229 | * @return mixed |
||
230 | */ |
||
231 | public function filter_cpts( array $cpt_array ) { |
||
235 | |||
236 | |||
237 | |||
238 | /** |
||
239 | * @param array $menuitems |
||
240 | * @return array |
||
241 | */ |
||
242 | View Code Duplication | public function nav_metabox_items( array $menuitems ) { |
|
250 | |||
251 | |||
252 | |||
253 | /** |
||
254 | * Adds the payment methods in {event-espresso-core}/caffeinated/payment_methods |
||
255 | * |
||
256 | * @param array $payment_method_paths |
||
257 | * @return array values are folder paths to payment method folders |
||
258 | */ |
||
259 | public function caf_payment_methods( $payment_method_paths ) { |
||
264 | /** |
||
265 | * Gets the injected table analyzer, or throws an exception |
||
266 | * @return TableAnalysis |
||
267 | * @throws \EE_Error |
||
268 | */ |
||
269 | View Code Duplication | protected function _get_table_analysis() { |
|
281 | } |
||
282 | $brewing = new EE_Brewing_Regular( |
||
285 |
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.