@@ -30,7 +30,7 @@ class XdebugHandler
30
30
/** @var string|null */
31
31
protected $ tmpIni ;
32
32
33
- /** @var bool|null */
33
+ /** @var bool */
34
34
private static $ inRestart ;
35
35
36
36
/** @var string */
@@ -81,7 +81,7 @@ class XdebugHandler
81
81
*/
82
82
public function __construct ($ envPrefix )
83
83
{
84
- if (!is_string ($ envPrefix ) || empty ( $ envPrefix) ) {
84
+ if (!is_string ($ envPrefix ) || $ envPrefix === '' ) {
85
85
throw new \RuntimeException ('Invalid constructor parameter ' );
86
86
}
87
87
@@ -90,20 +90,13 @@ public function __construct($envPrefix)
90
90
$ this ->envOriginalInis = self ::$ name .self ::SUFFIX_INIS ;
91
91
92
92
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 );
104
96
}
105
97
106
- self ::$ xdebugActive = $ this ->loaded && $ this ->mode !== 'off ' ;
98
+ self ::$ xdebugActive = $ this ->loaded !== null && $ this ->mode !== 'off ' ;
99
+ self ::$ inRestart = false ;
107
100
108
101
if ($ this ->cli = PHP_SAPI === 'cli ' ) {
109
102
$ this ->debug = (string ) getenv (self ::DEBUG );
@@ -163,7 +156,7 @@ public function check()
163
156
$ this ->notify (Status::CHECK , $ this ->loaded .'| ' .$ this ->mode );
164
157
$ envArgs = explode ('| ' , (string ) getenv ($ this ->envAllowXdebug ));
165
158
166
- if (empty ( $ envArgs [0 ]) && $ this ->requiresRestart (self ::$ xdebugActive )) {
159
+ if (!(( bool ) $ envArgs [0 ]) && $ this ->requiresRestart (self ::$ xdebugActive )) {
167
160
// Restart required
168
161
$ this ->notify (Status::RESTART );
169
162
@@ -181,7 +174,7 @@ public function check()
181
174
Process::setEnv ($ this ->envAllowXdebug );
182
175
self ::$ inRestart = true ;
183
176
184
- if (! $ this ->loaded ) {
177
+ if ($ this ->loaded === null ) {
185
178
// Skipped version is only set if Xdebug is not loaded
186
179
self ::$ skipped = $ envArgs [1 ];
187
180
}
@@ -194,8 +187,9 @@ public function check()
194
187
}
195
188
196
189
$ this ->notify (Status::NORESTART );
190
+ $ settings = self ::getRestartSettings ();
197
191
198
- if ($ settings = self :: getRestartSettings () ) {
192
+ if ($ settings !== null ) {
199
193
// Called with existing settings, so sync our settings
200
194
$ this ->syncSettings ($ settings );
201
195
}
@@ -211,7 +205,7 @@ public function check()
211
205
*/
212
206
public static function getAllIniFiles ()
213
207
{
214
- if (! empty ( self ::$ name) ) {
208
+ if (self ::$ name !== null ) {
215
209
$ env = getenv (self ::$ name .self ::SUFFIX_INIS );
216
210
217
211
if (false !== $ env ) {
@@ -220,8 +214,9 @@ public static function getAllIniFiles()
220
214
}
221
215
222
216
$ paths = array ((string ) php_ini_loaded_file ());
217
+ $ scanned = php_ini_scanned_files ();
223
218
224
- if ($ scanned = php_ini_scanned_files () ) {
219
+ if ($ scanned !== false ) {
225
220
$ paths = array_merge ($ paths , array_map ('trim ' , explode (', ' , $ scanned )));
226
221
}
227
222
@@ -365,7 +360,7 @@ private function doRestart(array $command)
365
360
*/
366
361
private function prepareRestart ()
367
362
{
368
- $ error = '' ;
363
+ $ error = null ;
369
364
$ iniFiles = self ::getAllIniFiles ();
370
365
$ scannedInis = count ($ iniFiles ) > 1 ;
371
366
$ tmpDir = sys_get_temp_dir ();
@@ -381,35 +376,37 @@ private function prepareRestart()
381
376
} elseif (!$ this ->checkMainScript ()) {
382
377
$ error = 'Unable to access main script: ' .$ this ->script ;
383
378
} 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 ;
385
380
} elseif (!$ this ->setEnvironment ($ scannedInis , $ iniFiles )) {
386
381
$ error = 'Unable to set environment variables ' ;
387
382
}
388
383
389
- if ($ error ) {
384
+ if ($ error !== null ) {
390
385
$ this ->notify (Status::ERROR , $ error );
391
386
}
392
387
393
- return empty ( $ error) ;
388
+ return $ error === null ;
394
389
}
395
390
396
391
/**
397
392
* Returns true if the tmp ini file was written
398
393
*
399
394
* @param string[] $iniFiles All ini files used in the current process
400
395
* @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
402
397
*
403
398
* @return bool
404
399
*/
405
400
private function writeTmpIni (array $ iniFiles , $ tmpDir , &$ error )
406
401
{
407
- if (! $ this -> tmpIni = ( string ) @tempnam ($ tmpDir , '' )) {
402
+ if (( $ tmpfile = @tempnam ($ tmpDir , '' )) === false ) {
408
403
return false ;
409
404
}
410
405
406
+ $ this ->tmpIni = $ tmpfile ;
407
+
411
408
// $iniFiles has at least one item and it may be empty
412
- if (empty ( $ iniFiles [0 ]) ) {
409
+ if ($ iniFiles [0 ] === '' ) {
413
410
array_shift ($ iniFiles );
414
411
}
415
412
@@ -564,8 +561,9 @@ private function checkMainScript()
564
561
565
562
// Use a backtrace to resolve Phar and chdir issues.
566
563
$ trace = debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS );
564
+ $ main = end ($ trace );
567
565
568
- if (( $ main = end ( $ trace )) && isset ($ main ['file ' ])) {
566
+ if ($ main !== false && isset ($ main ['file ' ])) {
569
567
return file_exists ($ this ->script = $ main ['file ' ]);
570
568
}
571
569
@@ -622,10 +620,12 @@ private function syncSettings(array $settings)
622
620
*/
623
621
private function checkScanDirConfig ()
624
622
{
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 !== '' ;
629
629
}
630
630
631
631
/**
@@ -641,7 +641,7 @@ private function checkConfiguration(&$info)
641
641
return false ;
642
642
}
643
643
644
- if (extension_loaded ('uopz ' ) && !ini_get ('uopz.disable ' )) {
644
+ if (extension_loaded ('uopz ' ) && !(( bool ) ini_get ('uopz.disable ' ) )) {
645
645
// uopz works at opcode level and disables exit calls
646
646
if (function_exists ('uopz_allow_exit ' )) {
647
647
@uopz_allow_exit (true );
@@ -653,7 +653,9 @@ private function checkConfiguration(&$info)
653
653
654
654
// Check UNC paths when using cmd.exe
655
655
if (defined ('PHP_WINDOWS_VERSION_BUILD ' ) && PHP_VERSION_ID < 70400 ) {
656
- if (!$ workingDir = getcwd ()) {
656
+ $ workingDir = getcwd ();
657
+
658
+ if ($ workingDir === false ) {
657
659
$ info = 'unable to determine working directory ' ;
658
660
return false ;
659
661
}
@@ -696,4 +698,40 @@ private function tryEnableSignals()
696
698
sapi_windows_set_ctrl_handler (function ($ evt ) {});
697
699
}
698
700
}
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
+ }
699
737
}
0 commit comments