Skip to content

Commit 0b09505

Browse files
authored
Merge pull request #177 from kapilpandya22/fix/phpmd-3-compatibility
Support PHPMD 3 in the mess detector runner
2 parents 6e27587 + fd0694e commit 0b09505

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

magento-mess-detector/LiveCodePhpmdRunner.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ public function canRun()
5656
*/
5757
public function run(array $whiteList)
5858
{
59+
$command = $this->createCommand();
60+
61+
// PHPMD 3 turned the command into a Symfony console command and dropped the array based
62+
// CommandLineOptions constructor. Images that build Magento 2.4.7 still install PHPMD 2
63+
// (it pins phpmd/phpmd ^2.12), so both APIs have to keep working.
64+
if ($command instanceof \Symfony\Component\Console\Command\Command) {
65+
$input = new \Symfony\Component\Console\Input\ArrayInput(
66+
[
67+
'paths' => explode(',', $this->getSourceCodePath($whiteList)),
68+
'--format' => 'github',
69+
'--ruleset' => [$this->rulesetFile],
70+
'--reportfile-github' => $this->reportFile,
71+
'--suffixes' => ['php'],
72+
'--exclude' => ['vendor/', 'tmp/', 'var/', 'generated/', '.git/', '.idea/'],
73+
],
74+
$command->getDefinition()
75+
);
76+
77+
return $command->run($input, new \Symfony\Component\Console\Output\NullOutput());
78+
}
79+
5980
$commandLineArguments = [
6081
'run_file_mock', //emulate script name in console arguments
6182
$this->getSourceCodePath($whiteList),
@@ -71,11 +92,25 @@ public function run(array $whiteList)
7192

7293
$options = new \PHPMD\TextUI\CommandLineOptions($commandLineArguments);
7394

74-
$command = new \PHPMD\TextUI\Command();
75-
7695
return $command->run($options, new \PHPMD\RuleSetFactory());
7796
}
7897

98+
/**
99+
* PHPMD 2.15 made the command require a \PHPMD\Console\Output, PHPMD 3 takes no arguments.
100+
*
101+
* @return \PHPMD\TextUI\Command
102+
*/
103+
private function createCommand()
104+
{
105+
$constructor = (new \ReflectionClass(\PHPMD\TextUI\Command::class))->getConstructor();
106+
107+
if ($constructor !== null && $constructor->getNumberOfRequiredParameters() > 0) {
108+
return new \PHPMD\TextUI\Command(new \PHPMD\Console\NullOutput());
109+
}
110+
111+
return new \PHPMD\TextUI\Command();
112+
}
113+
79114
private function getSourceCodePath($whiteList): string
80115
{
81116
if (!empty($whiteList)) {

magento-mess-detector/PhpmdRunner.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testCodeMess()
6060
}
6161

6262
$this->assertEquals(
63-
Command::EXIT_SUCCESS,
63+
$this->getSuccessExitCode(),
6464
$result,
6565
"PHP Code Mess has found error(s):" . PHP_EOL . $output
6666
);
@@ -71,5 +71,15 @@ public function testCodeMess()
7171
}
7272
}
7373

74+
/**
75+
* PHPMD 3 renamed Command::EXIT_SUCCESS to Command::SUCCESS.
76+
*
77+
* @return int
78+
*/
79+
private function getSuccessExitCode(): int
80+
{
81+
return defined(Command::class . '::EXIT_SUCCESS') ? Command::EXIT_SUCCESS : Command::SUCCESS;
82+
}
83+
7484
}
7585

0 commit comments

Comments
 (0)