Skip to content

Commit fd0694e

Browse files
kapilpandya22claude
andcommitted
Support PHPMD 3 in the mess detector runner
PHPMD 3 changed how the command is called: it is now a Symfony console command taking an InputInterface, instead of the old array based CommandLineOptions. The runner still used the old call, so the job crashed with a TypeError before analysing anything. The images do not all carry the same PHPMD, so the runner now checks what is installed and calls it accordingly: - PHP 8.1 image builds Magento 2.4.7, which pins phpmd/phpmd ^2.12 -> PHPMD 2 - PHP 8.2-8.4 images build Magento 2.4.8-p5 -> PHPMD 3 - PHP 8.5 image builds Magento 2.4.9 -> PHPMD 3 Two related breakages are fixed at the same time: - PHPMD 2.15 made Command::__construct() require a \PHPMD\Console\Output. The runner passed no argument, so the PHP 8.1 image already failed with an ArgumentCountError, independently of PHPMD 3. The command is now built based on what the installed version expects. - PHPMD 3 renamed Command::EXIT_SUCCESS to Command::SUCCESS, which would have broken the assertion right after the first fix. Verified against the real images, on a clean module and on a file with four deliberate violations, to confirm the job both passes and still reports findings in the github annotation format: image Magento PHPMD clean 4 planted violations 8.1 2.4.7-p10 2.15.0 passes all 4 reported 8.4 2.4.8-p5 3.0.0 passes all 4 reported 8.5 2.4.9 3.0.0 passes all 4 reported Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 6e27587 commit fd0694e

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)