Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use custom reporters #268

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ UPGRADE FROM 2.x to 3.0
- The position of `TrailingCommaMultiLineRule` error changed.
- The position of `TrailingCommaSingleLineRule` error changed.
- `TwigCsFixer\Command\TwigCsFixerCommand` class moved to `TwigCsFixer\Console\Command` folder.
- `TwigCsFixer\Report\Reporter\ReporterInterface` now require a `getName` method.

If you never implemented a custom rule, nothing else changed. Otherwise, ...

Expand Down
3 changes: 3 additions & 0 deletions docs/command.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Supported formats are:
- `junit` following JUnit schema XML from Jenkins.
- `null` if you don't want any reporting.

If you implemented and configured [a custom reporter](configuration.md#Custom-Reporters),
it can be used too.

## Debug mode

The `--debug` option displays error identifiers instead of messages. This is
Expand Down
19 changes: 17 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ vendor/bin/twig-cs-fixer lint --config=dir/.twig-cs-fixer.php /path/to/code
## Non fixable rules

Most of the rules are automatically fixable but some of them are not.
By default, the twig-cs-fixer disable all the non-fixable-rules, but you can still allow them in the config file:
By default, the twig-cs-fixer disable all the non-fixable-rules, but you can still allow them in your config file:

```php
<?php
Expand All @@ -75,7 +75,7 @@ Still, these rules can be useful for some static analysis. All of them can be fo
By default, all `.twig` files in the current directory are linted, except the ones in the `vendor` directory.

If you want to lint specific files or directories you can pass them as argument.
If you want a more sophisticated rule, you can configure it in the config file:
If you want a more sophisticated rule, you can configure it in your config file:

```php
<?php
Expand Down Expand Up @@ -123,6 +123,21 @@ $config->setCacheFile(null);
return $config;
```

## Custom Reporters

By default, the output of the tool is rendered in one the following formats:
`text`, `checkstyle`, `github`, `junit`, `null`. If you want to use a custom
format for reporting, you can configure it in your config file:

```php
<?php

$config = new TwigCsFixer\Config\Config();
$config->addCustomReporter(new App\Twig\CustomReporter());

return $config;
```

## Token parser, Twig Extension & Node Visitors

If you're using custom token parsers or binary/unary operators, they can be added in your config:
Expand Down
24 changes: 24 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Twig\TokenParser\TokenParserInterface;
use TwigCsFixer\Cache\Manager\CacheManagerInterface;
use TwigCsFixer\File\Finder as TwigCsFinder;
use TwigCsFixer\Report\Reporter\ReporterInterface;
use TwigCsFixer\Ruleset\Ruleset;
use TwigCsFixer\Standard\TwigCsFixer;

Expand All @@ -30,6 +31,11 @@ final class Config

private ?CacheManagerInterface $cacheManager = null;

/**
* @var list<ReporterInterface>
*/
private array $customReporters = [];

/**
* @var list<ExtensionInterface>
*/
Expand Down Expand Up @@ -109,13 +115,31 @@ public function getCacheFile(): ?string
return $this->cacheFile;
}

/**
* @return $this
*/
public function setCacheFile(?string $cacheFile): self
{
$this->cacheFile = $cacheFile;

return $this;
}

public function addCustomReporter(ReporterInterface $reporter): self
{
$this->customReporters[] = $reporter;

return $this;
}

/**
* @return list<ReporterInterface>
*/
public function getCustomReporters(): array
{
return $this->customReporters;
}

/**
* @return $this
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/TwigCsFixerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private function runLinter(Config $config, InputInterface $input, OutputInterfac
$input->getOption('fix') ? new Fixer($tokenizer) : null,
);

$reporterFactory = new ReporterFactory();
$reporterFactory = new ReporterFactory($config->getCustomReporters());
$reporter = $reporterFactory->getReporter($input->getOption('report'));
$reporter->display($output, $report, $input->getOption('level'), $input->getOption('debug'));

Expand Down
5 changes: 5 additions & 0 deletions src/Report/Reporter/CheckstyleReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ final class CheckstyleReporter implements ReporterInterface
{
public const NAME = 'checkstyle';

public function getName(): string
{
return self::NAME;
}

public function display(
OutputInterface $output,
Report $report,
Expand Down
5 changes: 5 additions & 0 deletions src/Report/Reporter/GithubReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ final class GithubReporter implements ReporterInterface
{
public const NAME = 'github';

public function getName(): string
{
return self::NAME;
}

public function display(
OutputInterface $output,
Report $report,
Expand Down
5 changes: 5 additions & 0 deletions src/Report/Reporter/JUnitReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ final class JUnitReporter implements ReporterInterface
{
public const NAME = 'junit';

public function getName(): string
{
return self::NAME;
}

public function display(
OutputInterface $output,
Report $report,
Expand Down
5 changes: 5 additions & 0 deletions src/Report/Reporter/NullReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ final class NullReporter implements ReporterInterface
{
public const NAME = 'null';

public function getName(): string
{
return self::NAME;
}

public function display(
OutputInterface $output,
Report $report,
Expand Down
2 changes: 2 additions & 0 deletions src/Report/Reporter/ReporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public function display(
?string $level,
bool $debug
): void;

public function getName(): string;
}
5 changes: 5 additions & 0 deletions src/Report/Reporter/TextReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ final class TextReporter implements ReporterInterface
private const ERROR_LINE_FORMAT = '%-5s| %s';
private const ERROR_LINE_WIDTH = 120;

public function getName(): string
{
return self::NAME;
}

public function display(
OutputInterface $output,
Report $report,
Expand Down
14 changes: 14 additions & 0 deletions src/Report/ReporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,22 @@

final class ReporterFactory
{
/**
* @param list<ReporterInterface> $customReporters
*/
public function __construct(
private array $customReporters = []
) {
}

public function getReporter(string $format = TextReporter::NAME): ReporterInterface
{
foreach ($this->customReporters as $reporter) {
if ($format === $reporter->getName()) {
return $reporter;
}
}

return match ($format) {
NullReporter::NAME => new NullReporter(),
TextReporter::NAME => new TextReporter(),
Expand Down
10 changes: 10 additions & 0 deletions tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use TwigCsFixer\Cache\Manager\NullCacheManager;
use TwigCsFixer\Config\Config;
use TwigCsFixer\File\Finder as TwigCsFinder;
use TwigCsFixer\Report\Reporter\NullReporter;
use TwigCsFixer\Ruleset\Ruleset;
use TwigCsFixer\Standard\TwigCsFixer;

Expand Down Expand Up @@ -73,6 +74,15 @@ public function testConfigCacheFile(): void
static::assertNull($config->getCacheFile());
}

public function testConfigCustomReporters(): void
{
$config = new Config();
static::assertCount(0, $config->getCustomReporters());

$config->addCustomReporter(new NullReporter());
static::assertCount(1, $config->getCustomReporters());
}

public function testConfigTokenParsers(): void
{
$config = new Config();
Expand Down
5 changes: 5 additions & 0 deletions tests/Report/Reporter/CheckstyleReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

final class CheckstyleReporterTest extends TestCase
{
public function testGetName(): void
{
static::assertSame(CheckstyleReporter::NAME, (new CheckstyleReporter())->getName());
}

/**
* @dataProvider displayDataProvider
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/Report/Reporter/GithubReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

final class GithubReporterTest extends TestCase
{
public function testGetName(): void
{
static::assertSame(GithubReporter::NAME, (new GithubReporter())->getName());
}

/**
* @dataProvider displayDataProvider
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/Report/Reporter/JUnitReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

final class JUnitReporterTest extends TestCase
{
public function testGetName(): void
{
static::assertSame(JUnitReporter::NAME, (new JUnitReporter())->getName());
}

/**
* @dataProvider displayDataProvider
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/Report/Reporter/NullReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

final class NullReporterTest extends TestCase
{
public function testGetName(): void
{
static::assertSame(NullReporter::NAME, (new NullReporter())->getName());
}

/**
* @dataProvider displayDataProvider
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/Report/Reporter/TextReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

final class TextReporterTest extends TestCase
{
public function testGetName(): void
{
static::assertSame(TextReporter::NAME, (new TextReporter())->getName());
}

/**
* @dataProvider displayDataProvider
*/
Expand Down
27 changes: 26 additions & 1 deletion tests/Report/ReporterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace TwigCsFixer\Tests\Report;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\OutputInterface;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\Reporter\CheckstyleReporter;
use TwigCsFixer\Report\Reporter\GithubReporter;
use TwigCsFixer\Report\Reporter\JUnitReporter;
use TwigCsFixer\Report\Reporter\NullReporter;
use TwigCsFixer\Report\Reporter\ReporterInterface;
use TwigCsFixer\Report\Reporter\TextReporter;
use TwigCsFixer\Report\ReporterFactory;

Expand All @@ -26,11 +29,33 @@ public function testGetReporter(): void
static::assertInstanceOf(GithubReporter::class, $reporterFactory->getReporter(GithubReporter::NAME));
}

public function testGetMessageForAnotherFile(): void
public function testGetUnsupporterReporter(): void
{
$reporterFactory = new ReporterFactory();

$this->expectExceptionMessage('No reporter supports the format "foo".');
$reporterFactory->getReporter('foo');
}

public function testGetCustomReporter(): void
{
$fooReporter = new class() implements ReporterInterface {
public function getName(): string
{
return 'foo';
}

public function display(
OutputInterface $output,
Report $report,
?string $level,
bool $debug
): void {
}
};

$reporterFactory = new ReporterFactory([$fooReporter]);

static::assertSame($fooReporter, $reporterFactory->getReporter('foo'));
}
}
Loading