Skip to content
Draft
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
90 changes: 90 additions & 0 deletions src/Report/IgnoredViolationId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Report;

final class IgnoredViolationId
{
public function __construct(
private ?string $ruleIdentifier = null,
private ?string $messageIdentifier = null,
private ?int $startLine = null,
private ?int $startLinePosition = null,
private ?int $endLine = null,
private ?int $endLinePosition = null,
) {
}

public static function fromString(string $string, ?int $line = null): self
{
$exploded = explode(':', $string);
$name = $exploded[0];
$explodedName = '' !== $name ? explode('.', $name) : null;

$line ??= isset($exploded[1]) && '' !== $exploded[1] ? (int) $exploded[1] : null;
$linePosition = isset($exploded[2]) && '' !== $exploded[2] ? (int) $exploded[2] : null;

return new self(
$explodedName[0] ?? null,
$explodedName[1] ?? null,
$line,
$linePosition,
$line,
$linePosition
);
}

public function match(ViolationId $violationId): bool
{
return $this->matchIdentifier($this->ruleIdentifier, $violationId->getRuleIdentifier())
&& $this->matchIdentifier($this->messageIdentifier, $violationId->getMessageIdentifier())
&& $this->matchLinePosition($violationId->getLine(), $violationId->getLinePosition());
}

private function matchIdentifier(?string $self, ?string $other): bool
{
return null === $self || strtolower($self) === strtolower((string) $other);
}

private function matchLinePosition(?int $line, ?int $linePosition): bool
{
if (null !== $this->startLine) {
if (null === $line || $line < $this->startLine) {
return false;
}
if (
$line === $this->startLine
&& null !== $this->startLinePosition
&& (null === $linePosition || $linePosition < $this->startLinePosition)
) {
return false;
}
} elseif (
null !== $this->startLinePosition
&& (null === $linePosition || $linePosition < $this->startLinePosition)
) {
return false;
}

if (null !== $this->endLine) {
if (null === $line || $line > $this->endLine) {
return false;
}
if (
$line === $this->endLine
&& null !== $this->endLinePosition
&& (null === $linePosition || $linePosition > $this->endLinePosition)
) {
return false;
}
} elseif (
null !== $this->endLinePosition
&& (null === $linePosition || $linePosition > $this->endLinePosition)
) {
return false;
}

return true;
}
}
23 changes: 10 additions & 13 deletions src/Report/ViolationId.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ public function __construct(
) {
}

public function getRuleIdentifier(): ?string
{
return $this->ruleIdentifier;
}

public function getMessageIdentifier(): ?string
{
return $this->messageIdentifier;
}

public function getLine(): ?int
{
return $this->line;
Expand Down Expand Up @@ -56,17 +66,4 @@ public function toString(): string
$this->linePosition ?? '',
), ':');
}

public function match(self $violationId): bool
{
return $this->matchValue($this->ruleIdentifier, $violationId->ruleIdentifier)
&& $this->matchValue($this->messageIdentifier, $violationId->messageIdentifier)
&& $this->matchValue($this->line, $violationId->line)
&& $this->matchValue($this->linePosition, $violationId->linePosition);
}

private function matchValue(string|int|null $self, string|int|null $other): bool
{
return null === $self || strtolower((string) $self) === strtolower((string) $other);
}
}
4 changes: 2 additions & 2 deletions src/Rules/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace TwigCsFixer\Rules;

use TwigCsFixer\Report\IgnoredViolationId;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\Violation;
use TwigCsFixer\Report\ViolationId;
use TwigCsFixer\Token\Token;
use TwigCsFixer\Token\Tokens;

Expand All @@ -24,7 +24,7 @@ final public function lintFile(Tokens $tokens, Report $report): void
}

/**
* @param list<ViolationId> $ignoredViolations
* @param list<IgnoredViolationId> $ignoredViolations
*/
protected function init(?Report $report, array $ignoredViolations = []): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Node/NodeRuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace TwigCsFixer\Rules\Node;

use Twig\NodeVisitor\NodeVisitorInterface;
use TwigCsFixer\Report\IgnoredViolationId;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\ViolationId;

interface NodeRuleInterface extends NodeVisitorInterface
{
/**
* @param list<ViolationId> $ignoredViolations
* @param list<IgnoredViolationId> $ignoredViolations
*/
public function setReport(Report $report, array $ignoredViolations = []): void;
}
3 changes: 2 additions & 1 deletion src/Rules/RuleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace TwigCsFixer\Rules;

use TwigCsFixer\Report\IgnoredViolationId;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\Violation;
use TwigCsFixer\Report\ViolationId;
Expand All @@ -13,7 +14,7 @@ trait RuleTrait
private ?Report $report = null;

