diff --git a/src/Runner/SniffRunner.php b/src/Runner/SniffRunner.php index 4059d76..15e7e9a 100644 --- a/src/Runner/SniffRunner.php +++ b/src/Runner/SniffRunner.php @@ -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, diff --git a/src/Runner/XmlFileProcessor.php b/src/Runner/XmlFileProcessor.php index 549156e..39b9d05 100644 --- a/src/Runner/XmlFileProcessor.php +++ b/src/Runner/XmlFileProcessor.php @@ -30,8 +30,8 @@ public function __construct( $this->report = $report ?? new Report(); } - /** @param list $changedLines */ - public function processFile(string $filePath, array $changedLines = [], string $reportPath = ''): FileReport + /** @param list|null $changedLines */ + public function processFile(string $filePath, ?array $changedLines = null, string $reportPath = ''): FileReport { $effectivePath = $reportPath !== '' ? $reportPath : $filePath; $fileReport = new FileReport($effectivePath); @@ -51,23 +51,23 @@ public function processFile(string $filePath, array $changedLines = [], string $ return $this->processContent($content, $effectivePath, $fileReport, $changedLines); } - /** @param list $changedLines */ + /** @param list|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 $changedLines */ + /** @param list|null $changedLines */ private function processContent( string $content, string $filePath, FileReport $fileReport, - array $changedLines = [], + ?array $changedLines = null, ): FileReport { $content = $this->preprocessor->process($content); @@ -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); } diff --git a/tests/Unit/Runner/SniffRunnerTest.php b/tests/Unit/Runner/SniffRunnerTest.php index 971803b..f6801a3 100644 --- a/tests/Unit/Runner/SniffRunnerTest.php +++ b/tests/Unit/Runner/SniffRunnerTest.php @@ -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()); + } } diff --git a/tests/Unit/Runner/XmlFileProcessorTest.php b/tests/Unit/Runner/XmlFileProcessorTest.php index 7633e1a..6bd60b4 100644 --- a/tests/Unit/Runner/XmlFileProcessorTest.php +++ b/tests/Unit/Runner/XmlFileProcessorTest.php @@ -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( + ' + 3 + 4 + 5 + ' + ); + + $report = $this->processor([$sniff])->processString($xml, 'f.xml', []); + + self::assertSame(0, $report->getViolationCount()); + } + /** @param list $lines */ private function sniff(array $lines): SniffInterface {