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_full_sync_listeners( $callable ) { |
||
| 35 | add_action( 'jetpack_full_sync_callables', $callable ); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function init_before_send() { |
||
| 44 | |||
| 45 | public function reset_data() { |
||
| 49 | |||
| 50 | function set_callable_whitelist( $callables ) { |
||
| 53 | |||
| 54 | function get_callable_whitelist() { |
||
| 57 | |||
| 58 | public function get_all_callables() { |
||
| 70 | |||
| 71 | private function get_callable( $callable ) { |
||
| 74 | |||
| 75 | public function enqueue_full_sync_actions( $config ) { |
||
| 87 | |||
| 88 | public function estimate_full_sync_actions( $config ) { |
||
| 89 | return 1; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function get_full_sync_actions() { |
||
| 95 | |||
| 96 | public function unlock_sync_callable() { |
||
| 99 | |||
| 100 | View Code Duplication | public function maybe_sync_callables() { |
|
| 101 | if ( ! is_admin() ) { |
||
| 102 | return; |
||
| 103 | } |
||
| 104 | |||
| 105 | if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) { |
||
| 106 | return; |
||
| 107 | } |
||
| 108 | |||
| 109 | set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_callables_wait_time ); |
||
| 110 | |||
| 111 | $callables = $this->get_all_callables(); |
||
| 112 | |||
| 113 | if ( empty( $callables ) ) { |
||
| 114 | return; |
||
| 115 | } |
||
| 116 | |||
| 117 | $callable_checksums = (array) get_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() ); |
||
| 118 | |||
| 119 | // only send the callables that have changed |
||
| 120 | foreach ( $callables as $name => $value ) { |
||
| 121 | $checksum = $this->get_check_sum( $value ); |
||
| 122 | // explicitly not using Identical comparison as get_option returns a string |
||
| 123 | if ( ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ) && ! is_null( $value ) ) { |
||
| 124 | /** |
||
| 125 | * Tells the client to sync a callable (aka function) to the server |
||
| 126 | * |
||
| 127 | * @since 4.2.0 |
||
| 128 | * |
||
| 129 | * @param string The name of the callable |
||
| 130 | * @param mixed The value of the callable |
||
| 131 | */ |
||
| 132 | do_action( 'jetpack_sync_callable', $name, $value ); |
||
| 133 | $callable_checksums[ $name ] = $checksum; |
||
| 134 | } else { |
||
| 135 | $callable_checksums[ $name ] = $checksum; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | update_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums ); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function expand_callables( $args ) { |
||
| 148 | } |
||
| 149 |
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.