@@ -30,7 +30,7 @@ class XdebugHandler
3030 /** @var string|null */
3131 protected $ tmpIni ;
3232
33- /** @var bool|null */
33+ /** @var bool */
3434 private static $ inRestart ;
3535
3636 /** @var string */
@@ -81,7 +81,7 @@ class XdebugHandler
8181 */
8282 public function __construct ($ envPrefix )
8383 {
84- if (!is_string ($ envPrefix ) || empty ( $ envPrefix) ) {
84+ if (!is_string ($ envPrefix ) || $ envPrefix === '' ) {
8585 throw new \RuntimeException ('Invalid constructor parameter ' );
8686 }
8787
@@ -90,20 +90,13 @@ public function __construct($envPrefix)
9090 $ this ->envOriginalInis = self ::$ name .self ::SUFFIX_INIS ;
9191
9292 if (extension_loaded ('xdebug ' )) {
93- $ this ->loaded = phpversion ('xdebug ' ) ?: 'unknown ' ;
94-
95- if (version_compare ($ this ->loaded , '3.1 ' , '>= ' )) {
96- $ modes = xdebug_info ('mode ' );
97- $ this ->mode = empty ($ modes ) ? 'off ' : implode (', ' , $ modes );
98- } elseif (false !== ($ mode = ini_get ('xdebug.mode ' ))) {
99- $ this ->mode = getenv ('XDEBUG_MODE ' ) ?: ($ mode ?: 'off ' );
100- if (Preg::isMatch ('/^,+$/ ' , str_replace (' ' , '' , $ this ->mode ))) {
101- $ this ->mode = 'off ' ;
102- }
103- }
93+ $ version = phpversion ('xdebug ' );
94+ $ this ->loaded = $ version !== false ? $ version : 'unknown ' ;
95+ $ this ->mode = $ this ->getXdebugMode ($ this ->loaded );
10496 }
10597
106- self ::$ xdebugActive = $ this ->loaded && $ this ->mode !== 'off ' ;
98+ self ::$ xdebugActive = $ this ->loaded !== null && $ this ->mode !== 'off ' ;
99+ self ::$ inRestart = false ;
107100
108101 if ($ this ->cli = PHP_SAPI === 'cli ' ) {
109102 $ this ->debug = (string ) getenv (self ::DEBUG );
@@ -163,7 +156,7 @@ public function check()
163156 $ this ->notify (Status::CHECK , $ this ->loaded .'| ' .$ this ->mode );
164157 $ envArgs = explode ('| ' , (string ) getenv ($ this ->envAllowXdebug ));
165158
166- if (empty ( $ envArgs [0 ]) && $ this ->requiresRestart (self ::$ xdebugActive )) {
159+ if (!(( bool ) $ envArgs [0 ]) && $ this ->requiresRestart (self ::$ xdebugActive )) {
167160 // Restart required
168161 $ this ->notify (Status::RESTART );
169162
@@ -181,7 +174,7 @@ public function check()
181174 Process::setEnv ($ this ->envAllowXdebug );
182175 self ::$ inRestart = true ;
183176
184- if (! $ this ->loaded ) {
177+ if ($ this ->loaded === null ) {
185178 // Skipped version is only set if Xdebug is not loaded
186179 self ::$ skipped = $ envArgs [1 ];
187180 }
@@ -194,8 +187,9 @@ public function check()
194187 }
195188
196189 $ this ->notify (Status::NORESTART );
190+ $ settings = self ::getRestartSettings ();
197191
198- if ($ settings = self :: getRestartSettings () ) {
192+ if ($ settings !== null ) {
199193 // Called with existing settings, so sync our settings
200194 $ this ->syncSettings ($ settings );
201195 }
@@ -211,7 +205,7 @@ public function check()
211205 */
212206 public static function getAllIniFiles ()
213207 {
214- if (! empty ( self ::$ name) ) {
208+ if (self ::$ name !== null ) {
215209 $ env = getenv (self ::$ name .self ::SUFFIX_INIS );
216210
217211 if (false !== $ env ) {
@@ -220,8 +214,9 @@ public static function getAllIniFiles()
220214 }
221215
222216 $ paths = array ((string ) php_ini_loaded_file ());
217+ $ scanned = php_ini_scanned_files ();
223218
224- if ($ scanned = php_ini_scanned_files () ) {
219+ if ($ scanned !== false ) {
225220 $ paths = array_merge ($ paths , array_map ('trim ' , explode (', ' , $ scanned )));
226221 }
227222
@@ -365,7 +360,7 @@ private function doRestart(array $command)
365360 */
366361 private function prepareRestart ()
367362 {
368- $ error = '' ;
363+ $ error = null ;
369364 $ iniFiles = self ::getAllIniFiles ();
370365 $ scannedInis = count ($ iniFiles ) > 1 ;
371366 $ tmpDir = sys_get_temp_dir ();
@@ -381,35 +376,37 @@ private function prepareRestart()
381376 } elseif (!$ this ->checkMainScript ()) {
382377 $ error = 'Unable to access main script: ' .$ this ->script ;
383378 } elseif (!$ this ->writeTmpIni ($ iniFiles , $ tmpDir , $ error )) {
384- $ error = $ error ? : 'Unable to create temp ini file at: ' .$ tmpDir ;
379+ $ error = $ error !== null ? $ error : 'Unable to create temp ini file at: ' .$ tmpDir ;
385380 } elseif (!$ this ->setEnvironment ($ scannedInis , $ iniFiles )) {
386381 $ error = 'Unable to set environment variables ' ;
387382 }
388383
389- if ($ error ) {
384+ if ($ error !== null ) {
390385 $ this ->notify (Status::ERROR , $ error );
391386 }
392387
393- return empty ( $ error) ;
388+ return $ error === null ;
394389 }
395390
396391 /**
397392 * Returns true if the tmp ini file was written
398393 *
399394 * @param string[] $iniFiles All ini files used in the current process
400395 * @param string $tmpDir The system temporary directory
401- * @param string $error Set by method if ini file cannot be read
396+ * @param null| string $error Set by method if ini file cannot be read
402397 *
403398 * @return bool
404399 */
405400 private function writeTmpIni (array $ iniFiles , $ tmpDir , &$ error )
406401 {
407- if (! $ this -> tmpIni = ( string ) @tempnam ($ tmpDir , '' )) {
402+ if (( $ tmpfile = @tempnam ($ tmpDir , '' )) === false ) {
408403 return false ;
409404 }
410405
406+ $ this ->tmpIni = $ tmpfile ;
407+
411408 // $iniFiles has at least one item and it may be empty
412- if (empty ( $ iniFiles [0 ]) ) {
409+ if ($ iniFiles [0 ] === '' ) {
413410 array_shift ($ iniFiles );
414411 }
415412
@@ -564,8 +561,9 @@ private function checkMainScript()
564561
565562 // Use a backtrace to resolve Phar and chdir issues.
566563 $ trace = debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS );
564+ $ main = end ($ trace );
567565
568- if (( $ main = end ( $ trace )) && isset ($ main ['file ' ])) {
566+ if ($ main !== false && isset ($ main ['file ' ])) {
569567 return file_exists ($ this ->script = $ main ['file ' ]);
570568 }
571569
@@ -622,10 +620,12 @@ private function syncSettings(array $settings)
622620 */
623621 private function checkScanDirConfig ()
624622 {
625- return !(getenv ('PHP_INI_SCAN_DIR ' )
626- && !PHP_CONFIG_FILE_SCAN_DIR
627- && (PHP_VERSION_ID < 70113
628- || PHP_VERSION_ID === 70200 ));
623+ if (PHP_VERSION_ID >= 70113 && PHP_VERSION_ID !== 70200 ) {
624+ return true ;
625+ }
626+
627+ return ((string ) getenv ('PHP_INI_SCAN_DIR ' ) === '' )
628+ || PHP_CONFIG_FILE_SCAN_DIR !== '' ;
629629 }
630630
631631 /**
@@ -641,7 +641,7 @@ private function checkConfiguration(&$info)
641641 return false ;
642642 }
643643
644- if (extension_loaded ('uopz ' ) && !ini_get ('uopz.disable ' )) {
644+ if (extension_loaded ('uopz ' ) && !(( bool ) ini_get ('uopz.disable ' ) )) {
645645 // uopz works at opcode level and disables exit calls
646646 if (function_exists ('uopz_allow_exit ' )) {
647647 @uopz_allow_exit (true );
@@ -653,7 +653,9 @@ private function checkConfiguration(&$info)
653653
654654 // Check UNC paths when using cmd.exe
655655 if (defined ('PHP_WINDOWS_VERSION_BUILD ' ) && PHP_VERSION_ID < 70400 ) {
656- if (!$ workingDir = getcwd ()) {
656+ $ workingDir = getcwd ();
657+
658+ if ($ workingDir === false ) {
657659 $ info = 'unable to determine working directory ' ;
658660 return false ;
659661 }
@@ -696,4 +698,40 @@ private function tryEnableSignals()
696698 sapi_windows_set_ctrl_handler (function ($ evt ) {});
697699 }
698700 }
701+
702+ /**
703+ * Returns the Xdebug mode if available
704+ *
705+ * @param string $version
706+ *
707+ * @return string|null
708+ */
709+ private function getXdebugMode ($ version )
710+ {
711+ if (version_compare ($ version , '3.1 ' , '>= ' )) {
712+ $ modes = xdebug_info ('mode ' );
713+ return count ($ modes ) === 0 ? 'off ' : implode (', ' , $ modes );
714+ }
715+
716+ // See if xdebug.mode is supported in this version
717+ $ iniMode = ini_get ('xdebug.mode ' );
718+ if ($ iniMode === false ) {
719+ return null ;
720+ }
721+
722+ // Environment value wins but cannot be empty
723+ $ envMode = (string ) getenv ('XDEBUG_MODE ' );
724+ if ($ envMode !== '' ) {
725+ $ mode = $ envMode ;
726+ } else {
727+ $ mode = $ iniMode !== '' ? $ iniMode : 'off ' ;
728+ }
729+
730+ // An empty comma-separated list is treated as mode 'off'
731+ if (Preg::isMatch ('/^,+$/ ' , str_replace (' ' , '' , $ mode ))) {
732+ $ mode = 'off ' ;
733+ }
734+
735+ return $ mode ;
736+ }
699737}
0 commit comments