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 | private $constants_whitelist; |
||
| 14 | |||
| 15 | public function set_defaults() { |
||
| 16 | $this->constants_whitelist = Jetpack_Sync_Defaults::$default_constants_whitelist; |
||
|
|
|||
| 17 | } |
||
| 18 | |||
| 19 | public function init_listeners( $callable ) { |
||
| 20 | add_action( 'jetpack_sync_constant', $callable, 10, 2 ); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function init_full_sync_listeners( $callable ) { |
||
| 24 | add_action( 'jetpack_full_sync_constants', $callable ); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function init_before_send() { |
||
| 28 | add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_constants' ) ); |
||
| 29 | |||
| 30 | // full sync |
||
| 31 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_constants', array( $this, 'expand_constants' ) ); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function reset_data() { |
||
| 35 | delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME ); |
||
| 36 | delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ); |
||
| 37 | } |
||
| 38 | |||
| 39 | function set_constants_whitelist( $constants ) { |
||
| 40 | $this->constants_whitelist = $constants; |
||
| 41 | } |
||
| 42 | |||
| 43 | function get_constants_whitelist() { |
||
| 44 | return $this->constants_whitelist; |
||
| 45 | } |
||
| 46 | |||
| 47 | function enqueue_full_sync_actions( $config ) { |
||
| 48 | /** |
||
| 49 | * Tells the client to sync all constants to the server |
||
| 50 | * |
||
| 51 | * @since 4.2.0 |
||
| 52 | * |
||
| 53 | * @param boolean Whether to expand constants (should always be true) |
||
| 54 | */ |
||
| 55 | do_action( 'jetpack_full_sync_constants', true ); |
||
| 56 | |||
| 57 | return 1; |
||
| 58 | } |
||
| 59 | |||
| 60 | function estimate_full_sync_actions( $config ) { |
||
| 61 | return 1; |
||
| 62 | } |
||
| 63 | |||
| 64 | function get_full_sync_actions() { |
||
| 67 | |||
| 68 | View Code Duplication | function maybe_sync_constants() { |
|
| 101 | |||
| 102 | // public so that we don't have to store an option for each constant |
||
| 103 | function get_all_constants() { |
||
| 109 | |||
| 110 | private function get_constant( $constant ) { |
||
| 115 | |||
| 116 | public function expand_constants( $args ) { |
||
| 123 | } |
||
| 124 |
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.