@@ -508,7 +508,12 @@ private static function get_process_env_variables(): array {
508508 $ env ['WP_CLI_REQUIRE ' ] = $ updated ;
509509 }
510510
511- $ env = array_merge ( $ env , self ::get_php_tuning_env_variables () );
511+ $ env = array_filter (
512+ array_merge ( $ env , self ::get_php_tuning_env_variables () ),
513+ static function ( $ value ) {
514+ return null !== $ value ;
515+ }
516+ );
512517
513518 $ config_path = getenv ( 'WP_CLI_CONFIG_PATH ' );
514519 if ( false !== $ config_path ) {
@@ -569,7 +574,7 @@ private static function get_process_env_variables(): array {
569574 *
570575 * Set WP_CLI_TEST_PHP_TUNING=off to launch the processes with the PHP configuration as it is.
571576 *
572- * @return array<string, string>
577+ * @return array<string, string|null> Null for variables to leave out.
573578 */
574579 private static function get_php_tuning_env_variables (): array {
575580 if ( 'off ' === getenv ( 'WP_CLI_TEST_PHP_TUNING ' ) || self ::running_with_code_coverage () ) {
@@ -587,9 +592,13 @@ private static function get_php_tuning_env_variables(): array {
587592 return $ env ;
588593 }
589594
595+ // The directories are named after the settings, so that a cache written with other settings,
596+ // such as by an older version of this package, is never used, nor its configuration file.
597+ $ settings = [ 'opcache.enable_cli=1 ' , 'opcache.file_cache_only=1 ' , 'opcache.optimization_level=0 ' ];
598+ $ key = substr ( md5 ( implode ( "\n" , $ settings ) ), 0 , 8 );
590599 $ temp_dir = rtrim ( sys_get_temp_dir (), '/ ' );
591- $ config_dir = "{$ temp_dir }/wp-cli-test-php-config " ;
592- $ cache_dir = "{$ temp_dir }/wp-cli-test-opcache " ;
600+ $ config_dir = "{$ temp_dir }/wp-cli-test-php-config- { $ key } " ;
601+ $ cache_dir = "{$ temp_dir }/wp-cli-test-opcache- { $ key } " ;
593602
594603 foreach ( [ $ config_dir , $ cache_dir ] as $ dir ) {
595604 if ( ! is_dir ( $ dir ) && ! @mkdir ( $ dir , 0777 , true ) && ! is_dir ( $ dir ) ) {
@@ -600,16 +609,32 @@ private static function get_php_tuning_env_variables(): array {
600609 $ excluded = array_unique ( array_filter ( [ $ temp_dir , realpath ( $ temp_dir ) ] ) );
601610 file_put_contents ( "{$ config_dir }/opcache-blacklist.txt " , implode ( "/ \n" , $ excluded ) . "/ \n" );
602611
603- $ ini = [
604- 'opcache.enable_cli=1 ' ,
605- "opcache.file_cache= {$ cache_dir }" ,
606- 'opcache.file_cache_only=1 ' ,
607- "opcache.blacklist_filename= {$ config_dir }/opcache-blacklist.txt " ,
608- ];
612+ // The optimizer bakes in what it finds at compile time, such as whether a function exists,
613+ // which differs between processes with other settings, such as disabled functions.
614+ $ ini = array_merge (
615+ $ settings ,
616+ [
617+ "opcache.file_cache= {$ cache_dir }" ,
618+ "opcache.blacklist_filename= {$ config_dir }/opcache-blacklist.txt " ,
619+ ]
620+ );
609621 file_put_contents ( "{$ config_dir }/wp-cli-tests.ini " , implode ( "\n" , $ ini ) . "\n" );
610622
611623 // Add to the directories PHP scans for configuration files rather than replacing them.
612624 $ scan_dirs = getenv ( 'PHP_INI_SCAN_DIR ' );
625+
626+ // Behat and Composer restart without Xdebug through composer/xdebug-handler, which clears
627+ // the directories, and keeps the original ones in its settings, with "*" if there were none.
628+ // The configuration file it points PHPRC to holds the scanned files already, so restore that too.
629+ $ restart_settings = getenv ( 'XDEBUG_HANDLER_SETTINGS ' );
630+ if ( '' === $ scan_dirs && is_string ( $ restart_settings ) ) {
631+ $ restart = explode ( '| ' , $ restart_settings );
632+ if ( isset ( $ restart [2 ], $ restart [3 ] ) ) {
633+ $ scan_dirs = '* ' === $ restart [2 ] ? false : $ restart [2 ];
634+ $ env ['PHPRC ' ] = '* ' === $ restart [3 ] ? null : $ restart [3 ];
635+ }
636+ }
637+
613638 if ( false === $ scan_dirs ) {
614639 $ scan_dirs = PHP_CONFIG_FILE_SCAN_DIR ;
615640 }
0 commit comments