From 01be0b57a5f8997017fecfd37fbc05381d71a024 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 9 Jan 2024 23:46:50 +0100 Subject: [PATCH] Allow to disable every rules --- src/Report/ViolationId.php | 8 ++++---- src/Token/Tokenizer.php | 19 ++++++++++++++----- tests/Report/ViolationIdTest.php | 7 +++++-- tests/Rules/Fixtures/disable2.twig | 1 + tests/Rules/RuleTest.php | 2 +- .../Fixtures/ignored_violations.twig | 1 + tests/Token/Tokenizer/TokenizerTest.php | 1 + 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/Report/ViolationId.php b/src/Report/ViolationId.php index 7566bc2a..9e2c0ab7 100644 --- a/src/Report/ViolationId.php +++ b/src/Report/ViolationId.php @@ -7,7 +7,7 @@ final class ViolationId { public function __construct( - private string $ruleShortName, + private ?string $ruleShortName = null, private ?string $identifier = null, private ?string $tokenName = null, private ?int $line = null, @@ -19,13 +19,13 @@ public static function fromString(string $string, ?int $line = null): self { $exploded = explode(':', $string); $name = $exploded[0]; - $explodedName = explode('.', $name); + $explodedName = '' !== $name ? explode('.', $name) : null; $line ??= isset($exploded[1]) && '' !== $exploded[1] ? (int) $exploded[1] : null; $position = isset($exploded[2]) && '' !== $exploded[2] ? (int) $exploded[2] : null; return new self( - $explodedName[0], + $explodedName[0] ?? null, $explodedName[1] ?? null, $explodedName[2] ?? null, $line, @@ -37,7 +37,7 @@ public function toString(): string { $name = rtrim(sprintf( '%s.%s.%s', - $this->ruleShortName, + $this->ruleShortName ?? '', $this->identifier ?? '', $this->tokenName ?? '' ), '.'); diff --git a/src/Token/Tokenizer.php b/src/Token/Tokenizer.php index 1fd872d9..4e0d2217 100644 --- a/src/Token/Tokenizer.php +++ b/src/Token/Tokenizer.php @@ -216,14 +216,14 @@ private function pushState(int $state, array $data = []): void * * @see https://github.com/vimeo/psalm/issues/8989 */ - private function setStateParam(string $name, int|string $value): void + private function setStateParam(string $name, int|string|bool $value): void { Assert::notEmpty($this->state, 'Cannot set state params without a current state.'); $this->state[\count($this->state) - 1][1][$name] = $value; } - private function getStateParam(string $name): int|string|null + private function getStateParam(string $name): int|string|bool|null { Assert::notEmpty($this->state, 'Cannot get state params without a current state.'); @@ -689,24 +689,33 @@ private function getOperatorRegex(Environment $env): string private function extractIgnoredViolations(string $comment): void { $comment = trim($comment); - if (1 === preg_match('/^twig-cs-fixer-disable(|-line|-next-line) ([\s\w,.:]+)/i', $comment, $match)) { + if (1 === preg_match('/^twig-cs-fixer-disable(|-line|-next-line)\s+([\s\w,.:]*)/i', $comment, $match)) { $this->setStateParam('ignoredViolations', preg_replace('/\s+/', ',', $match[2]) ?? ''); $this->setStateParam('ignoredType', trim($match[1], '-')); } else { - $this->setStateParam('ignoredViolations', ''); + $this->setStateParam('ignoredViolations', false); } } private function processIgnoredViolations(): void { $ignoredViolations = $this->getStateParam('ignoredViolations'); + if (!\is_string($ignoredViolations)) { + return; + } $line = match ($this->getStateParam('ignoredType')) { 'line' => (int) $this->getStateParam('startLine'), 'next-line' => $this->line + 1, default => null, }; - $ignoredViolationsExploded = explode(',', (string) $ignoredViolations); + if ('' === $ignoredViolations) { + $this->ignoredViolations[] = ViolationId::fromString($ignoredViolations, $line); + + return; + } + + $ignoredViolationsExploded = explode(',', $ignoredViolations); foreach ($ignoredViolationsExploded as $ignoredViolation) { if ('' === $ignoredViolation) { continue; diff --git a/tests/Report/ViolationIdTest.php b/tests/Report/ViolationIdTest.php index 62a3e557..7167d7a7 100644 --- a/tests/Report/ViolationIdTest.php +++ b/tests/Report/ViolationIdTest.php @@ -13,7 +13,7 @@ class ViolationIdTest extends TestCase * @dataProvider toStringDataProvider */ public function testToString( - string $ruleShortName, + ?string $ruleShortName, ?string $identifier, ?string $tokenName, ?int $line, @@ -35,10 +35,11 @@ public function testToString( } /** - * @return iterable + * @return iterable */ 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']; @@ -64,6 +65,8 @@ 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', 'short', true]; yield ['short', 'short.id.token:1:1', true]; yield ['short.id', 'short.id.token:1:1', true]; diff --git a/tests/Rules/Fixtures/disable2.twig b/tests/Rules/Fixtures/disable2.twig index b2a17b79..a6de9f35 100644 --- a/tests/Rules/Fixtures/disable2.twig +++ b/tests/Rules/Fixtures/disable2.twig @@ -6,6 +6,7 @@ twig-cs-fixer-disable-line Fake.Error #} This comment disable for the line with "{ #" => ERROR ON THIS LINE {# twig-cs-fixer-disable-line Fake.Error::1 #} This comment disable for the first token of the line. {# twig-cs-fixer-disable-line Fake.Error::2 #} This comment disable for the second token of the line. => ERROR ON THIS LINE +{# twig-cs-fixer-disable-line #} This comment disable for the line for every rule {# twig-cs-fixer-disable-next-line Fake.Error #} This comment disable for the first token of the next-line. => ERROR ON THIS LINE {# twig-cs-fixer-disable-next-line Fake.Error #} This comment disable for the next line. => ERROR ON THIS LINE diff --git a/tests/Rules/RuleTest.php b/tests/Rules/RuleTest.php index 0f28a304..3c3250ec 100644 --- a/tests/Rules/RuleTest.php +++ b/tests/Rules/RuleTest.php @@ -152,7 +152,7 @@ public static function ignoredViolationsDataProvider(): iterable ]; yield [ __DIR__.'/Fixtures/disable2.twig', - [3, 6, 8, 9, 11], + [3, 6, 8, 10, 12], ]; } } diff --git a/tests/Token/Tokenizer/Fixtures/ignored_violations.twig b/tests/Token/Tokenizer/Fixtures/ignored_violations.twig index cae3d675..76eba371 100644 --- a/tests/Token/Tokenizer/Fixtures/ignored_violations.twig +++ b/tests/Token/Tokenizer/Fixtures/ignored_violations.twig @@ -2,3 +2,4 @@ {# Twig-CS-Fixer-disable Foo.Bar.BazInsensitive #} {# twig-cs-fixer-disable-line Foo.Bar #} {# twig-cs-fixer-disable-next-line Foo.Bar Bar.Foo #} +{# twig-cs-fixer-disable-next-line #} diff --git a/tests/Token/Tokenizer/TokenizerTest.php b/tests/Token/Tokenizer/TokenizerTest.php index 90545789..5fcf7461 100644 --- a/tests/Token/Tokenizer/TokenizerTest.php +++ b/tests/Token/Tokenizer/TokenizerTest.php @@ -89,6 +89,7 @@ public function testTokenizeIgnoredViolations(): void 'Foo.Bar:3', 'Foo.Bar:5', 'Bar.Foo:5', + ':6', ], array_map( static fn (ViolationId $validationId) => $validationId->toString(),