Skip to content

Commit

Permalink
Add sniff name in sniff violation
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Nov 22, 2023
1 parent 78d1f61 commit 685a263
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
16 changes: 7 additions & 9 deletions src/Report/SniffViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand Down Expand Up @@ -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;
}
}
10 changes: 8 additions & 2 deletions src/Sniff/AbstractSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions tests/Report/SniffViolationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Sniff/Fixtures/FakeSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Tests\Sniff\Fixtures;

use TwigCsFixer\Sniff\AbstractSniff;

class FakeSniff extends AbstractSniff
{
public function process(int $tokenPosition, array $tokens): void
{
}
}
7 changes: 7 additions & 0 deletions tests/Sniff/SniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SplFileInfo;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Sniff\AbstractSniff;
use TwigCsFixer\Tests\Sniff\Fixtures\FakeSniff;
use TwigCsFixer\Token\Token;

final class SniffTest extends TestCase
Expand Down Expand Up @@ -36,6 +37,12 @@ protected function process(int $tokenPosition, array $tokens): void
static::assertSame(2, $report->getTotalErrors());
}

public function testSniffName(): void
{
$sniff = new FakeSniff();
static::assertSame(FakeSniff::class, $sniff->getName());
}

public function testSniffWithReport2(): void
{
$report = new Report([new SplFileInfo('fakeFile.html.twig')]);
Expand Down

0 comments on commit 685a263

Please sign in to comment.