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:
- The lack of nested if statements in the "Better" version compared with the "Changed" version.
- 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.