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_Heartbeat { |
||
6 | |||
7 | /** |
||
8 | * Holds the singleton instance of this class |
||
9 | * |
||
10 | * @since 2.3.3 |
||
11 | * @var Jetpack_Heartbeat |
||
12 | */ |
||
13 | private static $instance = false; |
||
14 | |||
15 | private $cron_name = 'jetpack_v2_heartbeat'; |
||
16 | |||
17 | /** |
||
18 | * Singleton |
||
19 | * |
||
20 | * @since 2.3.3 |
||
21 | * @static |
||
22 | * @return Jetpack_Heartbeat |
||
23 | */ |
||
24 | public static function init() { |
||
31 | |||
32 | /** |
||
33 | * Constructor for singleton |
||
34 | * |
||
35 | * @since 2.3.3 |
||
36 | * @return Jetpack_Heartbeat |
||
|
|||
37 | */ |
||
38 | View Code Duplication | private function __construct() { |
|
39 | if ( ! Jetpack::is_active() ) { |
||
40 | return; |
||
41 | } |
||
42 | |||
43 | // Schedule the task |
||
44 | add_action( $this->cron_name, array( $this, 'cron_exec' ) ); |
||
45 | |||
46 | if ( ! wp_next_scheduled( $this->cron_name ) ) { |
||
47 | // Deal with the old pre-3.0 weekly one. |
||
48 | if ( $timestamp = wp_next_scheduled( 'jetpack_heartbeat' ) ) { |
||
49 | wp_unschedule_event( $timestamp, 'jetpack_heartbeat' ); |
||
50 | } |
||
51 | |||
52 | wp_schedule_event( time(), 'daily', $this->cron_name ); |
||
53 | } |
||
54 | |||
55 | add_filter( 'jetpack_xmlrpc_methods', array( __CLASS__, 'jetpack_xmlrpc_methods' ) ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Method that gets executed on the wp-cron call |
||
60 | * |
||
61 | * @since 2.3.3 |
||
62 | * @global string $wp_version |
||
63 | */ |
||
64 | View Code Duplication | public function cron_exec() { |
|
65 | |||
66 | $jetpack = Jetpack::init(); |
||
67 | |||
68 | /* |
||
69 | * This should run daily. Figuring in for variances in |
||
70 | * WP_CRON, don't let it run more than every 23 hours at most. |
||
71 | * |
||
72 | * i.e. if it ran less than 23 hours ago, fail out. |
||
73 | */ |
||
74 | $last = (int) Jetpack_Options::get_option( 'last_heartbeat' ); |
||
75 | if ( $last && ( $last + DAY_IN_SECONDS - HOUR_IN_SECONDS > time() ) ) { |
||
76 | return; |
||
77 | } |
||
78 | |||
79 | /* |
||
80 | * Check for an identity crisis |
||
81 | * |
||
82 | * If one exists: |
||
83 | * - Bump stat for ID crisis |
||
84 | * - Email site admin about potential ID crisis |
||
85 | */ |
||
86 | |||
87 | // Coming Soon! |
||
88 | |||
89 | foreach ( self::generate_stats_array( 'v2-' ) as $key => $value ) { |
||
90 | $jetpack->stat( $key, $value ); |
||
91 | } |
||
92 | |||
93 | Jetpack_Options::update_option( 'last_heartbeat', time() ); |
||
94 | |||
95 | $jetpack->do_stats( 'server_side' ); |
||
96 | |||
97 | /** |
||
98 | * Fires when we synchronize all registered options on heartbeat. |
||
99 | * |
||
100 | * @since 3.3.0 |
||
101 | */ |
||
102 | do_action( 'jetpack_heartbeat' ); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Generates heartbeat stats data. |
||
107 | * |
||
108 | * @param string $prefix Prefix to add before stats identifier. |
||
109 | * |
||
110 | * @return array The stats array. |
||
111 | */ |
||
112 | public static function generate_stats_array( $prefix = '' ) { |
||
164 | |||
165 | public static function jetpack_xmlrpc_methods( $methods ) { |
||
169 | |||
170 | public static function xmlrpc_data_response( $params = array() ) { |
||
177 | |||
178 | View Code Duplication | public function deactivate() { |
|
179 | // Deal with the old pre-3.0 weekly one. |
||
187 | |||
188 | } |
||
189 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.