diff --git a/README.md b/README.md index b8a776b..97bba25 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ twigcs /path/to/views --reporter emacs twigcs /path/to/views --reporter json twigcs /path/to/views --reporter csv twigcs /path/to/views --reporter githubAction +twigcs /path/to/views --reporter gitlab ``` ## Using older twig versions diff --git a/composer.json b/composer.json index c982c54..320bd7e 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "require": { "php": "^7.4 || ^8.0", "ext-ctype": "*", + "ext-hash": "*", "ext-json": "*", "ext-mbstring": "*", "ext-simplexml": "*", diff --git a/src/Container.php b/src/Container.php index 1c69f50..bc7a54d 100644 --- a/src/Container.php +++ b/src/Container.php @@ -7,8 +7,10 @@ use FriendsOfTwig\Twigcs\Reporter\CsvReporter; use FriendsOfTwig\Twigcs\Reporter\EmacsReporter; use FriendsOfTwig\Twigcs\Reporter\GithubActionReporter; +use FriendsOfTwig\Twigcs\Reporter\GitLabReporter; use FriendsOfTwig\Twigcs\Reporter\JsonReporter; use FriendsOfTwig\Twigcs\Reporter\JUnitReporter; +use FriendsOfTwig\Twigcs\Reporter\ReporterInterface; use FriendsOfTwig\Twigcs\Validator\Validator; class Container extends \ArrayObject @@ -43,6 +45,10 @@ public function __construct() return new GithubActionReporter(new ConsoleReporter()); }; + $this['reporter.gitlab'] = function (): ReporterInterface { + return new GitLabReporter(); + }; + $this['lexer'] = function () { return new Lexer(); }; diff --git a/src/Reporter/GitLabReporter.php b/src/Reporter/GitLabReporter.php new file mode 100644 index 0000000..60a5f2e --- /dev/null +++ b/src/Reporter/GitLabReporter.php @@ -0,0 +1,68 @@ +getSeverity()) { + case Violation::SEVERITY_INFO: + $severity = 'info'; + break; + case Violation::SEVERITY_WARNING: + $severity = 'minor'; + break; + default: + $severity = 'major'; + break; + } + + $errors[] = [ + 'description' => $violation->getReason(), + 'fingerprint' => hash( + 'sha256', + implode( + '', [ + $violation->getFilename(), + $violation->getLine(), + $violation->getColumn(), + $violation->getReason(), + ] + ) + ), + 'severity' => $severity, + 'location' => [ + 'path' => $violation->getFilename(), + 'lines' => [ + 'begin' => $violation->getLine(), + ], + ], + ]; + } + + $output->writeln(json_encode($errors, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT)); + } +} diff --git a/tests/Reporter/GitLabReporterTest.php b/tests/Reporter/GitLabReporterTest.php new file mode 100644 index 0000000..4ab748c --- /dev/null +++ b/tests/Reporter/GitLabReporterTest.php @@ -0,0 +1,78 @@ +createMock(ConsoleOutput::class); + + $output + ->expects($this->once()) + ->method('writeln') + ->with(self::EXPECTED_REPORT); + + $reporter->report($output, [ + new Violation('template.twig', 10, 20, 'You are not allowed to do that.'), + new Violation('template.twig', 10, 20, 'You should not do that.', Violation::SEVERITY_WARNING), + new Violation('template.twig', 10, 20, 'You might not want to do that.', Violation::SEVERITY_INFO), + ]); + } + + public function testReportWithJsonException(): void + { + $reporter = new GitLabReporter(); + $output = $this->createMock(ConsoleOutput::class); + + $this->expectException(JsonException::class); + $reporter->report($output, [new Violation('template.twig', 10, 20, "Error message with latin1 character \xE7")]); + } +}