/**
* @var list<ViolationId>
* @var list<IgnoredViolationId>
*/
private array $ignoredViolations = [];

Expand Down
6 changes: 3 additions & 3 deletions src/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Twig\Source;
use TwigCsFixer\Environment\StubbedEnvironment;
use TwigCsFixer\Exception\CannotTokenizeException;
use TwigCsFixer\Report\ViolationId;
use TwigCsFixer\Report\IgnoredViolationId;
use Webmozart\Assert\Assert;

/**
Expand Down Expand Up @@ -897,7 +897,7 @@ private function processIgnoredViolations(): void
};

if ('' === $ignoredViolations) {
$this->tokens->addIgnoredViolation(ViolationId::fromString($ignoredViolations, $line));
$this->tokens->addIgnoredViolation(IgnoredViolationId::fromString($ignoredViolations, $line));

return;
}
Expand All @@ -907,7 +907,7 @@ private function processIgnoredViolations(): void
if ('' === $ignoredViolation) {
continue;
}
$this->tokens->addIgnoredViolation(ViolationId::fromString($ignoredViolation, $line));
$this->tokens->addIgnoredViolation(IgnoredViolationId::fromString($ignoredViolation, $line));
}
}
}
8 changes: 4 additions & 4 deletions src/Token/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace TwigCsFixer\Token;

use TwigCsFixer\Report\ViolationId;
use TwigCsFixer\Report\IgnoredViolationId;

final class Tokens
{
Expand All @@ -24,7 +24,7 @@ final class Tokens
private array $indexes = [];

/**
* @var list<ViolationId>
* @var list<IgnoredViolationId>
*/
private array $ignoredViolations = [];

Expand Down Expand Up @@ -126,7 +126,7 @@ public function findPrevious(string|array $type, int $start, int $end = 0, bool
return false;
}

