@@ -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 )) {
0 commit comments