diff --git a/src/Report/SniffViolation.php b/src/Report/SniffViolation.php index 66d4fa5c..fedc705c 100644 --- a/src/Report/SniffViolation.php +++ b/src/Report/SniffViolation.php @@ -16,13 +16,13 @@ final class SniffViolation public const LEVEL_ERROR = 2; public const LEVEL_FATAL = 3; - private ?int $linePosition = null; - public function __construct( private int $level, private string $message, private string $filename, - private ?int $line = null + private ?int $line = null, + private ?int $linePosition = null, + private ?string $sniffName = null ) { } @@ -72,15 +72,13 @@ public function getFilename(): string return $this->filename; } - public function setLinePosition(?int $linePosition): self + public function getLinePosition(): ?int { - $this->linePosition = $linePosition; - - return $this; + return $this->linePosition; } - public function getLinePosition(): ?int + public function getSniffName(): ?string { - return $this->linePosition; + return $this->sniffName; } } diff --git a/src/Sniff/AbstractSniff.php b/src/Sniff/AbstractSniff.php index 75f41689..4de6a80d 100644 --- a/src/Sniff/AbstractSniff.php +++ b/src/Sniff/AbstractSniff.php @@ -15,6 +15,11 @@ abstract class AbstractSniff implements SniffInterface private ?FixerInterface $fixer = null; + public function getName(): string + { + return static::class; + } + public function lintFile(array $stream, Report $report): void { $this->report = $report; @@ -133,9 +138,10 @@ private function addMessage(int $messageType, string $message, Token $token): vo $messageType, $message, $token->getFilename(), - $token->getLine() + $token->getLine(), + $token->getPosition(), + $this->getName(), ); - $sniffViolation->setLinePosition($token->getPosition()); $report->addMessage($sniffViolation); } diff --git a/tests/Report/SniffViolationTest.php b/tests/Report/SniffViolationTest.php index ea25c61a..52d81d35 100644 --- a/tests/Report/SniffViolationTest.php +++ b/tests/Report/SniffViolationTest.php @@ -11,14 +11,13 @@ final class SniffViolationTest extends TestCase { public function testGetters(): void { - $sniffViolation = new SniffViolation(SniffViolation::LEVEL_WARNING, 'message', 'filename', 42); + $sniffViolation = new SniffViolation(SniffViolation::LEVEL_WARNING, 'message', 'filename', 42, 33, 'name'); static::assertSame(SniffViolation::LEVEL_WARNING, $sniffViolation->getLevel()); static::assertSame('message', $sniffViolation->getMessage()); static::assertSame('filename', $sniffViolation->getFilename()); static::assertSame(42, $sniffViolation->getLine()); - - $sniffViolation->setLinePosition(33); static::assertSame(33, $sniffViolation->getLinePosition()); + static::assertSame('name', $sniffViolation->getSniffName()); } /** diff --git a/tests/Sniff/Fixtures/FakeSniff.php b/tests/Sniff/Fixtures/FakeSniff.php new file mode 100644 index 00000000..97ff1e02 --- /dev/null +++ b/tests/Sniff/Fixtures/FakeSniff.php @@ -0,0 +1,12 @@ +getTotalErrors()); } + public function testSniffName(): void + { + $sniff = new FakeSniff(); + static::assertSame('TwigCsFixer\Tests\Sniff\Fixtures\FakeSniff', $sniff->getName()); + } + public function testSniffWithReport2(): void { $report = new Report([new SplFileInfo('fakeFile.html.twig')]);