public function addIgnoredViolation(ViolationId $violationId): self
public function addIgnoredViolation(IgnoredViolationId $violationId): self
{
if ($this->readOnly) {
throw new \LogicException('Cannot add ignored violation because the tokens are in read-only mode.');
Expand All @@ -138,7 +138,7 @@ public function addIgnoredViolation(ViolationId $violationId): self
}

/**
* @return list<ViolationId>
* @return list<IgnoredViolationId>
*/
public function getIgnoredViolations(): array
{
Expand Down
83 changes: 83 additions & 0 deletions tests/Report/IgnoredViolationIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Tests\Report;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use TwigCsFixer\Report\IgnoredViolationId;
use TwigCsFixer\Report\ViolationId;

final class IgnoredViolationIdTest extends TestCase
{
/**
* @dataProvider matchDataProvider
*/
#[DataProvider('matchDataProvider')]
public function testMatch(IgnoredViolationId $ignoredViolationId, string $string2, bool $expected): void
{
$violationId = ViolationId::fromString($string2);
static::assertSame($expected, $ignoredViolationId->match($violationId));
}

/**
* @return iterable<array-key, array{IgnoredViolationId, string, bool}>
*/
public static function matchDataProvider(): iterable
{
yield [new IgnoredViolationId(), 'short', true];
yield [new IgnoredViolationId(), 'short.id:1:1', true];
yield [new IgnoredViolationId('short'), 'short', true];
yield [new IgnoredViolationId('short'), 'short.id:1:1', true];
yield [new IgnoredViolationId('short', 'id'), 'short.id:1:1', true];
yield [new IgnoredViolationId('short', 'notId'), 'short.id:1:1', false];
yield [new IgnoredViolationId('short', 'id'), 'short', false];
yield [new IgnoredViolationId('SHORT', 'ID'), 'short.id:1:1', true];
yield [new IgnoredViolationId('short', 'id', 2, 1, 2, 1), 'short.id:1:1', false];
yield [new IgnoredViolationId('short', 'id', 1, 2, 1, 2), 'short.id:1:1', false];
yield [new IgnoredViolationId('short', startLinePosition: 1, endLinePosition: 1), 'short.id:1:1', true];
yield [new IgnoredViolationId('short', 'id', startLinePosition: 1, endLinePosition: 1), 'short.id:1:1', true];

yield [new IgnoredViolationId(startLine: 10), 'short', false];
yield [new IgnoredViolationId(endLine: 10), 'short', false];
yield [new IgnoredViolationId(startLinePosition: 10), 'short', false];
yield [new IgnoredViolationId(endLinePosition: 10), 'short', false];

yield [new IgnoredViolationId(startLine: 10, endLine: 20), 'short:10', true];
yield [new IgnoredViolationId(startLine: 10, endLine: 20), 'short:15', true];
yield [new IgnoredViolationId(startLine: 10, endLine: 20), 'short:20', true];
yield [new IgnoredViolationId(startLine: 10, endLine: 20), 'short:9', false];
yield [new IgnoredViolationId(startLine: 10, endLine: 20), 'short:21', false];
yield [new IgnoredViolationId(startLinePosition: 10, endLinePosition: 20), 'short::10', true];
yield [new IgnoredViolationId(startLinePosition: 10, endLinePosition: 20), 'short::15', true];
yield [new IgnoredViolationId(startLinePosition: 10, endLinePosition: 20), 'short::20', true];
yield [new IgnoredViolationId(startLinePosition: 10, endLinePosition: 20), 'short::9', false];
yield [new IgnoredViolationId(startLinePosition: 10, endLinePosition: 20), 'short::21', false];

yield [new IgnoredViolationId(startLine: 10, startLinePosition: 20, endLine: 30, endLinePosition: 40), 'short:10:19', false];
yield [new IgnoredViolationId(startLine: 10, startLinePosition: 20, endLine: 30, endLinePosition: 40), 'short:10:20', true];
yield [new IgnoredViolationId(startLine: 10, startLinePosition: 20, endLine: 30, endLinePosition: 40), 'short:11:19', true];
yield [new IgnoredViolationId(startLine: 10, startLinePosition: 20, endLine: 30, endLinePosition: 40), 'short:29:41', true];
yield [new IgnoredViolationId(startLine: 10, startLinePosition: 20, endLine: 30, endLinePosition: 40), 'short:30:40', true];
yield [new IgnoredViolationId(startLine: 10, startLinePosition: 20, endLine: 30, endLinePosition: 40), 'short:30:41', false];

yield [new IgnoredViolationId(startLine: 10), 'short:9', false];
yield [new IgnoredViolationId(startLine: 10), 'short:10', true];
yield [new IgnoredViolationId(startLine: 10, startLinePosition: 10), 'short:10', false];
yield [new IgnoredViolationId(startLine: 10, startLinePosition: 10), 'short:11', true];
yield [new IgnoredViolationId(endLine: 10), 'short:10', true];
yield [new IgnoredViolationId(endLine: 10), 'short:11', false];
yield [new IgnoredViolationId(endLine: 10, endLinePosition: 10), 'short:10', false];
yield [new IgnoredViolationId(endLine: 10, endLinePosition: 10), 'short:9', true];

yield [new IgnoredViolationId(startLine: 10), 'short::10', false];
yield [new IgnoredViolationId(startLinePosition: 10), 'short::10', true];
yield [new IgnoredViolationId(startLinePosition: 10), 'short::9', false];
yield [new IgnoredViolationId(startLine: 10, startLinePosition: 10), 'short::10', false];
yield [new IgnoredViolationId(endLine: 10), 'short::10', false];
yield [new IgnoredViolationId(endLinePosition: 10), 'short::10', true];
yield [new IgnoredViolationId(endLinePosition: 10), 'short::11', false];
yield [new IgnoredViolationId(endLine: 10, endLinePosition: 10), 'short:10', false];
}
}
34 changes: 0 additions & 34 deletions tests/Report/ViolationIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public function testToString(
$linePosition
);
static::assertSame($expected, $violationId->toString());

$fromString = ViolationId::fromString($expected);
static::assertTrue($fromString->match($violationId));
static::assertTrue($violationId->match($fromString));
}

/**
Expand All @@ -47,34 +43,4 @@ public static function toStringDataProvider(): iterable
yield ['short', 'id', 1, null, 'short.id:1'];
yield ['short', 'id', 1, 1, 'short.id:1:1'];
}

/**
* @dataProvider matchDataProvider
*/
#[DataProvider('matchDataProvider')]
public function testMatch(string $string1, string $string2, bool $expected): void
{
$violationId1 = ViolationId::fromString($string1);
$violationId2 = ViolationId::fromString($string2);
static::assertSame($expected, $violationId1->match($violationId2));
}

/**
* @return iterable<array-key, array{string, string, bool}>
*/
public static function matchDataProvider(): iterable
{
yield ['', 'short', true];
yield ['', 'short.id:1:1', true];
yield ['short', 'short', true];
yield ['short', 'short.id:1:1', true];
yield ['short.id', 'short.id:1:1', true];
yield ['short.notId', 'short.id:1:1', false];
yield ['short.id', 'short', false];
yield ['SHORT.ID', 'short.id:1:1', true];
yield ['short.id:2:1', 'short.id:1:1', false];
yield ['short.id:1:2', 'short.id:1:1', false];
yield ['short::1', 'short.id:1:1', true];
yield ['short.id::1', 'short.id:1:1', true];
}
}
Loading