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 ) { |
||
37 | $this->constants_whitelist = $constants; |
||
38 | } |
||
39 | |||
40 | function get_constants_whitelist() { |
||
41 | return $this->constants_whitelist; |
||
42 | } |
||
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() { |
||
61 | |||
62 | function get_full_sync_actions() { |
||
65 | |||
66 | View Code Duplication | function maybe_sync_constants() { |
|
67 | if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) { |
||
68 | return; |
||
69 | } |
||
70 | |||
71 | $constants = $this->get_all_constants(); |
||
72 | if ( empty( $constants ) ) { |
||
73 | return; |
||
74 | } |
||
75 | |||
76 | set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_constants_wait_time ); |
||
77 | $constants_checksums = (array) get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, array() ); |
||
78 | |||
79 | foreach ( $constants as $name => $value ) { |
||
80 | $checksum = $this->get_check_sum( $value ); |
||
81 | // explicitly not using Identical comparison as get_option returns a string |
||
82 | if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum ) && ! is_null( $value ) ) { |
||
83 | /** |
||
84 | * Tells the client to sync a constant to the server |
||
85 | * |
||
86 | * @since 4.2.0 |
||
87 | * |
||
88 | * @param string The name of the constant |
||
89 | * @param mixed The value of the constant |
||
90 | */ |
||
91 | do_action( 'jetpack_sync_constant', $name, $value ); |
||
92 | $constants_checksums[ $name ] = $checksum; |
||
93 | } else { |
||
94 | $constants_checksums[ $name ] = $checksum; |
||
95 | } |
||
96 | } |
||
97 | update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums ); |
||
98 | } |
||
99 | |||
100 | // public so that we don't have to store an option for each constant |
||
101 | function get_all_constants() { |
||
102 | return array_combine( |
||
103 | $this->constants_whitelist, |
||
104 | array_map( array( $this, 'get_constant' ), $this->constants_whitelist ) |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | private function get_constant( $constant ) { |
||
109 | return ( defined( $constant ) ) ? |
||
110 | constant( $constant ) |
||
111 | : null; |
||
112 | } |
||
113 | |||
114 | public function expand_constants( $args ) { |
||
120 | } |
||
121 |
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.