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 |
||
16 | class Heartbeat { |
||
17 | |||
18 | /** |
||
19 | * Holds the singleton instance of this class |
||
20 | * |
||
21 | * @since 2.3.3 |
||
22 | * @var Heartbeat |
||
23 | */ |
||
24 | private static $instance = false; |
||
25 | |||
26 | /** |
||
27 | * Cronjob identifier |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | private $cron_name = 'jetpack_v2_heartbeat'; |
||
32 | |||
33 | /** |
||
34 | * Singleton |
||
35 | * |
||
36 | * @since 2.3.3 |
||
37 | * @static |
||
38 | * @return Heartbeat |
||
39 | */ |
||
40 | public static function init() { |
||
47 | |||
48 | /** |
||
49 | * Constructor for singleton |
||
50 | * |
||
51 | * @since 2.3.3 |
||
52 | * @return Heartbeat |
||
|
|||
53 | */ |
||
54 | View Code Duplication | private function __construct() { |
|
71 | |||
72 | /** |
||
73 | * Method that gets executed on the wp-cron call |
||
74 | * |
||
75 | * @since 2.3.3 |
||
76 | * @global string $wp_version |
||
77 | */ |
||
78 | View Code Duplication | public function cron_exec() { |
|
118 | |||
119 | /** |
||
120 | * Generates heartbeat stats data. |
||
121 | * |
||
122 | * @param string $prefix Prefix to add before stats identifier. |
||
123 | * |
||
124 | * @return array The stats array. |
||
125 | */ |
||
126 | public static function generate_stats_array( $prefix = '' ) { |
||
156 | |||
157 | /** |
||
158 | * Registers jetpack.getHeartbeatData xmlrpc method |
||
159 | * |
||
160 | * @param array $methods The list of methods to be filtered. |
||
161 | * @return array $methods |
||
162 | */ |
||
163 | public static function jetpack_xmlrpc_methods( $methods ) { |
||
167 | |||
168 | /** |
||
169 | * Handles the response for the jetpack.getHeartbeatData xmlrpc method |
||
170 | * |
||
171 | * @param array $params The parameters received in the request. |
||
172 | * @return array $params all the stats that hearbeat handles. |
||
173 | */ |
||
174 | public static function xmlrpc_data_response( $params = array() ) { |
||
181 | |||
182 | /** |
||
183 | * Clear scheduled events |
||
184 | * |
||
185 | * @return void |
||
186 | */ |
||
187 | View Code Duplication | public function deactivate() { |
|
197 | |||
198 | } |
||
199 |
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.