From ce37df27e7395ad67613cc1bae634fa9d097b3cd Mon Sep 17 00:00:00 2001 From: Dzianis Kotau Date: Mon, 18 Jul 2022 01:15:05 +0400 Subject: [PATCH 1/6] Allow output format supported by PHP-CS-Fixer --- README.md | 38 ++++++++ app/Actions/ElaborateSummary.php | 31 ++++++- app/Actions/FixCode.php | 20 ++++- app/Commands/DefaultCommand.php | 4 +- .../ConfigurationResolverFactory.php | 1 + tests/Feature/FormatTest.php | 86 +++++++++++++++++++ tests/Feature/ReportTest.php | 25 ++++++ 7 files changed, 201 insertions(+), 4 deletions(-) create mode 100644 tests/Feature/FormatTest.php create mode 100644 tests/Feature/ReportTest.php diff --git a/README.md b/README.md index 0cb76011..e35bada0 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,44 @@ Documentation for Pint can be found on the [Laravel website](https://laravel.com/docs/pint). +### Format output + +You can output results in different [format](https://cs.symfony.com/doc/usage.html) supported by PHP-CS-Fixer. +This is especially useful in CI/CD pipelines (for example, [Annotations via the Checks API](https://docs.github.com/en/rest/checks)): + +```bash +pint --test --format=checkstyle +``` + +Then you can send output to the tool that will read it and create annotations for you. For example, for GitHub Actions +you can implement following job: + +```yaml +- name: Show Pint results in PR +run: pint --test --format=checkstyle | cs2pr +``` + +### Save report to the file +If you run `pint` with [--format](#format-output) option, it will suppress standard pint's output. If you want to see +both standard and formatted output, you can save formatted output to report file: + +```bash +pint --test --format=checkstyle --report=checkstyle.xml +``` + +This is especially useful in CI/CD pipelines (for example, [Annotations via the Checks API](https://docs.github.com/en/rest/checks)) +when you want to have both job logs and job annotations. You can have one job to generate report file and second job +to read from report file. For example, for GitHub Actions you can implement following jobs: + +```yaml +- name: Check PHP code style +continue-on-error: true +run: pint --test --format=checkstyle --report=./pint-report.xml + +- name: Show Pint results in PR +run: cs2pr ./pint-report.xml +``` + ## Contributing diff --git a/app/Actions/ElaborateSummary.php b/app/Actions/ElaborateSummary.php index 5313771f..edf803b3 100644 --- a/app/Actions/ElaborateSummary.php +++ b/app/Actions/ElaborateSummary.php @@ -31,9 +31,10 @@ public function __construct( * * @param int $totalFiles * @param array $changes + * @param \PhpCsFixer\Console\Report\FixReport\ReporterInterface $reporter * @return int */ - public function execute($totalFiles, $changes) + public function execute($totalFiles, $changes, $reporter) { $summary = new ReportSummary( $changes, @@ -44,7 +45,14 @@ public function execute($totalFiles, $changes) $this->output->isDecorated() ); - tap($summary, fn () => $this->summaryOutput->handle($summary, $totalFiles))->getChanged(); + $this->format($reporter, $summary); + + if ( + $this->input->getOption('format') === 'txt' + || $this->input->getOption('report') !== null + ) { + tap($summary, fn() => $this->summaryOutput->handle($summary, $totalFiles))->getChanged(); + } $failure = ($summary->isDryRun() && count($changes) > 0) || count($this->errors->getInvalidErrors()) > 0 @@ -53,4 +61,23 @@ public function execute($totalFiles, $changes) return $failure ? Command::FAILURE : Command::SUCCESS; } + + /** + * @param \PhpCsFixer\Console\Report\FixReport\ReporterInterface $reporter + * @param ReportSummary $summary + * @return void + */ + private function format($reporter, $summary) + { + if ($this->input->getOption('format') === 'txt') { + return; + } + + $report = $reporter->generate($summary); + if ($this->input->getOption('report') === null) { + $this->output->writeln($report); + } else { + file_put_contents($this->input->getOption('report'), stripcslashes($report), LOCK_EX); + } + } } diff --git a/app/Actions/FixCode.php b/app/Actions/FixCode.php index 6cf41289..1faf4070 100644 --- a/app/Actions/FixCode.php +++ b/app/Actions/FixCode.php @@ -7,6 +7,9 @@ class FixCode { + /** @var \PhpCsFixer\Console\Report\FixReport\ReporterInterface */ + protected $reporter; + /** * Creates a new Fix Code instance. * @@ -36,7 +39,12 @@ public function execute() { [$resolver, $totalFiles] = ConfigurationResolverFactory::fromIO($this->input, $this->output); - $this->progress->subscribe(); + if ( + $this->input->getOption('format') === 'txt' + || $this->input->getOption('report') !== null + ) { + $this->progress->subscribe(); + } /** @var array $changes */ $changes = with(new Runner( @@ -52,6 +60,16 @@ public function execute() $resolver->shouldStopOnViolation() ))->fix(); + $this->reporter = $resolver->getReporter(); + return tap([$totalFiles, $changes], fn () => $this->progress->unsubscribe()); } + + /** + * @return \PhpCsFixer\Console\Report\FixReport\ReporterInterface + */ + public function getReporter() + { + return $this->reporter; + } } diff --git a/app/Commands/DefaultCommand.php b/app/Commands/DefaultCommand.php index cac9593a..1d5ce874 100644 --- a/app/Commands/DefaultCommand.php +++ b/app/Commands/DefaultCommand.php @@ -38,6 +38,8 @@ protected function configure() new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The configuration that should be used'), new InputOption('preset', '', InputOption::VALUE_REQUIRED, 'The preset that should be used'), new InputOption('test', '', InputOption::VALUE_NONE, 'Test for code style errors without fixing them'), + new InputOption('format', '', InputOption::VALUE_REQUIRED, 'The output format', 'txt'), + new InputOption('report', '', InputOption::VALUE_REQUIRED, 'The file to output report to'), ] ); } @@ -53,6 +55,6 @@ public function handle($fixCode, $elaborateSummary) { [$totalFiles, $changes] = $fixCode->execute(); - return $elaborateSummary->execute($totalFiles, $changes); + return $elaborateSummary->execute($totalFiles, $changes, $fixCode->getReporter()); } } diff --git a/app/Factories/ConfigurationResolverFactory.php b/app/Factories/ConfigurationResolverFactory.php index cc26e50c..16ed2fed 100644 --- a/app/Factories/ConfigurationResolverFactory.php +++ b/app/Factories/ConfigurationResolverFactory.php @@ -65,6 +65,7 @@ public static function fromIO($input, $output) 'stop-on-violation' => false, 'verbosity' => $output->getVerbosity(), 'show-progress' => 'true', + 'format' => $input->getOption('format'), ], Project::path(), new ToolInfo(), diff --git a/tests/Feature/FormatTest.php b/tests/Feature/FormatTest.php new file mode 100644 index 00000000..5a73fd7e --- /dev/null +++ b/tests/Feature/FormatTest.php @@ -0,0 +1,86 @@ + base_path('tests/Fixtures/with-fixable-issues'), + '--preset' => 'psr12', + '--format' => 'checkstyle', + ]); + + expect($statusCode)->toBe(1) + ->and($output) + ->toContain('') + ->toContain('') + ->toContain('') + ->not + ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ + 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', + ]))); +}); + +it('outputs json format', function () { + [$statusCode, $output] = run('default', [ + 'path' => base_path('tests/Fixtures/with-fixable-issues'), + '--preset' => 'psr12', + '--format' => 'json', + ]); + + expect($statusCode)->toBe(1) + ->and($output) + ->toBeJson() + ->toContain('appliedFixers') + ->not + ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ + 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', + ]))); +}); + +it('outputs xml format', function () { + [$statusCode, $output] = run('default', [ + 'path' => base_path('tests/Fixtures/with-fixable-issues'), + '--preset' => 'psr12', + '--format' => 'xml', + ]); + + expect($statusCode)->toBe(1) + ->and($output) + ->toContain('') + ->not + ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ + 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', + ]))); +}); + +it('outputs junit format', function () { + [$statusCode, $output] = run('default', [ + 'path' => base_path('tests/Fixtures/with-fixable-issues'), + '--preset' => 'psr12', + '--format' => 'junit', + ]); + + expect($statusCode)->toBe(1) + ->and($output) + ->toContain('') + ->toContain('CDATA') + ->not + ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ + 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', + ]))); +}); + +it('outputs gitlab format', function () { + [$statusCode, $output] = run('default', [ + 'path' => base_path('tests/Fixtures/with-fixable-issues'), + '--preset' => 'psr12', + '--format' => 'gitlab', + ]); + + expect($statusCode)->toBe(1) + ->and($output) + ->toBeJson() + ->toContain('fingerprint') + ->not + ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ + 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', + ]))); +}); diff --git a/tests/Feature/ReportTest.php b/tests/Feature/ReportTest.php new file mode 100644 index 00000000..9235d4ad --- /dev/null +++ b/tests/Feature/ReportTest.php @@ -0,0 +1,25 @@ + base_path('tests/Fixtures/with-fixable-issues'), + '--preset' => 'psr12', + '--format' => 'checkstyle', + '--report' => $report, + ]); + + expect($statusCode)->toBe(1) + ->and($output) + ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ + 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', + ]))) + ->and(file_exists($report)) + ->toBeTrue() + ->and(file_get_contents($report)) + ->toContain('') + ->toContain('') + ->toContain('') + ->and(unlink($report)) + ->toBeTrue(); +}); From 26549f756c36ba2db12c97a41c4ada3bf506d0ec Mon Sep 17 00:00:00 2001 From: Dzianis Kotau Date: Wed, 20 Jul 2022 16:02:01 +0400 Subject: [PATCH 2/6] Format refactor --- app/Actions/ElaborateSummary.php | 29 +++++++++++++++++++++------- app/Commands/DefaultCommand.php | 2 +- app/Providers/AppServiceProvider.php | 22 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/app/Actions/ElaborateSummary.php b/app/Actions/ElaborateSummary.php index edf803b3..c2c89605 100644 --- a/app/Actions/ElaborateSummary.php +++ b/app/Actions/ElaborateSummary.php @@ -3,6 +3,7 @@ namespace App\Actions; use Illuminate\Console\Command; +use Illuminate\Support\Facades\App; use PhpCsFixer\Console\Report\FixReport\ReportSummary; use Symfony\Component\Console\Output\OutputInterface; @@ -31,10 +32,9 @@ public function __construct( * * @param int $totalFiles * @param array $changes - * @param \PhpCsFixer\Console\Report\FixReport\ReporterInterface $reporter * @return int */ - public function execute($totalFiles, $changes, $reporter) + public function execute($totalFiles, $changes) { $summary = new ReportSummary( $changes, @@ -45,13 +45,13 @@ public function execute($totalFiles, $changes, $reporter) $this->output->isDecorated() ); - $this->format($reporter, $summary); + $this->format($summary); if ( $this->input->getOption('format') === 'txt' || $this->input->getOption('report') !== null ) { - tap($summary, fn() => $this->summaryOutput->handle($summary, $totalFiles))->getChanged(); + tap($summary, fn () => $this->summaryOutput->handle($summary, $totalFiles))->getChanged(); } $failure = ($summary->isDryRun() && count($changes) > 0) @@ -63,21 +63,36 @@ public function execute($totalFiles, $changes, $reporter) } /** - * @param \PhpCsFixer\Console\Report\FixReport\ReporterInterface $reporter * @param ReportSummary $summary * @return void */ - private function format($reporter, $summary) + private function format($summary) { if ($this->input->getOption('format') === 'txt') { return; } - $report = $reporter->generate($summary); + $report = $this->report($summary); if ($this->input->getOption('report') === null) { $this->output->writeln($report); } else { file_put_contents($this->input->getOption('report'), stripcslashes($report), LOCK_EX); } } + + /** + * @param ReportSummary $summary + * @return string + */ + private function report($summary) + { + $reporters = App::tagged('reporters'); + $format = $this->input->getOption('format'); + + foreach ($reporters as $reporter) { + if ($format === $reporter->getFormat()) { + return $reporter->generate($summary); + } + } + } } diff --git a/app/Commands/DefaultCommand.php b/app/Commands/DefaultCommand.php index 1d5ce874..be3aab50 100644 --- a/app/Commands/DefaultCommand.php +++ b/app/Commands/DefaultCommand.php @@ -55,6 +55,6 @@ public function handle($fixCode, $elaborateSummary) { [$totalFiles, $changes] = $fixCode->execute(); - return $elaborateSummary->execute($totalFiles, $changes, $fixCode->getReporter()); + return $elaborateSummary->execute($totalFiles, $changes); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 4aec48cc..b2e658d0 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,6 +3,12 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; +use PhpCsFixer\Console\Report\FixReport\CheckstyleReporter; +use PhpCsFixer\Console\Report\FixReport\GitlabReporter; +use PhpCsFixer\Console\Report\FixReport\JsonReporter; +use PhpCsFixer\Console\Report\FixReport\JunitReporter; +use PhpCsFixer\Console\Report\FixReport\TextReporter; +use PhpCsFixer\Console\Report\FixReport\XmlReporter; use PhpCsFixer\Error\ErrorsManager; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -32,5 +38,21 @@ public function register() $this->app->singleton(EventDispatcher::class, function () { return new EventDispatcher(); }); + + $this->app->singleton(CheckstyleReporter::class, fn () => new CheckstyleReporter()); + $this->app->singleton(GitlabReporter::class, fn () => new GitlabReporter()); + $this->app->singleton(JsonReporter::class, fn () => new JsonReporter()); + $this->app->singleton(JunitReporter::class, fn () => new JunitReporter()); + $this->app->singleton(TextReporter::class, fn () => new TextReporter()); + $this->app->singleton(XmlReporter::class, fn () => new XmlReporter()); + + $this->app->tag([ + CheckstyleReporter::class, + GitlabReporter::class, + JsonReporter::class, + JunitReporter::class, + TextReporter::class, + XmlReporter::class, + ], 'reporters'); } } From 517d4c532f34acb8a4c72d83238ab76a14c1482f Mon Sep 17 00:00:00 2001 From: Dzianis Kotau Date: Wed, 20 Jul 2022 16:34:51 +0400 Subject: [PATCH 3/6] Clear code --- app/Actions/ElaborateSummary.php | 9 +++++++-- app/Actions/FixCode.php | 13 ------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/app/Actions/ElaborateSummary.php b/app/Actions/ElaborateSummary.php index c2c89605..9f5eca80 100644 --- a/app/Actions/ElaborateSummary.php +++ b/app/Actions/ElaborateSummary.php @@ -72,7 +72,10 @@ private function format($summary) return; } - $report = $this->report($summary); + if (($report = $this->report($summary)) === null) { + return; + } + if ($this->input->getOption('report') === null) { $this->output->writeln($report); } else { @@ -82,7 +85,7 @@ private function format($summary) /** * @param ReportSummary $summary - * @return string + * @return string|null */ private function report($summary) { @@ -94,5 +97,7 @@ private function report($summary) return $reporter->generate($summary); } } + + return null; } } diff --git a/app/Actions/FixCode.php b/app/Actions/FixCode.php index 1faf4070..319b0db3 100644 --- a/app/Actions/FixCode.php +++ b/app/Actions/FixCode.php @@ -7,9 +7,6 @@ class FixCode { - /** @var \PhpCsFixer\Console\Report\FixReport\ReporterInterface */ - protected $reporter; - /** * Creates a new Fix Code instance. * @@ -60,16 +57,6 @@ public function execute() $resolver->shouldStopOnViolation() ))->fix(); - $this->reporter = $resolver->getReporter(); - return tap([$totalFiles, $changes], fn () => $this->progress->unsubscribe()); } - - /** - * @return \PhpCsFixer\Console\Report\FixReport\ReporterInterface - */ - public function getReporter() - { - return $this->reporter; - } } From 9ef8a2daa8927b0d3e7dad3a6806bfe0d2b73cf8 Mon Sep 17 00:00:00 2001 From: Dzianis Kotau Date: Wed, 20 Jul 2022 18:36:59 +0400 Subject: [PATCH 4/6] Refactor reporters resolving --- app/Actions/ElaborateSummary.php | 36 +++++++++++-------- .../ConfigurationResolverFactory.php | 1 - app/Providers/AppServiceProvider.php | 22 ------------ tests/Feature/ReportTest.php | 2 +- 4 files changed, 23 insertions(+), 38 deletions(-) diff --git a/app/Actions/ElaborateSummary.php b/app/Actions/ElaborateSummary.php index 9f5eca80..68a3c82e 100644 --- a/app/Actions/ElaborateSummary.php +++ b/app/Actions/ElaborateSummary.php @@ -3,8 +3,14 @@ namespace App\Actions; use Illuminate\Console\Command; -use Illuminate\Support\Facades\App; +use PhpCsFixer\Console\Report\FixReport\CheckstyleReporter; +use PhpCsFixer\Console\Report\FixReport\GitlabReporter; +use PhpCsFixer\Console\Report\FixReport\JsonReporter; +use PhpCsFixer\Console\Report\FixReport\JunitReporter; use PhpCsFixer\Console\Report\FixReport\ReportSummary; +use PhpCsFixer\Console\Report\FixReport\TextReporter; +use PhpCsFixer\Console\Report\FixReport\XmlReporter; +use Symfony\Component\Console\Exception\InvalidOptionException; use Symfony\Component\Console\Output\OutputInterface; class ElaborateSummary @@ -72,9 +78,7 @@ private function format($summary) return; } - if (($report = $this->report($summary)) === null) { - return; - } + $report = $this->report($summary); if ($this->input->getOption('report') === null) { $this->output->writeln($report); @@ -84,20 +88,24 @@ private function format($summary) } /** - * @param ReportSummary $summary - * @return string|null + * @param ReportSummary $summary + * @return string + * + * @throws InvalidOptionException */ private function report($summary) { - $reporters = App::tagged('reporters'); $format = $this->input->getOption('format'); + $reporter = match ($format) { + 'checkstyle' => new CheckstyleReporter(), + 'gitlab' => new GitlabReporter(), + 'json' => new JsonReporter(), + 'junit' => new JunitReporter(), + 'txt' => new TextReporter(), + 'xml' => new XmlReporter(), + default => throw new InvalidOptionException(sprintf('Format "%s" is not supported.', $format)) + }; - foreach ($reporters as $reporter) { - if ($format === $reporter->getFormat()) { - return $reporter->generate($summary); - } - } - - return null; + return $reporter->generate($summary); } } diff --git a/app/Factories/ConfigurationResolverFactory.php b/app/Factories/ConfigurationResolverFactory.php index 16ed2fed..cc26e50c 100644 --- a/app/Factories/ConfigurationResolverFactory.php +++ b/app/Factories/ConfigurationResolverFactory.php @@ -65,7 +65,6 @@ public static function fromIO($input, $output) 'stop-on-violation' => false, 'verbosity' => $output->getVerbosity(), 'show-progress' => 'true', - 'format' => $input->getOption('format'), ], Project::path(), new ToolInfo(), diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index b2e658d0..4aec48cc 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,12 +3,6 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; -use PhpCsFixer\Console\Report\FixReport\CheckstyleReporter; -use PhpCsFixer\Console\Report\FixReport\GitlabReporter; -use PhpCsFixer\Console\Report\FixReport\JsonReporter; -use PhpCsFixer\Console\Report\FixReport\JunitReporter; -use PhpCsFixer\Console\Report\FixReport\TextReporter; -use PhpCsFixer\Console\Report\FixReport\XmlReporter; use PhpCsFixer\Error\ErrorsManager; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -38,21 +32,5 @@ public function register() $this->app->singleton(EventDispatcher::class, function () { return new EventDispatcher(); }); - - $this->app->singleton(CheckstyleReporter::class, fn () => new CheckstyleReporter()); - $this->app->singleton(GitlabReporter::class, fn () => new GitlabReporter()); - $this->app->singleton(JsonReporter::class, fn () => new JsonReporter()); - $this->app->singleton(JunitReporter::class, fn () => new JunitReporter()); - $this->app->singleton(TextReporter::class, fn () => new TextReporter()); - $this->app->singleton(XmlReporter::class, fn () => new XmlReporter()); - - $this->app->tag([ - CheckstyleReporter::class, - GitlabReporter::class, - JsonReporter::class, - JunitReporter::class, - TextReporter::class, - XmlReporter::class, - ], 'reporters'); } } diff --git a/tests/Feature/ReportTest.php b/tests/Feature/ReportTest.php index 9235d4ad..0cd955ee 100644 --- a/tests/Feature/ReportTest.php +++ b/tests/Feature/ReportTest.php @@ -1,7 +1,7 @@ base_path('tests/Fixtures/with-fixable-issues'), '--preset' => 'psr12', From 33363d6d29dc35ba98d1a9479946e71cbd232dd0 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 21 Jul 2022 12:22:11 +0100 Subject: [PATCH 5/6] Removes docs --- README.md | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/README.md b/README.md index e35bada0..0cb76011 100644 --- a/README.md +++ b/README.md @@ -20,44 +20,6 @@ Documentation for Pint can be found on the [Laravel website](https://laravel.com/docs/pint). -### Format output - -You can output results in different [format](https://cs.symfony.com/doc/usage.html) supported by PHP-CS-Fixer. -This is especially useful in CI/CD pipelines (for example, [Annotations via the Checks API](https://docs.github.com/en/rest/checks)): - -```bash -pint --test --format=checkstyle -``` - -Then you can send output to the tool that will read it and create annotations for you. For example, for GitHub Actions -you can implement following job: - -```yaml -- name: Show Pint results in PR -run: pint --test --format=checkstyle | cs2pr -``` - -### Save report to the file -If you run `pint` with [--format](#format-output) option, it will suppress standard pint's output. If you want to see -both standard and formatted output, you can save formatted output to report file: - -```bash -pint --test --format=checkstyle --report=checkstyle.xml -``` - -This is especially useful in CI/CD pipelines (for example, [Annotations via the Checks API](https://docs.github.com/en/rest/checks)) -when you want to have both job logs and job annotations. You can have one job to generate report file and second job -to read from report file. For example, for GitHub Actions you can implement following jobs: - -```yaml -- name: Check PHP code style -continue-on-error: true -run: pint --test --format=checkstyle --report=./pint-report.xml - -- name: Show Pint results in PR -run: cs2pr ./pint-report.xml -``` - ## Contributing From c28accfcc1be9cc1750219e0f6d0b4a079b66a56 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 21 Jul 2022 14:05:57 +0100 Subject: [PATCH 6/6] Reworks `--format` option --- app/Actions/ElaborateSummary.php | 69 +++++++++----------------------- app/Actions/FixCode.php | 5 +-- app/Commands/DefaultCommand.php | 3 +- tests/Feature/FormatTest.php | 15 +++---- tests/Feature/ReportTest.php | 25 ------------ 5 files changed, 27 insertions(+), 90 deletions(-) delete mode 100644 tests/Feature/ReportTest.php diff --git a/app/Actions/ElaborateSummary.php b/app/Actions/ElaborateSummary.php index 68a3c82e..c8efb865 100644 --- a/app/Actions/ElaborateSummary.php +++ b/app/Actions/ElaborateSummary.php @@ -3,14 +3,7 @@ namespace App\Actions; use Illuminate\Console\Command; -use PhpCsFixer\Console\Report\FixReport\CheckstyleReporter; -use PhpCsFixer\Console\Report\FixReport\GitlabReporter; -use PhpCsFixer\Console\Report\FixReport\JsonReporter; -use PhpCsFixer\Console\Report\FixReport\JunitReporter; -use PhpCsFixer\Console\Report\FixReport\ReportSummary; -use PhpCsFixer\Console\Report\FixReport\TextReporter; -use PhpCsFixer\Console\Report\FixReport\XmlReporter; -use Symfony\Component\Console\Exception\InvalidOptionException; +use PhpCsFixer\Console\Report\FixReport; use Symfony\Component\Console\Output\OutputInterface; class ElaborateSummary @@ -42,7 +35,7 @@ public function __construct( */ public function execute($totalFiles, $changes) { - $summary = new ReportSummary( + $summary = new FixReport\ReportSummary( $changes, 0, 0, @@ -51,13 +44,10 @@ public function execute($totalFiles, $changes) $this->output->isDecorated() ); - $this->format($summary); - - if ( - $this->input->getOption('format') === 'txt' - || $this->input->getOption('report') !== null - ) { - tap($summary, fn () => $this->summaryOutput->handle($summary, $totalFiles))->getChanged(); + if ($this->input->getOption('format')) { + $this->displayUsingFormatter($summary, $totalFiles); + } else { + $this->summaryOutput->handle($summary, $totalFiles); } $failure = ($summary->isDryRun() && count($changes) > 0) @@ -69,43 +59,24 @@ public function execute($totalFiles, $changes) } /** - * @param ReportSummary $summary - * @return void - */ - private function format($summary) - { - if ($this->input->getOption('format') === 'txt') { - return; - } - - $report = $this->report($summary); - - if ($this->input->getOption('report') === null) { - $this->output->writeln($report); - } else { - file_put_contents($this->input->getOption('report'), stripcslashes($report), LOCK_EX); - } - } - - /** - * @param ReportSummary $summary - * @return string + * Formats the given summary using the "selected" formatter. * - * @throws InvalidOptionException + * @param \PhpCsFixer\Console\Report\FixReport\ReportSummary $summary + * @param int $totalFiles + * @return void */ - private function report($summary) + protected function displayUsingFormatter($summary, $totalFiles) { - $format = $this->input->getOption('format'); - $reporter = match ($format) { - 'checkstyle' => new CheckstyleReporter(), - 'gitlab' => new GitlabReporter(), - 'json' => new JsonReporter(), - 'junit' => new JunitReporter(), - 'txt' => new TextReporter(), - 'xml' => new XmlReporter(), - default => throw new InvalidOptionException(sprintf('Format "%s" is not supported.', $format)) + $reporter = match ($format = $this->input->getOption('format')) { + 'checkstyle' => new FixReport\CheckstyleReporter(), + 'gitlab' => new FixReport\GitlabReporter(), + 'json' => new FixReport\JsonReporter(), + 'junit' => new FixReport\JunitReporter(), + 'txt' => new FixReport\TextReporter(), + 'xml' => new FixReport\XmlReporter(), + default => abort(1, sprintf('Format [%s] is not supported.', $format)), }; - return $reporter->generate($summary); + $this->output->write($reporter->generate($summary)); } } diff --git a/app/Actions/FixCode.php b/app/Actions/FixCode.php index 319b0db3..65105cea 100644 --- a/app/Actions/FixCode.php +++ b/app/Actions/FixCode.php @@ -36,10 +36,7 @@ public function execute() { [$resolver, $totalFiles] = ConfigurationResolverFactory::fromIO($this->input, $this->output); - if ( - $this->input->getOption('format') === 'txt' - || $this->input->getOption('report') !== null - ) { + if (is_null($this->input->getOption('format'))) { $this->progress->subscribe(); } diff --git a/app/Commands/DefaultCommand.php b/app/Commands/DefaultCommand.php index be3aab50..10137d72 100644 --- a/app/Commands/DefaultCommand.php +++ b/app/Commands/DefaultCommand.php @@ -38,8 +38,7 @@ protected function configure() new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The configuration that should be used'), new InputOption('preset', '', InputOption::VALUE_REQUIRED, 'The preset that should be used'), new InputOption('test', '', InputOption::VALUE_NONE, 'Test for code style errors without fixing them'), - new InputOption('format', '', InputOption::VALUE_REQUIRED, 'The output format', 'txt'), - new InputOption('report', '', InputOption::VALUE_REQUIRED, 'The file to output report to'), + new InputOption('format', '', InputOption::VALUE_REQUIRED, 'The output format that should be used'), ] ); } diff --git a/tests/Feature/FormatTest.php b/tests/Feature/FormatTest.php index 5a73fd7e..18dc8d7e 100644 --- a/tests/Feature/FormatTest.php +++ b/tests/Feature/FormatTest.php @@ -12,8 +12,7 @@ ->toContain('') ->toContain('') ->toContain('') - ->not - ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ + ->not->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', ]))); }); @@ -29,8 +28,7 @@ ->and($output) ->toBeJson() ->toContain('appliedFixers') - ->not - ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ + ->not->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', ]))); }); @@ -45,8 +43,7 @@ expect($statusCode)->toBe(1) ->and($output) ->toContain('') - ->not - ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ + ->not->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', ]))); }); @@ -62,8 +59,7 @@ ->and($output) ->toContain('') ->toContain('CDATA') - ->not - ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ + ->not->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', ]))); }); @@ -79,8 +75,7 @@ ->and($output) ->toBeJson() ->toContain('fingerprint') - ->not - ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ + ->not->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', ]))); }); diff --git a/tests/Feature/ReportTest.php b/tests/Feature/ReportTest.php deleted file mode 100644 index 0cd955ee..00000000 --- a/tests/Feature/ReportTest.php +++ /dev/null @@ -1,25 +0,0 @@ - base_path('tests/Fixtures/with-fixable-issues'), - '--preset' => 'psr12', - '--format' => 'checkstyle', - '--report' => $report, - ]); - - expect($statusCode)->toBe(1) - ->and($output) - ->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [ - 'tests', 'Fixtures', 'with-fixable-issues', 'file.php', - ]))) - ->and(file_exists($report)) - ->toBeTrue() - ->and(file_get_contents($report)) - ->toContain('') - ->toContain('') - ->toContain('') - ->and(unlink($report)) - ->toBeTrue(); -});