Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Last active May 17, 2020 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeschinkel/f685573dcf6c4cb30000a1157d722100 to your computer and use it in GitHub Desktop.
Save mikeschinkel/f685573dcf6c4cb30000a1157d722100 to your computer and use it in GitHub Desktop.
Refactoring Pantheon_Sessions::initialize_session_override() using do{...}while(false);

Example use of do {...} while(false)

See this gist for background.

This example comes from my work here when looking for a solution for this issue related to this WordPress plugin.

I recognized I could make it clearer using the do {...} while(false) pattern so I wanted to document here.

Note:

  1. The lack of nested if statements in the "Better" version compared with the "Changed" version.
  2. The lack of complex if condition in the "Better" version compared with the "AlternateChanged" version.

However I am not going to submit my "better" version as a patch because most people don't appreciate how this pattern can help so I will be choosing my battles on the PR.

Note that I think that function could be further refactored but my only goal was to stop the PHP error "PHP Warning: session_set_save_handler(): Cannot change save handler when headers already sent" from showing up in the PHP error log when running a WP-CLI command on Pantheon, and any futher refactoring could potential have unintended consequences such as this one.

class Pantheon_Sessions {
/**
* Override the default sessions implementation with our own
*
* Largely adopted from Drupal 7's implementation
*/
private function initialize_session_override() {
require_once dirname( __FILE__ ) . '/inc/class-session.php';
require_once dirname( __FILE__ ) . '/inc/class-session-handler.php';
$session_handler = new Pantheon_Sessions\Session_Handler;
if ( PHP_SESSION_ACTIVE !== session_status() ) {
session_set_save_handler( $session_handler, false );
}
// Close the session before $wpdb destructs itself.
add_action( 'shutdown', 'session_write_close', 999, 0 );
}
}
class Pantheon_Sessions {
/**
* Override the default sessions implementation with our own
*
* Largely adopted from Drupal 7's implementation
*/
private function initialize_session_override() {
require_once dirname( __FILE__ ) . '/inc/class-session.php';
require_once dirname( __FILE__ ) . '/inc/class-session-handler.php';
$session_handler = new Pantheon_Sessions\Session_Handler;
if ( PHP_SESSION_ACTIVE !== session_status() ) {
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
session_set_save_handler( $session_handler, false );
}
}
// Close the session before $wpdb destructs itself.
add_action( 'shutdown', 'session_write_close', 999, 0 );
}
}
/**
* Override the default sessions implementation with our own
*
* Largely adopted from Drupal 7's implementation
*/
private function initialize_session_override() {
require_once dirname( __FILE__ ) . '/inc/class-session.php';
require_once dirname( __FILE__ ) . '/inc/class-session-handler.php';
$session_handler = new Pantheon_Sessions\Session_Handler;
if ( PHP_SESSION_ACTIVE !== session_status() && ( ! defined( 'WP_CLI' ) || ! WP_CLI ) ) {
session_set_save_handler( $session_handler, false );
}
// Close the session before $wpdb destructs itself.
add_action( 'shutdown', 'session_write_close', 999, 0 );
}
class Pantheon_Sessions {
/**
* Override the default sessions implementation with our own
*
* Largely adopted from Drupal 7's implementation
*/
private function initialize_session_override() {
do {
require_once dirname( __FILE__ ) . '/inc/class-session.php';
require_once dirname( __FILE__ ) . '/inc/class-session-handler.php';
$session_handler = new Pantheon_Sessions\Session_Handler;
if ( PHP_SESSION_ACTIVE === session_status() ) {
break;
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
break;
}
session_set_save_handler( $session_handler, false );
} while (false);
// Close the session before $wpdb destructs itself.
add_action( 'shutdown', 'session_write_close', 999, 0 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment