Skip to content

Commit

Permalink
Remove token name feature
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 10, 2024
1 parent 64bd0ac commit 7d6a135
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 156 deletions.
6 changes: 1 addition & 5 deletions src/Report/ViolationId.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ final class ViolationId
public function __construct(
private ?string $ruleShortName = null,
private ?string $identifier = null,
private ?string $tokenName = null,
private ?int $line = null,
private ?int $linePosition = null,
) {
Expand All @@ -27,7 +26,6 @@ public static function fromString(string $string, ?int $line = null): self
return new self(
$explodedName[0] ?? null,
$explodedName[1] ?? null,
$explodedName[2] ?? null,
$line,
$position
);
Expand All @@ -36,10 +34,9 @@ public static function fromString(string $string, ?int $line = null): self
public function toString(): string
{
$name = rtrim(sprintf(
'%s.%s.%s',
'%s.%s',
$this->ruleShortName ?? '',
$this->identifier ?? '',
$this->tokenName ?? ''
), '.');

return rtrim(sprintf(
Expand All @@ -54,7 +51,6 @@ public function match(self $violationId): bool
{
return $this->matchValue($this->ruleShortName, $violationId->ruleShortName)
&& $this->matchValue($this->identifier, $violationId->identifier)
&& $this->matchValue($this->tokenName, $violationId->tokenName)
&& $this->matchValue($this->line, $violationId->line)
&& $this->matchValue($this->linePosition, $violationId->linePosition);
}
Expand Down
1 change: 0 additions & 1 deletion src/Rules/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ private function addMessage(int $messageType, string $message, Token $token, ?st
$id = new ViolationId(
$this->getShortName(),
$messageId ?? ucfirst(strtolower(Violation::getLevelAsString($messageType))),
$token->getName(),
$token->getLine(),
$token->getPosition(),
);
Expand Down
15 changes: 0 additions & 15 deletions src/Token/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace TwigCsFixer\Token;

use ReflectionClass;

/**
* Override of Twig's Token to add new constants.
*/
Expand Down Expand Up @@ -79,19 +77,6 @@ public function getType(): int|string
return $this->type;
}

public function getName(): string
{
$constants = (new ReflectionClass($this))->getConstants();
$constantName = array_search($this->type, $constants, true);
if (false !== $constantName) {
$name = str_replace('_', '', ucwords(strtolower($constantName), '_'));

return str_ends_with($name, 'Type') ? substr($name, 0, -4) : $name;
}

return (string) $this->getType();
}

public function getLine(): int
{
return $this->line;
Expand Down
39 changes: 17 additions & 22 deletions tests/Report/ViolationIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ class ViolationIdTest extends TestCase
public function testToString(
?string $ruleShortName,
?string $identifier,
?string $tokenName,
?int $line,
?int $linePosition,
string $expected,
): void {
$violationId = new ViolationId(
$ruleShortName,
$identifier,
$tokenName,
$line,
$linePosition
);
Expand All @@ -35,19 +33,17 @@ public function testToString(
}

/**
* @return iterable<array-key, array{string|null, string|null, string|null, int|null, int|null, string}>
* @return iterable<array-key, array{string|null, string|null, int|null, int|null, string}>
*/
public static function toStringDataProvider(): iterable
{
yield [null, null, null, null, null, ''];
yield ['short', null, null, null, null, 'short'];
yield ['short', 'id', null, null, null, 'short.id'];
yield ['short', null, 'token', null, null, 'short..token'];
yield ['short', null, null, 1, null, 'short:1'];
yield ['short', null, null, null, 1, 'short::1'];
yield ['short', 'id', 'token', null, null, 'short.id.token'];
yield ['short', 'id', 'token', 1, null, 'short.id.token:1'];
yield ['short', 'id', 'token', 1, 1, 'short.id.token:1:1'];
yield [null, null, null, null, ''];
yield ['short', null, null, null, 'short'];
yield ['short', 'id', null, null, 'short.id'];
yield ['short', null, 1, null, 'short:1'];
yield ['short', null, null, 1, 'short::1'];
yield ['short', 'id', 1, null, 'short.id:1'];
yield ['short', 'id', 1, 1, 'short.id:1:1'];
}

/**
Expand All @@ -66,17 +62,16 @@ public function testMatch(string $string1, string $string2, bool $expected): voi
public static function matchDataProvider(): iterable
{
yield ['', 'short', true];
yield ['', 'short.id.token:1:1', true];
yield ['', 'short.id:1:1', true];
yield ['short', 'short', true];
yield ['short', 'short.id.token:1:1', true];
yield ['short.id', 'short.id.token:1:1', true];
yield ['short.notId', 'short.id.token:1:1', false];
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.TOKEN', 'short.id.token:1:1', true];
yield ['short.id.token:2:1', 'short.id.token:1:1', false];
yield ['short.id.token:1:2', 'short.id.token:1:1', false];
yield ['short::1', 'short.id.token:1:1', true];
yield ['short.id::1', 'short.id.token:1:1', true];
yield ['short.id.token::1', 'short.id.token:1:1', true];
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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ final class BlockNameSpacingTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new BlockNameSpacingRule(), [
'BlockNameSpacing.After.BlockName:1:5',
'BlockNameSpacing.Before.BlockName:1:5',
'BlockNameSpacing.After.BlockName:3:3',
'BlockNameSpacing.Before.BlockName:3:3',
'BlockNameSpacing.After:1:5',
'BlockNameSpacing.Before:1:5',
'BlockNameSpacing.After:3:3',
'BlockNameSpacing.Before:3:3',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ final class DelimiterSpacingTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new DelimiterSpacingRule(), [
'DelimiterSpacing.After.BlockStart:15:1',
'DelimiterSpacing.Before.BlockEnd:15:12',
'DelimiterSpacing.After.BlockStart:15:15',
'DelimiterSpacing.Before.BlockEnd:15:25',
'DelimiterSpacing.After:15:1',
'DelimiterSpacing.Before:15:12',
'DelimiterSpacing.After:15:15',
'DelimiterSpacing.Before:15:25',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ final class OperatorNameSpacingTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new OperatorNameSpacingRule(), [
'OperatorNameSpacing.Error.Operator:2:13',
'OperatorNameSpacing.Error.Operator:3:13',
'OperatorNameSpacing.Error.Operator:4:10',
'OperatorNameSpacing.Error:2:13',
'OperatorNameSpacing.Error:3:13',
'OperatorNameSpacing.Error:4:10',
]);
}
}
88 changes: 44 additions & 44 deletions tests/Rules/Operator/OperatorSpacing/OperatorSpacingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,50 @@ final class OperatorSpacingTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new OperatorSpacingRule(), [
'OperatorSpacing.After.Operator:1:5',
'OperatorSpacing.Before.Operator:1:5',
'OperatorSpacing.After.Operator:2:5',
'OperatorSpacing.Before.Operator:2:5',
'OperatorSpacing.After.Operator:3:5',
'OperatorSpacing.Before.Operator:3:5',
'OperatorSpacing.After.Operator:4:5',
'OperatorSpacing.Before.Operator:4:5',
'OperatorSpacing.After.Operator:5:5',
'OperatorSpacing.Before.Operator:5:5',
'OperatorSpacing.After.Operator:6:5',
'OperatorSpacing.Before.Operator:6:5',
'OperatorSpacing.After.Operator:7:5',
'OperatorSpacing.Before.Operator:7:5',
'OperatorSpacing.After.Operator:8:7',
'OperatorSpacing.Before.Operator:8:7',
'OperatorSpacing.After.Operator:9:10',
'OperatorSpacing.Before.Operator:9:10',
'OperatorSpacing.After.Operator:9:19',
'OperatorSpacing.Before.Operator:9:19',
'OperatorSpacing.After.Operator:10:5',
'OperatorSpacing.Before.Operator:10:5',
'OperatorSpacing.After.Operator:11:4',
'OperatorSpacing.After.Operator:12:11',
'OperatorSpacing.Before.Operator:12:11',
'OperatorSpacing.After.Operator:13:11',
'OperatorSpacing.Before.Operator:13:11',
'OperatorSpacing.After.Operator:14:7',
'OperatorSpacing.Before.Operator:14:7',
'OperatorSpacing.After.Operator:15:7',
'OperatorSpacing.Before.Operator:15:7',
'OperatorSpacing.After.Operator:19:5',
'OperatorSpacing.Before.Operator:19:5',
'OperatorSpacing.After.Operator:20:5',
'OperatorSpacing.Before.Operator:20:5',
'OperatorSpacing.After.Operator:22:6',
'OperatorSpacing.After.Operator:33:10',
'OperatorSpacing.Before.Operator:33:10',
'OperatorSpacing.After.Operator:35:13',
'OperatorSpacing.Before.Operator:35:13',
'OperatorSpacing.After.Operator:36:13',
'OperatorSpacing.Before.Operator:36:13',
'OperatorSpacing.After.Operator:37:13',
'OperatorSpacing.Before.Operator:37:13',
'OperatorSpacing.After:1:5',
'OperatorSpacing.Before:1:5',
'OperatorSpacing.After:2:5',
'OperatorSpacing.Before:2:5',
'OperatorSpacing.After:3:5',
'OperatorSpacing.Before:3:5',
'OperatorSpacing.After:4:5',
'OperatorSpacing.Before:4:5',
'OperatorSpacing.After:5:5',
'OperatorSpacing.Before:5:5',
'OperatorSpacing.After:6:5',
'OperatorSpacing.Before:6:5',
'OperatorSpacing.After:7:5',
'OperatorSpacing.Before:7:5',
'OperatorSpacing.After:8:7',
'OperatorSpacing.Before:8:7',
'OperatorSpacing.After:9:10',
'OperatorSpacing.Before:9:10',
'OperatorSpacing.After:9:19',
'OperatorSpacing.Before:9:19',
'OperatorSpacing.After:10:5',
'OperatorSpacing.Before:10:5',
'OperatorSpacing.After:11:4',
'OperatorSpacing.After:12:11',
'OperatorSpacing.Before:12:11',
'OperatorSpacing.After:13:11',
'OperatorSpacing.Before:13:11',
'OperatorSpacing.After:14:7',
'OperatorSpacing.Before:14:7',
'OperatorSpacing.After:15:7',
'OperatorSpacing.Before:15:7',
'OperatorSpacing.After:19:5',
'OperatorSpacing.Before:19:5',
'OperatorSpacing.After:20:5',
'OperatorSpacing.Before:20:5',
'OperatorSpacing.After:22:6',
'OperatorSpacing.After:33:10',
'OperatorSpacing.Before:33:10',
'OperatorSpacing.After:35:13',
'OperatorSpacing.Before:35:13',
'OperatorSpacing.After:36:13',
'OperatorSpacing.Before:36:13',
'OperatorSpacing.After:37:13',
'OperatorSpacing.Before:37:13',
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ final class PunctuationSpacingTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new PunctuationSpacingRule(), [
'PunctuationSpacing.After.Punctuation:3:4',
'PunctuationSpacing.Before.Punctuation:3:10',
'PunctuationSpacing.After.Punctuation:4:4',
'PunctuationSpacing.Before.Punctuation:4:10',
'PunctuationSpacing.Before.Punctuation:4:16',
'PunctuationSpacing.Before.Punctuation:4:22',
'PunctuationSpacing.Before.Punctuation:4:28',
'PunctuationSpacing.After.Punctuation:5:12',
'PunctuationSpacing.Before.Punctuation:5:16',
'PunctuationSpacing.Before.Punctuation:5:20',
'PunctuationSpacing.Before.Punctuation:5:24',
'PunctuationSpacing.After.Punctuation:6:6',
'PunctuationSpacing.Before.Punctuation:6:6',
'PunctuationSpacing.Before.Punctuation:7:12',
'PunctuationSpacing.Before.Punctuation:7:15',
'PunctuationSpacing.After:3:4',
'PunctuationSpacing.Before:3:10',
'PunctuationSpacing.After:4:4',
'PunctuationSpacing.Before:4:10',
'PunctuationSpacing.Before:4:16',
'PunctuationSpacing.Before:4:22',
'PunctuationSpacing.Before:4:28',
'PunctuationSpacing.After:5:12',
'PunctuationSpacing.Before:5:16',
'PunctuationSpacing.Before:5:20',
'PunctuationSpacing.Before:5:24',
'PunctuationSpacing.After:6:6',
'PunctuationSpacing.Before:6:6',
'PunctuationSpacing.Before:7:12',
'PunctuationSpacing.Before:7:15',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ final class TrailingCommaSingleLineTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new TrailingCommaSingleLineRule(), [
'TrailingCommaSingleLine.Error.Punctuation:2:9',
'TrailingCommaSingleLine.Error.Punctuation:4:13',
'TrailingCommaSingleLine.Error.Punctuation:6:12',
'TrailingCommaSingleLine.Error:2:9',
'TrailingCommaSingleLine.Error:4:13',
'TrailingCommaSingleLine.Error:6:12',
]);
}
}
6 changes: 3 additions & 3 deletions tests/Rules/Whitespace/BlankEOF/BlankEOFTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ final class BlankEOFTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new BlankEOFRule(), [
'BlankEOF.Error.Eof:4:1',
'BlankEOF.Error:4:1',
]);

$this->checkRule(new BlankEOFRule(), [
'BlankEOF.Error.Eof:2:7',
'BlankEOF.Error:2:7',
], __DIR__.'/BlankEOFTest2.twig');
}

Expand All @@ -25,7 +25,7 @@ public function testRuleForEmptyFile(): void
$this->checkRule(new BlankEOFRule(), [], __DIR__.'/BlankEOFTest.empty.twig');

$this->checkRule(new BlankEOFRule(), [
'BlankEOF.Error.Eof:3:1',
'BlankEOF.Error:3:1',
], __DIR__.'/BlankEOFTest.empty2.twig');
}
}
6 changes: 3 additions & 3 deletions tests/Rules/Whitespace/EmptyLines/EmptyLinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ final class EmptyLinesTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new EmptyLinesRule(), [
'EmptyLines.Error.Eol:2:1',
'EmptyLines.Error.Eol:5:1',
'EmptyLines.Error.Eol:10:1',
'EmptyLines.Error:2:1',
'EmptyLines.Error:5:1',
'EmptyLines.Error:10:1',
]);
}
}
8 changes: 4 additions & 4 deletions tests/Rules/Whitespace/Indent/IndentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function testConfiguration(): void
public function testRule(): void
{
$this->checkRule(new IndentRule(), [
'Indent.Error.CommentTab:2:1',
'Indent.Error.Tab:4:1',
'Indent.Error:2:1',
'Indent.Error:4:1',
]);
}

Expand All @@ -28,8 +28,8 @@ public function testRuleWithSpaceRatio(): void
$this->checkRule(
new IndentRule(2),
[
'Indent.Error.CommentTab:2:1',
'Indent.Error.Tab:4:1',
'Indent.Error:2:1',
'Indent.Error:4:1',
],
__DIR__.'/IndentTest.twig',
__DIR__.'/IndentTest.fixed2.twig',
Expand Down
Loading

0 comments on commit 7d6a135

Please sign in to comment.