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_Callables extends Jetpack_Sync_Module { |
||
6 | const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum'; |
||
7 | const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await'; |
||
8 | |||
9 | private $callable_whitelist; |
||
10 | |||
11 | public function name() { |
||
14 | |||
15 | public function set_defaults() { |
||
22 | |||
23 | public function init_listeners( $callable ) { |
||
33 | |||
34 | public function init_before_send() { |
||
35 | add_action( 'jetpack_sync_before_send', array( $this, 'maybe_sync_callables' ) ); |
||
36 | |||
37 | // full sync |
||
38 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) ); |
||
39 | } |
||
40 | |||
41 | public function reset_data() { |
||
42 | delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME ); |
||
43 | delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
||
44 | } |
||
45 | |||
46 | function set_callable_whitelist( $callables ) { |
||
47 | $this->callable_whitelist = $callables; |
||
48 | } |
||
49 | |||
50 | function get_callable_whitelist() { |
||
51 | return $this->callable_whitelist; |
||
52 | } |
||
53 | |||
54 | public function get_all_callables() { |
||
55 | // get_all_callables should run as the master user always. |
||
56 | $current_user_id = get_current_user_id(); |
||
57 | wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) ); |
||
58 | $callables = array_combine( |
||
59 | array_keys( $this->callable_whitelist ), |
||
60 | array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) ) |
||
61 | ); |
||
62 | wp_set_current_user( $current_user_id ); |
||
63 | return $callables; |
||
64 | } |
||
65 | |||
66 | private function get_callable( $callable ) { |
||
67 | return call_user_func( $callable ); |
||
68 | } |
||
69 | |||
70 | public function enqueue_full_sync_actions() { |
||
81 | |||
82 | public function get_full_sync_actions() { |
||
85 | |||
86 | public function force_sync_callables() { |
||
87 | delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME ); |
||
88 | delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
||
89 | $this->maybe_sync_callables(); |
||
90 | } |
||
91 | |||
92 | View Code Duplication | public function maybe_sync_callables() { |
|
93 | if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) { |
||
94 | return; |
||
95 | } |
||
96 | |||
97 | $callables = $this->get_all_callables(); |
||
98 | |||
99 | if ( empty( $callables ) ) { |
||
100 | return; |
||
101 | } |
||
102 | |||
103 | set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_callables_wait_time ); |
||
104 | |||
105 | $callable_checksums = (array) get_option( self::CALLABLES_CHECKSUM_OPTION_NAME , array() ); |
||
106 | |||
107 | // only send the callables that have changed |
||
108 | foreach ( $callables as $name => $value ) { |
||
109 | $checksum = $this->get_check_sum( $value ); |
||
110 | // explicitly not using Identical comparison as get_option returns a string |
||
111 | if ( ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ) && ! is_null( $value ) ) { |
||
112 | /** |
||
113 | * Tells the client to sync a callable (aka function) to the server |
||
114 | * |
||
115 | * @since 4.2.0 |
||
116 | * |
||
117 | * @param string The name of the callable |
||
118 | * @param mixed The value of the callable |
||
119 | */ |
||
120 | do_action( 'jetpack_sync_callable', $name, $value ); |
||
121 | $callable_checksums[ $name ] = $checksum; |
||
122 | } else { |
||
123 | $callable_checksums[ $name ] = $checksum; |
||
124 | } |
||
125 | } |
||
126 | update_option( self::CALLABLES_CHECKSUM_OPTION_NAME , $callable_checksums ); |
||
127 | } |
||
128 | |||
129 | public function expand_callables( $args ) { |
||
135 | } |
||
136 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.