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 |
||
7 | class VideoPress_Scheduler { |
||
8 | |||
9 | /** |
||
10 | * The name of the function used to run the cleanup cron. |
||
11 | */ |
||
12 | const CLEANUP_CRON_METHOD = 'videopress_cleanup_media_library'; |
||
13 | |||
14 | /** |
||
15 | * @var VideoPress_Scheduler |
||
16 | **/ |
||
17 | private static $instance = null; |
||
18 | |||
19 | /** |
||
20 | * A list of all of the crons that are to be activated, along with their interval timings. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $crons = array( |
||
25 | 'cleanup' => array( |
||
26 | 'method' => self::CLEANUP_CRON_METHOD, |
||
27 | 'interval' => 'minutes_30', |
||
28 | ), |
||
29 | ); |
||
30 | |||
31 | |||
32 | /** |
||
33 | * Private VideoPress_Scheduler constructor. |
||
34 | * |
||
35 | * Use the VideoPress_Scheduler::init() method to get an instance. |
||
36 | */ |
||
37 | private function __construct() { |
||
49 | |||
50 | /** |
||
51 | * Initialize the VideoPress_Scheduler and get back a singleton instance. |
||
52 | * |
||
53 | * @return VideoPress_Scheduler |
||
54 | */ |
||
55 | public static function init() { |
||
62 | |||
63 | /** |
||
64 | * Adds 30 minute running interval to the cron schedules. |
||
65 | * |
||
66 | * @param array $current_schedules Currently defined schedules list. |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | View Code Duplication | public function add_30_minute_cron_interval( $current_schedules ) { |
|
82 | |||
83 | /** |
||
84 | * Activate a single cron |
||
85 | * |
||
86 | * @param string $cron_name |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function activate_cron( $cron_name ) { |
||
100 | |||
101 | /** |
||
102 | * Activates widget update cron task. |
||
103 | */ |
||
104 | public function activate_all_crons() { |
||
116 | |||
117 | /** |
||
118 | * Only activate the crons if it is Jetpack that was activated. |
||
119 | * |
||
120 | * @param string $plugin_file_name |
||
121 | */ |
||
122 | public function activate_crons_on_jetpack_activation( $plugin_file_name ) { |
||
128 | |||
129 | /** |
||
130 | * Deactivates any crons associated with the VideoPress module. |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | public function deactivate_cron( $cron_name ) { |
||
145 | |||
146 | /** |
||
147 | * Deactivates any crons associated with the VideoPress module.. |
||
148 | */ |
||
149 | public function deactivate_all_crons() { |
||
155 | |||
156 | /** |
||
157 | * Is the given cron job currently active? |
||
158 | * |
||
159 | * If so, return when it will next run, |
||
160 | * |
||
161 | * @param string $cron_name |
||
162 | * |
||
163 | * @return int|bool Timestamp of the next run time OR false. |
||
164 | */ |
||
165 | public function check_cron( $cron_name ) { |
||
172 | |||
173 | /** |
||
174 | * Check that the given cron job name is valid. |
||
175 | * |
||
176 | * @param string $cron_name |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function is_cron_valid( $cron_name ) { |
||
188 | |||
189 | /** |
||
190 | * Get a list of all of the crons that are available. |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | public function get_crons() { |
||
197 | } |