Complex classes like Give_Email_Notifications often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_Email_Notifications, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Give_Email_Notifications { |
||
| 18 | /** |
||
| 19 | * Instance. |
||
| 20 | * |
||
| 21 | * @since 1.9 |
||
| 22 | * @access static |
||
| 23 | * @var |
||
| 24 | */ |
||
| 25 | static private $instance; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Array of email notifications. |
||
| 29 | * |
||
| 30 | * @since 1.9 |
||
| 31 | * @access private |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $emails = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Singleton pattern. |
||
| 38 | * |
||
| 39 | * @since 1.9 |
||
| 40 | * @access private |
||
| 41 | * Give_Payumoney_API constructor. |
||
| 42 | */ |
||
| 43 | private function __construct() { |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Get instance. |
||
| 49 | * |
||
| 50 | * @since 1.9 |
||
| 51 | * @access static |
||
| 52 | * @return static |
||
| 53 | */ |
||
| 54 | static function get_instance() { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Setup dependencies |
||
| 64 | * |
||
| 65 | * @since 1.9 |
||
| 66 | */ |
||
| 67 | public function init() { |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * Add setting to metabox. |
||
| 98 | * |
||
| 99 | * @since 1.9 |
||
| 100 | * @access public |
||
| 101 | * |
||
| 102 | * @param array $settings |
||
| 103 | * @param int $post_id |
||
| 104 | * |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | public function add_metabox_setting_fields( $settings, $post_id ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Add email notifications |
||
| 133 | * |
||
| 134 | * @since 1.9 |
||
| 135 | * @access private |
||
| 136 | */ |
||
| 137 | private function add_emails_notifications() { |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Get list of email notifications. |
||
| 170 | * |
||
| 171 | * @since 1.9 |
||
| 172 | * @access public |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | public function get_email_notifications() { |
||
| 178 | |||
| 179 | |||
| 180 | public function get_columns() { |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Get name column. |
||
| 198 | * |
||
| 199 | * @since 1.9 |
||
| 200 | * @access public |
||
| 201 | * |
||
| 202 | * @param Give_Email_Notification $email |
||
| 203 | */ |
||
| 204 | public function get_name_column( Give_Email_Notification $email ) { |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Print row actions. |
||
| 220 | * |
||
| 221 | * @since 1.9 |
||
| 222 | * @access private |
||
| 223 | * |
||
| 224 | * @param Give_Email_Notification $email |
||
| 225 | */ |
||
| 226 | private function print_row_actions( $email ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get recipient column. |
||
| 249 | * |
||
| 250 | * @since 1.9 |
||
| 251 | * @access public |
||
| 252 | * |
||
| 253 | * @param Give_Email_Notification $email |
||
| 254 | */ |
||
| 255 | public function get_recipient_column( Give_Email_Notification $email ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get status column. |
||
| 276 | * |
||
| 277 | * @since 1.9 |
||
| 278 | * @access public |
||
| 279 | * |
||
| 280 | * @param Give_Email_Notification $email |
||
| 281 | */ |
||
| 282 | public function get_status_column( Give_Email_Notification $email ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get email_type column. |
||
| 298 | * |
||
| 299 | * @since 1.9 |
||
| 300 | * @access public |
||
| 301 | * |
||
| 302 | * @param Give_Email_Notification $email |
||
| 303 | */ |
||
| 304 | public function get_email_type_column( Give_Email_Notification $email ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get setting column. |
||
| 314 | * |
||
| 315 | * @since 1.9 |
||
| 316 | * @access public |
||
| 317 | * |
||
| 318 | * @param Give_Email_Notification $email |
||
| 319 | */ |
||
| 320 | public function get_setting_column( Give_Email_Notification $email ) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Render column. |
||
| 330 | * |
||
| 331 | * @since 1.9 |
||
| 332 | * @access public |
||
| 333 | * |
||
| 334 | * @param Give_Email_Notification $email |
||
| 335 | * @param string $column_name |
||
| 336 | */ |
||
| 337 | public function render_column( Give_Email_Notification $email, $column_name ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Check if admin preview email or not |
||
| 347 | * |
||
| 348 | * @since 1.9 |
||
| 349 | * @access public |
||
| 350 | * @return bool $is_preview |
||
| 351 | */ |
||
| 352 | public function is_preview_email() { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Check if admin preview email or not |
||
| 368 | * |
||
| 369 | * @since 1.9 |
||
| 370 | * @access public |
||
| 371 | * @return bool $is_preview |
||
| 372 | */ |
||
| 373 | public function is_send_preview_email() { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Displays the email preview |
||
| 389 | * |
||
| 390 | * @since 1.9 |
||
| 391 | * @access public |
||
| 392 | * @return bool|null |
||
| 393 | */ |
||
| 394 | public function preview_email() { |
||
| 452 | |||
| 453 | |||
| 454 | /** |
||
| 455 | * Add header to donation receipt email preview |
||
| 456 | * |
||
| 457 | * @since 1.9 |
||
| 458 | * @access public |
||
| 459 | * |
||
| 460 | * @param Give_Email_Notification $email |
||
| 461 | */ |
||
| 462 | public function email_preview_header( $email ) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Add email preview data |
||
| 486 | * |
||
| 487 | * @since 1.9 |
||
| 488 | * @access public |
||
| 489 | * |
||
| 490 | * @param array $email_preview_data |
||
| 491 | * |
||
| 492 | * @return array |
||
| 493 | */ |
||
| 494 | public function email_preview_data( $email_preview_data ) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Replace email template tags. |
||
| 503 | * |
||
| 504 | * @since 1.9 |
||
| 505 | * @access public |
||
| 506 | * |
||
| 507 | * @param string $email_message |
||
| 508 | * @param array $email_preview_data |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | public function email_preview_message( $email_message, $email_preview_data ) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Displays the email preview |
||
| 525 | * |
||
| 526 | * @since 1.9 |
||
| 527 | * @access public |
||
| 528 | * @return bool|null |
||
| 529 | */ |
||
| 530 | public function send_preview_email() { |
||
| 551 | } |
||
| 552 | |||
| 558 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.