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 |
||
| 3 | class Jetpack_Sync_Module_Constants extends Jetpack_Sync_Module { |
||
| 4 | const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum'; |
||
| 5 | const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await'; |
||
| 6 | |||
| 7 | public function name() { |
||
| 10 | |||
| 11 | private $constants_whitelist; |
||
| 12 | |||
| 13 | public function set_defaults() { |
||
| 16 | |||
| 17 | public function init_listeners( $callable ) { |
||
| 23 | |||
| 24 | public function init_before_send() { |
||
| 25 | add_action( 'jetpack_sync_before_send', array( $this, 'maybe_sync_constants' ) ); |
||
| 26 | |||
| 27 | // full sync |
||
| 28 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_constants', array( $this, 'expand_constants' ) ); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function reset_data() { |
||
| 32 | delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME ); |
||
| 33 | delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ); |
||
| 34 | } |
||
| 35 | |||
| 36 | function set_constants_whitelist( $constants ) { |
||
| 39 | |||
| 40 | function get_constants_whitelist() { |
||
| 43 | |||
| 44 | function force_sync_constants() { |
||
| 45 | delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME ); |
||
| 46 | delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ); |
||
| 47 | $this->maybe_sync_constants(); |
||
| 48 | } |
||
| 49 | |||
| 50 | function enqueue_full_sync_actions() { |
||
| 51 | /** |
||
| 52 | * Tells the client to sync all constants to the server |
||
| 53 | * |
||
| 54 | * @since 4.2.0 |
||
| 55 | * |
||
| 56 | * @param boolean Whether to expand constants (should always be true) |
||
| 57 | */ |
||
| 58 | do_action( 'jetpack_full_sync_constants', true ); |
||
| 59 | |||
| 60 | return 1; |
||
| 61 | } |
||
| 62 | |||
| 63 | function get_full_sync_actions() { |
||
| 64 | return array( 'jetpack_full_sync_constants' ); |
||
| 65 | } |
||
| 66 | |||
| 67 | View Code Duplication | function maybe_sync_constants() { |
|
| 100 | |||
| 101 | // public so that we don't have to store an option for each constant |
||
| 102 | function get_all_constants() { |
||
| 108 | |||
| 109 | private function get_constant( $constant ) { |
||
| 114 | |||
| 115 | public function expand_constants( $args ) { |
||
| 116 | if ( $args[0] ) { |
||
| 117 | return $this->get_all_constants(); |
||
| 118 | } |
||
| 122 | } |
||
| 123 |
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.