Skip to content

Commit

Permalink
Rename Sniff to Rule (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Nov 28, 2023
1 parent dd28e60 commit 26d8bef
Show file tree
Hide file tree
Showing 108 changed files with 611 additions and 611 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,24 @@ Removes any space before and after opening and closing of arrays and hashes.

### Standard

By default, the generic standard is enabled with the twig coding standard rules and the following sniffs:
By default, the generic standard is enabled with the twig coding standard rules and the following rules:

- `BlankEOFSniff`: ensures that files end with one blank line.
- `BlockNameSpacingSniff`: ensure there is one space before and after block names.
- `EmptyLinesSniff`: ensures that 2 empty lines do not follow each other.
- `IndentSniff`: ensures that files are not indented with tabs.
- `TrailingCommaSingleLineSniff`: ensures that single-line arrays, objects and argument lists do not have a trailing comma.
- `TrailingSpaceSniff`: ensures that files have no trailing spaces.
- `BlankEOFRule`: ensures that files end with one blank line.
- `BlockNameSpacingRule`: ensure there is one space before and after block names.
- `EmptyLinesRule`: ensures that 2 empty lines do not follow each other.
- `IndentRule`: ensures that files are not indented with tabs.
- `TrailingCommaSingleLineRule`: ensures that single-line arrays, objects and argument lists do not have a trailing comma.
- `TrailingSpaceRule`: ensures that files have no trailing spaces.

If you want to use the basic Twig standard, another standard and/or add/disable a sniff, you can provide
If you want to use the basic Twig standard, another standard and/or add/disable a rule, you can provide
your own configuration with a `.twig-cs-fixer.php` file which returns a `TwigCsFixer\Config\Config` class:

```php
<?php

$ruleset = new TwigCsFixer\Ruleset\Ruleset();
$ruleset->addStandard(new TwigCsFixer\Standard\Generic());
$ruleset->removeSniff(TwigCsFixer\Sniff\EmptyLinesSniff::class);
$ruleset->removeRule(TwigCsFixer\Rules\EmptyLinesRule::class);

$config = new TwigCsFixer\Config\Config();
$config->setRuleset($ruleset);
Expand Down
8 changes: 4 additions & 4 deletions src/Cache/CacheEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public static function fromJson(string $json): Cache
Assert::string($data['php_version']);
Assert::keyExists($data, 'fixer_version');
Assert::string($data['fixer_version']);
Assert::keyExists($data, 'sniffs');
Assert::isArray($data['sniffs']);
Assert::keyExists($data, 'rules');
Assert::isArray($data['rules']);

$signature = new Signature(
$data['php_version'],
$data['fixer_version'],
$data['sniffs'],
$data['rules'],
);

$cache = new Cache($signature);
Expand All @@ -64,7 +64,7 @@ public static function toJson(Cache $cache): string
return json_encode([
'php_version' => $signature->getPhpVersion(),
'fixer_version' => $signature->getFixerVersion(),
'sniffs' => $signature->getSniffs(),
'rules' => $signature->getRules(),
'hashes' => $cache->getHashes(),
], \JSON_THROW_ON_ERROR);
}
Expand Down
22 changes: 11 additions & 11 deletions src/Cache/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

namespace TwigCsFixer\Cache;

use TwigCsFixer\Rules\ConfigurableRuleInterface;
use TwigCsFixer\Ruleset\Ruleset;
use TwigCsFixer\Sniff\ConfigurableSniffInterface;

final class Signature
{
/**
* @param array<mixed> $sniffs
* @param array<mixed> $rules
*/
public function __construct(
private string $phpVersion,
private string $fixerVersion,
private array $sniffs
private array $rules
) {
}

Expand All @@ -24,14 +24,14 @@ public static function fromRuleset(
string $fixerVersion,
Ruleset $ruleset,
): self {
$sniffs = [];
foreach ($ruleset->getSniffs() as $sniff) {
$sniffs[$sniff::class] = $sniff instanceof ConfigurableSniffInterface
? $sniff->getConfiguration()
$rules = [];
foreach ($ruleset->getRules() as $rule) {
$rules[$rule::class] = $rule instanceof ConfigurableRuleInterface
? $rule->getConfiguration()
: null;
}

return new self($phpVersion, $fixerVersion, $sniffs);
return new self($phpVersion, $fixerVersion, $rules);
}

public function getPhpVersion(): string
Expand All @@ -47,15 +47,15 @@ public function getFixerVersion(): string
/**
* @return array<mixed>
*/
public function getSniffs(): array
public function getRules(): array
{
return $this->sniffs;
return $this->rules;
}

public function equals(self $signature): bool
{
return $this->phpVersion === $signature->getPhpVersion()
&& $this->fixerVersion === $signature->getFixerVersion()
&& $this->sniffs === $signature->getSniffs();
&& $this->rules === $signature->getRules();
}
}
2 changes: 1 addition & 1 deletion src/Command/TwigCsFixerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Webmozart\Assert\Assert;

/**
* TwigCsFixer stands for "Twig Code Sniffer Fixer" and will check twig template of your project.
* TwigCsFixer stands for "Twig Code Style Fixer" and will check twig template of your project.
*/
final class TwigCsFixerCommand extends Command
{
Expand Down
26 changes: 13 additions & 13 deletions src/Report/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class Report
public const MESSAGE_TYPE_FATAL = 'FATAL';

/**
* @var array<string, list<SniffViolation>>
* @var array<string, list<Violation>>
*/
private array $violationsByFile = [];

Expand All @@ -43,36 +43,36 @@ public function __construct(iterable $files)
}
}

public function addViolation(SniffViolation $sniffViolation): self
public function addViolation(Violation $violation): self
{
$filename = $sniffViolation->getFilename();
$filename = $violation->getFilename();
if (!isset($this->violationsByFile[$filename])) {
throw new InvalidArgumentException(
sprintf('The file "%s" is not handled by this report.', $filename)
);
}

// Update stats
switch ($sniffViolation->getLevel()) {
case SniffViolation::LEVEL_NOTICE:
switch ($violation->getLevel()) {
case Violation::LEVEL_NOTICE:
$this->totalNotices++;
break;
case SniffViolation::LEVEL_WARNING:
case Violation::LEVEL_WARNING:
$this->totalWarnings++;
break;
case SniffViolation::LEVEL_ERROR:
case SniffViolation::LEVEL_FATAL:
case Violation::LEVEL_ERROR:
case Violation::LEVEL_FATAL:
$this->totalErrors++;
break;
}

$this->violationsByFile[$filename][] = $sniffViolation;
$this->violationsByFile[$filename][] = $violation;

return $this;
}

/**
* @return list<SniffViolation>
* @return list<Violation>
*/
public function getFileViolations(string $filename, ?string $level = null): array
{
Expand All @@ -89,13 +89,13 @@ public function getFileViolations(string $filename, ?string $level = null): arra
return array_values(
array_filter(
$this->violationsByFile[$filename],
static fn (SniffViolation $message): bool => $message->getLevel() >= SniffViolation::getLevelAsInt($level)
static fn (Violation $message): bool => $message->getLevel() >= Violation::getLevelAsInt($level)
)
);
}

/**
* @return list<SniffViolation>
* @return list<Violation>
*/
public function getViolations(?string $level = null): array
{
Expand All @@ -108,7 +108,7 @@ public function getViolations(?string $level = null): array
return array_values(
array_filter(
$messages,
static fn (SniffViolation $message): bool => $message->getLevel() >= SniffViolation::getLevelAsInt($level)
static fn (Violation $message): bool => $message->getLevel() >= Violation::getLevelAsInt($level)
)
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Report/Reporter/CheckstyleReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Symfony\Component\Console\Output\OutputInterface;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\SniffViolation;
use TwigCsFixer\Report\Violation;

final class CheckstyleReporter implements ReporterInterface
{
Expand All @@ -28,7 +28,7 @@ public function display(OutputInterface $output, Report $report, ?string $level
foreach ($fileViolations as $violation) {
$line = (string) $violation->getLine();
$linePosition = (string) $violation->getLinePosition();
$sniffName = $violation->getSniffName();
$ruleName = $violation->getRuleName();

$text .= ' <error';
if ('' !== $line) {
Expand All @@ -37,10 +37,10 @@ public function display(OutputInterface $output, Report $report, ?string $level
if ('' !== $linePosition) {
$text .= ' column="'.$linePosition.'"';
}
$text .= ' severity="'.strtolower(SniffViolation::getLevelAsString($violation->getLevel())).'"';
$text .= ' severity="'.strtolower(Violation::getLevelAsString($violation->getLevel())).'"';
$text .= ' message="'.$this->xmlEncode($violation->getMessage()).'"';
if (null !== $sniffName) {
$text .= ' source="'.$sniffName.'"';
if (null !== $ruleName) {
$text .= ' source="'.$ruleName.'"';
}
$text .= '/>'."\n";
}
Expand Down
6 changes: 3 additions & 3 deletions src/Report/Reporter/GithubReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Symfony\Component\Console\Output\OutputInterface;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\SniffViolation;
use TwigCsFixer\Report\Violation;

/**
* Allow errors to be reported in pull-requests diff when run in a GitHub Action
Expand All @@ -22,8 +22,8 @@ public function display(OutputInterface $output, Report $report, ?string $level
$violations = $report->getViolations($level);
foreach ($violations as $violation) {
$text = match ($violation->getLevel()) {
SniffViolation::LEVEL_NOTICE => '::notice',
SniffViolation::LEVEL_WARNING => '::warning',
Violation::LEVEL_NOTICE => '::notice',
Violation::LEVEL_WARNING => '::warning',
default => '::error',
};

Expand Down
4 changes: 2 additions & 2 deletions src/Report/Reporter/JUnitReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Symfony\Component\Console\Output\OutputInterface;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\SniffViolation;
use TwigCsFixer\Report\Violation;

final class JUnitReporter implements ReporterInterface
{
Expand All @@ -29,7 +29,7 @@ public function display(OutputInterface $output, Report $report, ?string $level
foreach ($violations as $violation) {
$text .= $this->createTestCase(
sprintf('%s:%s', $violation->getFilename(), $violation->getLine() ?? 0),
strtolower(SniffViolation::getLevelAsString($violation->getLevel())),
strtolower(Violation::getLevelAsString($violation->getLevel())),
$violation->getMessage()
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Report/Reporter/TextReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\SniffViolation;
use TwigCsFixer\Report\Violation;

/**
* Human-readable output with context.
Expand Down Expand Up @@ -68,7 +68,7 @@ public function display(OutputInterface $output, Report $report, ?string $level
$rows[] = new TableSeparator();
}

$messageLevel = SniffViolation::getLevelAsString($violation->getLevel());
$messageLevel = Violation::getLevelAsString($violation->getLevel());
$rows[] = [
new TableCell(sprintf('<comment>%s</comment>', $messageLevel)),
implode("\n", $formattedText),
Expand Down Expand Up @@ -119,7 +119,7 @@ private function getContext(string $template, int $line): array
return array_map(fn (string $code): string => substr($code, min($indents)), $result);
}

private function formatErrorMessage(SniffViolation $message): string
private function formatErrorMessage(Violation $message): string
{
return sprintf(
sprintf('<fg=red>%s</fg=red>', self::ERROR_LINE_FORMAT),
Expand Down
10 changes: 5 additions & 5 deletions src/Report/SniffViolation.php → src/Report/Violation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use InvalidArgumentException;

/**
* Wrapper class that represents a violation to a sniff with context.
* Wrapper class that represents a violation to a rule with context.
*/
final class SniffViolation
final class Violation
{
public const LEVEL_NOTICE = 0;
public const LEVEL_WARNING = 1;
Expand All @@ -22,7 +22,7 @@ public function __construct(
private string $filename,
private ?int $line = null,
private ?int $linePosition = null,
private ?string $sniffName = null
private ?string $ruleName = null
) {
}

Expand Down Expand Up @@ -77,8 +77,8 @@ public function getLinePosition(): ?int
return $this->linePosition;
}

public function getSniffName(): ?string
public function getRuleName(): ?string
{
return $this->sniffName;
return $this->ruleName;
}
}
Loading

0 comments on commit 26d8bef

Please sign in to comment.