From 9cc87df1fcb9e84bd2091fcadda31719a96a3a41 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Fri, 13 May 2022 10:20:21 +0200 Subject: [PATCH 01/11] Add gitlab reporter --- src/Container.php | 5 +++ src/Reporter/GitlabReporter.php | 63 +++++++++++++++++++++++++++ tests/Reporter/GitlabReporterTest.php | 48 ++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 src/Reporter/GitlabReporter.php create mode 100644 tests/Reporter/GitlabReporterTest.php diff --git a/src/Container.php b/src/Container.php index 1c69f50..0921241 100644 --- a/src/Container.php +++ b/src/Container.php @@ -7,6 +7,7 @@ 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\Validator\Validator; @@ -43,6 +44,10 @@ public function __construct() return new GithubActionReporter(new ConsoleReporter()); }; + $this['reporter.gitlab'] = function () { + 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..0af1f4a --- /dev/null +++ b/src/Reporter/GitlabReporter.php @@ -0,0 +1,63 @@ +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..591d0e2 --- /dev/null +++ b/tests/Reporter/GitlabReporterTest.php @@ -0,0 +1,48 @@ +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.'), + ]); + } +} From f18b28b62f5812fec745e60af0b502ff069bf927 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Fri, 13 May 2022 10:26:30 +0200 Subject: [PATCH 02/11] Add gitlab reporter --- tests/Reporter/GitlabReporterTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Reporter/GitlabReporterTest.php b/tests/Reporter/GitlabReporterTest.php index 591d0e2..a8526d8 100644 --- a/tests/Reporter/GitlabReporterTest.php +++ b/tests/Reporter/GitlabReporterTest.php @@ -23,6 +23,28 @@ class GitlabReporterTest extends TestCase "begin": 10 } } + }, + { + "description": "You should not do that.", + "fingerprint": "3068464e6cfb4c459076c2b5e910b8d99a80bbbadb1ff1e3e1551a71352ca3e2", + "severity": "minor", + "location": { + "path": "template.twig", + "lines": { + "begin": 10 + } + } + }, + { + "description": "You might not want to do that.", + "fingerprint": "b2b519c15c4819fe59ccff92544b35c1dd331a41d0ec138dc3f6b966d0362187", + "severity": "info", + "location": { + "path": "template.twig", + "lines": { + "begin": 10 + } + } } ] EOF; @@ -43,6 +65,8 @@ public function testReport(): void $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), ]); } } From 1e345da17cf1eca251fd33ff338cd9890c85fdd0 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Fri, 13 May 2022 10:38:17 +0200 Subject: [PATCH 03/11] Fix code style and dependency checks. --- composer.json | 1 + src/Container.php | 3 ++- src/Reporter/GitlabReporter.php | 14 ++++++++------ tests/Reporter/GitlabReporterTest.php | 1 + 4 files changed, 12 insertions(+), 7 deletions(-) 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 0921241..ac4a7df 100644 --- a/src/Container.php +++ b/src/Container.php @@ -10,6 +10,7 @@ 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 @@ -44,7 +45,7 @@ public function __construct() return new GithubActionReporter(new ConsoleReporter()); }; - $this['reporter.gitlab'] = function () { + $this['reporter.gitlab'] = function (): ReporterInterface { return new GitlabReporter(); }; diff --git a/src/Reporter/GitlabReporter.php b/src/Reporter/GitlabReporter.php index 0af1f4a..e06c255 100644 --- a/src/Reporter/GitlabReporter.php +++ b/src/Reporter/GitlabReporter.php @@ -1,4 +1,5 @@ getSeverity()) { + switch ($violation->getSeverity()) { case Violation::SEVERITY_INFO: $severity = 'info'; break; @@ -40,7 +42,7 @@ public function report(OutputInterface $output, array $violations): void 'fingerprint' => hash( 'sha256', implode( - [ + '', [ $violation->getFilename(), $violation->getLine(), $violation->getColumn(), @@ -48,9 +50,9 @@ public function report(OutputInterface $output, array $violations): void ] ) ), - 'severity' => $severity, - 'location' => [ - 'path' => $violation->getFilename(), + 'severity' => $severity, + 'location' => [ + 'path' => $violation->getFilename(), 'lines' => [ 'begin' => $violation->getLine(), ], @@ -58,6 +60,6 @@ public function report(OutputInterface $output, array $violations): void ]; } - $output->writeln(json_encode($errors, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)); + $output->writeln(json_encode($errors, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT)); } } diff --git a/tests/Reporter/GitlabReporterTest.php b/tests/Reporter/GitlabReporterTest.php index a8526d8..793fd8a 100644 --- a/tests/Reporter/GitlabReporterTest.php +++ b/tests/Reporter/GitlabReporterTest.php @@ -1,4 +1,5 @@ Date: Fri, 13 May 2022 11:01:15 +0200 Subject: [PATCH 04/11] Rename to GitLabReporter. Made classes final. --- src/Container.php | 4 ++-- .../{GitlabReporter.php => GitLabReporter.php} | 2 +- .../{GitlabReporterTest.php => GitLabReporterTest.php} | 10 +++------- 3 files changed, 6 insertions(+), 10 deletions(-) rename src/Reporter/{GitlabReporter.php => GitLabReporter.php} (96%) rename tests/Reporter/{GitlabReporterTest.php => GitLabReporterTest.php} (90%) diff --git a/src/Container.php b/src/Container.php index ac4a7df..bc7a54d 100644 --- a/src/Container.php +++ b/src/Container.php @@ -7,7 +7,7 @@ use FriendsOfTwig\Twigcs\Reporter\CsvReporter; use FriendsOfTwig\Twigcs\Reporter\EmacsReporter; use FriendsOfTwig\Twigcs\Reporter\GithubActionReporter; -use FriendsOfTwig\Twigcs\Reporter\GitlabReporter; +use FriendsOfTwig\Twigcs\Reporter\GitLabReporter; use FriendsOfTwig\Twigcs\Reporter\JsonReporter; use FriendsOfTwig\Twigcs\Reporter\JUnitReporter; use FriendsOfTwig\Twigcs\Reporter\ReporterInterface; @@ -46,7 +46,7 @@ public function __construct() }; $this['reporter.gitlab'] = function (): ReporterInterface { - return new GitlabReporter(); + return new GitLabReporter(); }; $this['lexer'] = function () { diff --git a/src/Reporter/GitlabReporter.php b/src/Reporter/GitLabReporter.php similarity index 96% rename from src/Reporter/GitlabReporter.php rename to src/Reporter/GitLabReporter.php index e06c255..3984f1f 100644 --- a/src/Reporter/GitlabReporter.php +++ b/src/Reporter/GitLabReporter.php @@ -11,7 +11,7 @@ use function implode; use function json_encode; -class GitlabReporter implements ReporterInterface +final class GitLabReporter implements ReporterInterface { /** * {@inheritdoc} diff --git a/tests/Reporter/GitlabReporterTest.php b/tests/Reporter/GitLabReporterTest.php similarity index 90% rename from tests/Reporter/GitlabReporterTest.php rename to tests/Reporter/GitLabReporterTest.php index 793fd8a..842d8b6 100644 --- a/tests/Reporter/GitlabReporterTest.php +++ b/tests/Reporter/GitLabReporterTest.php @@ -4,13 +4,12 @@ namespace FriendsOfTwig\Twigcs\Tests\Reporter; -use FriendsOfTwig\Twigcs\Reporter\GitlabReporter; +use FriendsOfTwig\Twigcs\Reporter\GitLabReporter; use FriendsOfTwig\Twigcs\Validator\Violation; -use JsonException; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Output\ConsoleOutput; -class GitlabReporterTest extends TestCase +final class GitLabReporterTest extends TestCase { public const EXPECTED_REPORT = <<createMock(ConsoleOutput::class); $output From a482a5009e73f1a7abc67162277787bf23cd7fed Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Fri, 13 May 2022 11:33:15 +0200 Subject: [PATCH 05/11] Add gitlab reporter to readme --- README.md | 1 + 1 file changed, 1 insertion(+) 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 From 36a7f951488e431be8b65b55a212b8e9ce5e60b1 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Fri, 13 May 2022 11:39:40 +0200 Subject: [PATCH 06/11] Add test with json exception --- tests/Reporter/GitLabReporterTest.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/Reporter/GitLabReporterTest.php b/tests/Reporter/GitLabReporterTest.php index 842d8b6..11f315d 100644 --- a/tests/Reporter/GitLabReporterTest.php +++ b/tests/Reporter/GitLabReporterTest.php @@ -6,6 +6,7 @@ use FriendsOfTwig\Twigcs\Reporter\GitLabReporter; use FriendsOfTwig\Twigcs\Validator\Violation; +use JsonException; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Output\ConsoleOutput; @@ -52,13 +53,12 @@ final class GitLabReporterTest extends TestCase public function testReport(): void { $reporter = new GitLabReporter(); - $output = $this->createMock(ConsoleOutput::class); + $output = $this->createMock(ConsoleOutput::class); $output ->expects($this->once()) ->method('writeln') - ->with(self::EXPECTED_REPORT) - ; + ->with(self::EXPECTED_REPORT); $reporter->report($output, [ new Violation('template.twig', 10, 20, 'You are not allowed to do that.'), @@ -66,4 +66,13 @@ public function testReport(): void 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")]); + } } From df60655ce0d4c312356d730847210b12bedb3515 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Fri, 13 May 2022 11:42:02 +0200 Subject: [PATCH 07/11] Code style fix --- tests/Reporter/GitLabReporterTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Reporter/GitLabReporterTest.php b/tests/Reporter/GitLabReporterTest.php index 11f315d..4ab748c 100644 --- a/tests/Reporter/GitLabReporterTest.php +++ b/tests/Reporter/GitLabReporterTest.php @@ -53,7 +53,7 @@ final class GitLabReporterTest extends TestCase public function testReport(): void { $reporter = new GitLabReporter(); - $output = $this->createMock(ConsoleOutput::class); + $output = $this->createMock(ConsoleOutput::class); $output ->expects($this->once()) @@ -70,7 +70,7 @@ public function testReport(): void public function testReportWithJsonException(): void { $reporter = new GitLabReporter(); - $output = $this->createMock(ConsoleOutput::class); + $output = $this->createMock(ConsoleOutput::class); $this->expectException(JsonException::class); $reporter->report($output, [new Violation('template.twig', 10, 20, "Error message with latin1 character \xE7")]); From 8be2f8c3afd8a13ba04c32be4a5f73ee9edf3755 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Fri, 13 May 2022 11:58:57 +0200 Subject: [PATCH 08/11] Removed unnecessary typehint from docblock --- src/Reporter/GitLabReporter.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Reporter/GitLabReporter.php b/src/Reporter/GitLabReporter.php index 3984f1f..fd3f0b9 100644 --- a/src/Reporter/GitLabReporter.php +++ b/src/Reporter/GitLabReporter.php @@ -16,8 +16,6 @@ final class GitLabReporter implements ReporterInterface /** * {@inheritdoc} * - * @param Violation[] $violations - * * @throws JsonException */ public function report(OutputInterface $output, array $violations): void From 8c49a2795111b080ad4c821cc005e7ac3d4b4d26 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Fri, 13 May 2022 17:03:14 +0200 Subject: [PATCH 09/11] Added reference to GitLab documentation about the format of the report. --- src/Reporter/GitLabReporter.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Reporter/GitLabReporter.php b/src/Reporter/GitLabReporter.php index fd3f0b9..41dc1dd 100644 --- a/src/Reporter/GitLabReporter.php +++ b/src/Reporter/GitLabReporter.php @@ -11,6 +11,10 @@ use function implode; use function json_encode; +/** + * Code Quality report format supported by GitLab + * @see https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html#implementing-a-custom-tool + */ final class GitLabReporter implements ReporterInterface { /** From dd94dc7b3b44d2183fe14bdfa4b22f6cfef29b7b Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Fri, 13 May 2022 17:04:06 +0200 Subject: [PATCH 10/11] Added reference to GitLab documentation about the format of the report. --- src/Reporter/GitLabReporter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Reporter/GitLabReporter.php b/src/Reporter/GitLabReporter.php index 41dc1dd..f101e92 100644 --- a/src/Reporter/GitLabReporter.php +++ b/src/Reporter/GitLabReporter.php @@ -13,6 +13,7 @@ /** * Code Quality report format supported by GitLab + * * @see https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html#implementing-a-custom-tool */ final class GitLabReporter implements ReporterInterface From e330bcea5fcc23f34f14509272ab1d2979f90880 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Fri, 13 May 2022 17:04:51 +0200 Subject: [PATCH 11/11] Added reference to GitLab documentation about the format of the report. --- src/Reporter/GitLabReporter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Reporter/GitLabReporter.php b/src/Reporter/GitLabReporter.php index f101e92..60a5f2e 100644 --- a/src/Reporter/GitLabReporter.php +++ b/src/Reporter/GitLabReporter.php @@ -12,7 +12,7 @@ use function json_encode; /** - * Code Quality report format supported by GitLab + * Code Quality report format supported by GitLab. * * @see https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html#implementing-a-custom-tool */