diff --git a/src/bin/phpmd b/src/bin/phpmd index 0ce7dde37..9fc90c755 100755 --- a/src/bin/phpmd +++ b/src/bin/phpmd @@ -16,8 +16,8 @@ * @link http://phpmd.org/ */ -use Composer\XdebugHandler\XdebugHandler; use PHPMD\TextUI\Command; +use PHPMD\TextUI\XdebugOptionHandler; if (file_exists(__DIR__ . '/../../../../autoload.php')) { // phpmd is part of a composer installation @@ -26,10 +26,18 @@ if (file_exists(__DIR__ . '/../../../../autoload.php')) { require_once __DIR__ . '/../../vendor/autoload.php'; } +// Check PHP setup for CLI arguments +if (!isset($_SERVER['argv']) && !isset($argv)) { + fwrite(STDERR, 'Please enable the "register_argc_argv" directive in your php.ini', PHP_EOL); + exit(1); +} elseif (!isset($argv)) { + $argv = $_SERVER['argv']; +} + // Restart if xdebug is loading, unless the environment variable PHPMD_ALLOW_XDEBUG is set. -$xdebug = new XdebugHandler('PHPMD'); -$xdebug->check(); -unset($xdebug); +$xdebugHandler = new XdebugOptionHandler('PHPMD'); +$xdebugHandler->check(); +unset($xdebugHandler); if (!ini_get('date.timezone')) { date_default_timezone_set('UTC'); @@ -37,13 +45,5 @@ if (!ini_get('date.timezone')) { ini_set('memory_limit', -1); -// Check php setup for cli arguments -if (!isset($_SERVER['argv']) && !isset($argv)) { - fwrite(STDERR, 'Please enable the "register_argc_argv" directive in your php.ini', PHP_EOL); - exit(1); -} elseif (!isset($argv)) { - $argv = $_SERVER['argv']; -} - // Run command line interface exit(Command::main($argv)->value); diff --git a/src/main/php/PHPMD/TextUI/CommandLineOptions.php b/src/main/php/PHPMD/TextUI/CommandLineOptions.php index 85b1956a2..9102e4169 100644 --- a/src/main/php/PHPMD/TextUI/CommandLineOptions.php +++ b/src/main/php/PHPMD/TextUI/CommandLineOptions.php @@ -827,6 +827,7 @@ public function usage(): string '--color: enable color in output' . \PHP_EOL . '--extra-line-in-excerpt: Specify how many extra lines are added ' . 'to a code snippet in html format' . \PHP_EOL . + '--xdebug: Enable Xdebug for debugging' . \PHP_EOL . '--: Explicit argument separator: Anything after "--" will be read as an argument even if' . 'it starts with "-" or matches the name of an option' . \PHP_EOL; } diff --git a/src/main/php/PHPMD/TextUI/XdebugOptionHandler.php b/src/main/php/PHPMD/TextUI/XdebugOptionHandler.php new file mode 100644 index 000000000..7b7172dd3 --- /dev/null +++ b/src/main/php/PHPMD/TextUI/XdebugOptionHandler.php @@ -0,0 +1,52 @@ + $command + */ + protected function restart(array $command): void + { + if (in_array('--xdebug', $command, true)) { + // Unset unwanted command arguments & options + if (($xdebugKey = array_search('--xdebug', $command, true)) !== false) { + unset($command[$xdebugKey]); + } + if (($noConfigKey = array_search('-n', $command, true)) !== false) { + unset($command[$noConfigKey]); + } + if (($configKey = array_search('-c', $command, true)) !== false) { + unset( + $command[$configKey + 1], + $command[$configKey] + ); + } + + // The PHP INI entries to enable Xdebug + $activateXdebugOptions[] = '-d xdebug.mode=debug'; + $activateXdebugOptions[] = '-d xdebug.start_with_request=on'; + + // Inject the activating command options just after the PHP binary + array_splice($command, 1, 0, $activateXdebugOptions); + + fwrite(STDERR, 'Restarting PHP Mess Detector with Xdebug enabled:' . PHP_EOL); + fwrite(STDERR, implode(' ', $command) . PHP_EOL); + fwrite(STDERR, PHP_EOL); + } + + if ($command) { + parent::restart($command); + } + } +} diff --git a/src/site/rst/documentation/index.rst b/src/site/rst/documentation/index.rst index 7ba22330c..e721c4715 100644 --- a/src/site/rst/documentation/index.rst +++ b/src/site/rst/documentation/index.rst @@ -96,6 +96,8 @@ Command line options - ``--color`` - enable color in output, for instance text renderer will show rule name in yellow and error description in red. + - ``--xdebug`` - will enable Xdebug for debugging PHP Mess Detector. + An example command line: :: phpmd PHP/Depend/DbusUI xml codesize --reportfile phpmd.xml --suffixes php,phtml