Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Add "--xdebug" CLI option to enabled Xdebug #1158

Open
wants to merge 8 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/bin/phpmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,24 +26,24 @@ 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');
}

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);
1 change: 1 addition & 0 deletions src/main/php/PHPMD/TextUI/CommandLineOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ public function usage()
'--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;
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/php/PHPMD/TextUI/XdebugOptionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

Check warning on line 1 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View workflow job for this annotation

GitHub Actions / Codestyle check (8.1)

Found violation(s) of type: combine_consecutive_unsets

Check warning on line 1 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View workflow job for this annotation

GitHub Actions / Codestyle check (8.1)

Found violation(s) of type: statement_indentation

Check warning on line 1 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View workflow job for this annotation

GitHub Actions / Codestyle check (8.1)

Found violation(s) of type: no_extra_blank_lines

namespace PHPMD\TextUI;

use Composer\XdebugHandler\XdebugHandler;

/**
* Xdebug CLI Option Handler
*
* Enables instead of disables Xdebug, if called with "--xdebug" CLI option.
*/
class XdebugOptionHandler extends XdebugHandler
{
/**
* Rebuilds the run command with Xdebug enabled, instead, if CLI option "--xdebug" is used
kylekatarnls marked this conversation as resolved.
Show resolved Hide resolved
*/
protected function restart(array $command): void

Check warning on line 17 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/main/php/PHPMD/TextUI/XdebugOptionHandler.php#L17

Added line #L17 was not covered by tests
{
if (in_array('--xdebug', $command, true)) {

Check warning on line 19 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/main/php/PHPMD/TextUI/XdebugOptionHandler.php#L19

Added line #L19 was not covered by tests
// Unset unwanted command arguments & options
if (($xdebugKey = array_search('--xdebug', $command, true)) !== false) {
unset($command[$xdebugKey]);

Check warning on line 22 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/main/php/PHPMD/TextUI/XdebugOptionHandler.php#L21-L22

Added lines #L21 - L22 were not covered by tests
}
if (($noConfigKey = array_search('-n', $command, true)) !== false) {
unset($command[$noConfigKey]);

Check warning on line 25 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/main/php/PHPMD/TextUI/XdebugOptionHandler.php#L24-L25

Added lines #L24 - L25 were not covered by tests
}
if (($configKey = array_search('-c', $command, true)) !== false) {
unset($command[$configKey + 1]);
unset($command[$configKey]);

Check warning on line 29 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/main/php/PHPMD/TextUI/XdebugOptionHandler.php#L27-L29

Added lines #L27 - L29 were not covered by tests
ravage84 marked this conversation as resolved.
Show resolved Hide resolved
}

// The PHP INI entries to enable Xdebug
$activateXdebugOptions[] = '-d xdebug.mode=debug';
$activateXdebugOptions[] = '-d xdebug.start_with_request=on';

Check warning on line 34 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/main/php/PHPMD/TextUI/XdebugOptionHandler.php#L33-L34

Added lines #L33 - L34 were not covered by tests

// Inject the activating command options just after the PHP binary
array_splice($command, 1, 0, $activateXdebugOptions);

Check warning on line 37 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/main/php/PHPMD/TextUI/XdebugOptionHandler.php#L37

Added line #L37 was not covered by tests

fwrite(STDERR, 'Restarting PHP Mess Detector with Xdebug enabled:' . PHP_EOL);
fwrite(STDERR, implode(' ', $command) . PHP_EOL);
fwrite(STDERR, PHP_EOL);

Check warning on line 41 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/main/php/PHPMD/TextUI/XdebugOptionHandler.php#L39-L41

Added lines #L39 - L41 were not covered by tests
}

if ($command) {
parent::restart($command);

Check warning on line 45 in src/main/php/PHPMD/TextUI/XdebugOptionHandler.php

View check run for this annotation

Codecov / codecov/patch

src/main/php/PHPMD/TextUI/XdebugOptionHandler.php#L44-L45

Added lines #L44 - L45 were not covered by tests
ravage84 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
2 changes: 2 additions & 0 deletions src/site/rst/documentation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading