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 |
||
5 | class Jetpack_Sync_Module_Constants extends Jetpack_Sync_Module { |
||
6 | const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum'; |
||
7 | const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await'; |
||
8 | |||
9 | public function name() { |
||
10 | return 'constants'; |
||
11 | } |
||
12 | |||
13 | public function init_listeners( $callable ) { |
||
14 | add_action( 'jetpack_sync_constant', $callable, 10, 2 ); |
||
15 | } |
||
16 | |||
17 | public function init_full_sync_listeners( $callable ) { |
||
18 | add_action( 'jetpack_full_sync_constants', $callable ); |
||
19 | } |
||
20 | |||
21 | public function init_before_send() { |
||
22 | add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_constants' ) ); |
||
23 | |||
24 | // full sync |
||
25 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_constants', array( $this, 'expand_constants' ) ); |
||
26 | } |
||
27 | |||
28 | public function reset_data() { |
||
29 | delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME ); |
||
30 | delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ); |
||
31 | } |
||
32 | |||
33 | function set_constants_whitelist( $constants ) { |
||
36 | |||
37 | function get_constants_whitelist() { |
||
40 | |||
41 | function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
42 | /** |
||
43 | * Tells the client to sync all constants to the server |
||
44 | * |
||
45 | * @since 4.2.0 |
||
46 | * |
||
47 | * @param boolean Whether to expand constants (should always be true) |
||
48 | */ |
||
49 | do_action( 'jetpack_full_sync_constants', true ); |
||
50 | |||
51 | // The number of actions enqueued, and next module state (true == done) |
||
52 | return array( 1, true ); |
||
53 | } |
||
54 | |||
55 | function estimate_full_sync_actions( $config ) { |
||
56 | return 1; |
||
57 | } |
||
58 | |||
59 | function get_full_sync_actions() { |
||
62 | |||
63 | View Code Duplication | function maybe_sync_constants() { |
|
64 | if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) { |
||
65 | return; |
||
66 | } |
||
67 | |||
68 | set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_constants_wait_time ); |
||
97 | |||
98 | // public so that we don't have to store an option for each constant |
||
99 | function get_all_constants() { |
||
106 | |||
107 | private function get_constant( $constant ) { |
||
112 | |||
113 | public function expand_constants( $args ) { |
||
120 | } |
||
121 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: