From f675d0f3271b5c9c507de6212acb7018ee9d4b26 Mon Sep 17 00:00:00 2001 From: Jordan Barnes Date: Wed, 22 Nov 2023 12:15:43 -0500 Subject: [PATCH] Added checkstyle reporting. --- src/Report/Reporter/CheckstyleReporter.php | 40 ++++++++++++++++++++++ src/Report/ReporterFactory.php | 2 ++ 2 files changed, 42 insertions(+) create mode 100644 src/Report/Reporter/CheckstyleReporter.php diff --git a/src/Report/Reporter/CheckstyleReporter.php b/src/Report/Reporter/CheckstyleReporter.php new file mode 100644 index 00000000..18070561 --- /dev/null +++ b/src/Report/Reporter/CheckstyleReporter.php @@ -0,0 +1,40 @@ +'); + + foreach ($report->getFiles() as $file) { + $fileMessages = $report->getMessages($file, $level); + if (\count($fileMessages) > 0) { + $filenode = $checkstyle->addChild('file'); + $filenode->addAttribute('name', $file); + + foreach ($fileMessages as $message) { + $violation = $filenode->addChild('violation'); + $violation->addAttribute('column', (string) $message->getLinePosition()); + $violation->addAttribute('line', (string) $message->getLine()); + $violation->addAttribute('severity', strtolower(SniffViolation::getLevelAsString($message->getLevel()))); + $violation->addAttribute('message', $message->getMessage()); + } + } + } + + $output->writeln($checkstyle->asXML()); + } +} diff --git a/src/Report/ReporterFactory.php b/src/Report/ReporterFactory.php index aa458723..cb7fef60 100644 --- a/src/Report/ReporterFactory.php +++ b/src/Report/ReporterFactory.php @@ -8,6 +8,7 @@ use TwigCsFixer\Report\Reporter\NullReporter; use TwigCsFixer\Report\Reporter\ReporterInterface; use TwigCsFixer\Report\Reporter\TextReporter; +use TwigCsFixer\Report\Reporter\CheckstyleReporter; final class ReporterFactory { @@ -16,6 +17,7 @@ public function getReporter(string $format = TextReporter::NAME): ReporterInterf return match ($format) { NullReporter::NAME => new NullReporter(), TextReporter::NAME => new TextReporter(), + CheckstyleReporter::NAME => new CheckstyleReporter(), default => throw new InvalidArgumentException( sprintf('No reporter supports the format "%s".', $format) ),