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 |
||
| 12 | class Remote_Notifications { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Plugin version, used for cache-busting of style and script file references. |
||
| 16 | *"" |
||
| 17 | * @since 1.0.0 |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | const VERSION = '1.3.0'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Unique identifier for your plugin. |
||
| 25 | * |
||
| 26 | * |
||
| 27 | * The variable name is used as the text domain when internationalizing strings |
||
| 28 | * of text. Its value should match the Text Domain file header in the main |
||
| 29 | * plugin file. |
||
| 30 | * |
||
| 31 | * @since 1.0.0 |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $plugin_slug = 'remote-notifications'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Instance of this class. |
||
| 39 | * |
||
| 40 | * @since 1.0.0 |
||
| 41 | * |
||
| 42 | * @var object |
||
| 43 | */ |
||
| 44 | protected static $instance = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Initialize the plugin by setting localization and loading public scripts |
||
| 48 | * and styles. |
||
| 49 | * |
||
| 50 | * @since 1.0.0 |
||
| 51 | */ |
||
| 52 | public function __construct() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Return the plugin slug. |
||
| 72 | * |
||
| 73 | * @since 1.0.0 |
||
| 74 | * |
||
| 75 | * @return string Plugin slug variable. |
||
| 76 | */ |
||
| 77 | public function get_plugin_slug() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Return an instance of this class. |
||
| 83 | * |
||
| 84 | * @since 1.0.0 |
||
| 85 | * |
||
| 86 | * @return object A single instance of this class. |
||
| 87 | */ |
||
| 88 | public static function get_instance() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Fired when the plugin is activated. |
||
| 100 | * |
||
| 101 | * @since 1.0.0 |
||
| 102 | * |
||
| 103 | * @param boolean $network_wide True if WPMU superadmin uses |
||
| 104 | * "Network Activate" action, false if |
||
| 105 | * WPMU is disabled or plugin is |
||
| 106 | * activated on an individual blog. |
||
| 107 | */ |
||
| 108 | View Code Duplication | public static function activate( $network_wide ) { |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Fired when the plugin is deactivated. |
||
| 137 | * |
||
| 138 | * @since 1.0.0 |
||
| 139 | * |
||
| 140 | * @param boolean $network_wide True if WPMU superadmin uses |
||
| 141 | * "Network Deactivate" action, false if |
||
| 142 | * WPMU is disabled or plugin is |
||
| 143 | * deactivated on an individual blog. |
||
| 144 | */ |
||
| 145 | View Code Duplication | public static function deactivate( $network_wide ) { |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Fired when a new site is activated with a WPMU environment. |
||
| 175 | * |
||
| 176 | * @since 1.0.0 |
||
| 177 | * |
||
| 178 | * @param int $blog_id ID of the new blog. |
||
| 179 | */ |
||
| 180 | public function activate_new_site( $blog_id ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get all blog ids of blogs in the current network that are: |
||
| 194 | * - not archived |
||
| 195 | * - not spam |
||
| 196 | * - not deleted |
||
| 197 | * |
||
| 198 | * @since 1.0.0 |
||
| 199 | * |
||
| 200 | * @return array|false The blog ids, false if no matches. |
||
| 201 | */ |
||
| 202 | private static function get_blog_ids() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Fired for each blog when the plugin is activated. |
||
| 217 | * |
||
| 218 | * @since 1.0.0 |
||
| 219 | */ |
||
| 220 | private static function single_activate() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Fired for each blog when the plugin is deactivated. |
||
| 226 | * |
||
| 227 | * @since 1.0.0 |
||
| 228 | */ |
||
| 229 | private static function single_deactivate() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Load the plugin text domain for translation. |
||
| 235 | * |
||
| 236 | * @since 1.0.0 |
||
| 237 | */ |
||
| 238 | public function load_plugin_textdomain() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Register and enqueue public-facing style sheet. |
||
| 249 | * |
||
| 250 | * @since 1.0.0 |
||
| 251 | */ |
||
| 252 | public function enqueue_styles() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Register and enqueues public-facing JavaScript files. |
||
| 258 | * |
||
| 259 | * @since 1.0.0 |
||
| 260 | */ |
||
| 261 | public function enqueue_scripts() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Create API endpoint inside the custom post type template |
||
| 267 | * |
||
| 268 | * @since 1.0.0 |
||
| 269 | */ |
||
| 270 | public function endpoint() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Register notification post type |
||
| 291 | * |
||
| 292 | * @since 1.0.0 |
||
| 293 | */ |
||
| 294 | public function register_notification_post_type() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Custom updated messages |
||
| 335 | * |
||
| 336 | * @param array $messages Post types messages array |
||
| 337 | * |
||
| 338 | * @return array Messages array with our post type messages added |
||
| 339 | */ |
||
| 340 | function updated_messages( $messages ) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Register the "Channels" taxonomy |
||
| 371 | * |
||
| 372 | * @since 1.0.0 |
||
| 373 | */ |
||
| 374 | View Code Duplication | public function register_channel() { |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Register the post type taxonomy |
||
| 405 | * |
||
| 406 | * This taxonomy will be used to limit notices |
||
| 407 | * display on specific post types only. |
||
| 408 | * |
||
| 409 | * @since 1.0.0 |
||
| 410 | */ |
||
| 411 | View Code Duplication | public function register_post_type() { |
|
| 439 | |||
| 440 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.