Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/Runner/SniffRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function run(ConfigData $config, ?array $overridePaths = null, ?array $di

$changedLines = $diffLines !== null
? $this->getChangedLinesForFile($file, $diffLines)
: [];
: null;

$fileReport = $processor->processFile(
$file,
Expand Down
14 changes: 7 additions & 7 deletions src/Runner/XmlFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function __construct(
$this->report = $report ?? new Report();
}

/** @param list<int> $changedLines */
public function processFile(string $filePath, array $changedLines = [], string $reportPath = ''): FileReport
/** @param list<int>|null $changedLines */
public function processFile(string $filePath, ?array $changedLines = null, string $reportPath = ''): FileReport
{
$effectivePath = $reportPath !== '' ? $reportPath : $filePath;
$fileReport = new FileReport($effectivePath);
Expand All @@ -51,23 +51,23 @@ public function processFile(string $filePath, array $changedLines = [], string $
return $this->processContent($content, $effectivePath, $fileReport, $changedLines);
}

/** @param list<int> $changedLines */
/** @param list<int>|null $changedLines */
public function processString(
string $xmlContent,
string $pseudoPath = 'input.xml',
array $changedLines = [],
?array $changedLines = null,
): FileReport {
$fileReport = new FileReport($pseudoPath);

return $this->processContent($xmlContent, $pseudoPath, $fileReport, $changedLines);
}

/** @param list<int> $changedLines */
/** @param list<int>|null $changedLines */
private function processContent(
string $content,
string $filePath,
FileReport $fileReport,
array $changedLines = [],
?array $changedLines = null,
): FileReport {
$content = $this->preprocessor->process($content);

Expand All @@ -87,7 +87,7 @@ private function processContent(
$this->report->addSniffTime($sniff->getCode(), microtime(true) - $start);
}

if ($changedLines !== []) {
if ($changedLines !== null) {
$violations = $this->filterRelevantViolations($violations, $document, $changedLines);
}

Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/Runner/SniffRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,41 @@ public function itScansAllFilesWhenNoDiffIsGiven(): void

self::assertSame(2, $report->getFilesScanned());
}

#[Test]
public function itReportsNoViolationsForFilesInDiffWithoutAddedLines(): void
{
$sniff = new class implements SniffInterface {
public function getCode(): string
{
return 'Test.ViolatingSniff';
}

public function process(\DOMDocument $document, string $content, string $filePath): array
{
return [
new Violation(
sniffCode: 'Test.ViolatingSniff',
filePath: $filePath,
line: 1,
message: 'Test violation',
severity: Severity::WARNING,
),
];
}

public function setProperty(string $name, string $value): void
{
}
};

$config = $this->createConfig(sniffs: [new SniffEntry($sniff::class)]);
$runner = new SniffRunner();

$diffLines = ['sniff_runner/default/file_a.xml' => []];
$report = $runner->run($config, null, $diffLines);

self::assertSame(1, $report->getFilesScanned());
self::assertFalse($report->hasViolations());
}
}
18 changes: 18 additions & 0 deletions tests/Unit/Runner/XmlFileProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,24 @@ public function itKeepsInternalErrorsEvenWithDiffFiltering(): void
self::assertSame('DocbookCS.Internal', $report->getViolations()[0]->sniffCode);
}

#[Test]
public function itReportsNoViolationsInDiffModeWhenNoLinesWereAdded(): void
{
$sniff = $this->sniff([3, 5]);

$xml = $this->xml(
'<chapter>
<simpara>3</simpara>
<simpara>4</simpara>
<simpara>5</simpara>
</chapter>'
);

$report = $this->processor([$sniff])->processString($xml, 'f.xml', []);

self::assertSame(0, $report->getViolationCount());
}

/** @param list<int> $lines */
private function sniff(array $lines): SniffInterface
{
Expand Down