From 7c17ca3655ae1ffe6b5edae6cbf6cef23e8bc152 Mon Sep 17 00:00:00 2001 From: AlessandroMinoccheri Date: Fri, 5 Jun 2026 15:11:47 +0200 Subject: [PATCH] ignored line numbers in the baseline --- src/CLI/Baseline.php | 6 ++++- src/CLI/Command/Check.php | 2 +- src/Rules/Violation.php | 5 ++++ src/Rules/Violations.php | 10 ++++++++ tests/Unit/Rules/ViolationsTest.php | 36 +++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/CLI/Baseline.php b/src/CLI/Baseline.php index 0cc82d22..d6f31971 100644 --- a/src/CLI/Baseline.php +++ b/src/CLI/Baseline.php @@ -65,12 +65,16 @@ public static function loadFromFile(string $filename): self ); } - public static function save(?string $filename, string $defaultFilePath, Violations $violations): string + public static function save(?string $filename, string $defaultFilePath, Violations $violations, bool $ignoreLineNumbers = false): string { if (null === $filename) { $filename = $defaultFilePath; } + if ($ignoreLineNumbers) { + $violations = $violations->withoutLineNumbers(); + } + file_put_contents($filename, json_encode($violations, \JSON_PRETTY_PRINT)); return $filename; diff --git a/src/CLI/Command/Check.php b/src/CLI/Command/Check.php index 836c9f1f..e03f3eda 100644 --- a/src/CLI/Command/Check.php +++ b/src/CLI/Command/Check.php @@ -164,7 +164,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int if (false !== $generateBaseline) { $result = $runner->baseline($config, $progress); - $baselineFilePath = Baseline::save($generateBaseline, self::DEFAULT_BASELINE_FILENAME, $result->getViolations()); + $baselineFilePath = Baseline::save($generateBaseline, self::DEFAULT_BASELINE_FILENAME, $result->getViolations(), $ignoreBaselineLinenumbers); $output->writeln("ℹ️ Baseline file '$baselineFilePath' created!"); diff --git a/src/Rules/Violation.php b/src/Rules/Violation.php index 6521b497..41cb75c6 100644 --- a/src/Rules/Violation.php +++ b/src/Rules/Violation.php @@ -61,4 +61,9 @@ public static function fromJson(array $json): self { return new self($json['fqcn'], $json['error'], $json['line'], $json['filePath'] ?? null); } + + public function withoutLineNumber(): self + { + return new self($this->fqcn, $this->error, null, $this->filePath); + } } diff --git a/src/Rules/Violations.php b/src/Rules/Violations.php index 47fe5f08..8b454274 100644 --- a/src/Rules/Violations.php +++ b/src/Rules/Violations.php @@ -109,6 +109,16 @@ public function remove(self $violations, bool $ignoreBaselineLinenumbers = false $this->violations = array_values($this->violations); } + public function withoutLineNumbers(): self + { + $copy = new self(); + foreach ($this->violations as $violation) { + $copy->add($violation->withoutLineNumber()); + } + + return $copy; + } + public function sort(): void { usort($this->violations, static fn (Violation $v1, Violation $v2): int => $v1 <=> $v2); diff --git a/tests/Unit/Rules/ViolationsTest.php b/tests/Unit/Rules/ViolationsTest.php index b5eaf755..f2ba4c8e 100644 --- a/tests/Unit/Rules/ViolationsTest.php +++ b/tests/Unit/Rules/ViolationsTest.php @@ -237,6 +237,42 @@ public function test_remove_violations_still_works_for_self_explanatory_messages self::assertCount(0, $violations); } + public function test_violation_without_line_number_returns_copy_with_null_line(): void + { + $violation = new Violation('App\Foo', 'some error', 42, '/src/Foo.php'); + $stripped = $violation->withoutLineNumber(); + + self::assertNull($stripped->getLine()); + self::assertEquals('App\Foo', $stripped->getFqcn()); + self::assertEquals('some error', $stripped->getError()); + self::assertEquals('/src/Foo.php', $stripped->getFilePath()); + } + + public function test_without_line_numbers_strips_all_line_numbers(): void + { + $violations = new Violations(); + $violations->add(new Violation('App\Foo', 'some error', 42, '/src/Foo.php')); + $violations->add(new Violation('App\Bar', 'other error', 10, '/src/Bar.php')); + + $stripped = $violations->withoutLineNumbers(); + + self::assertCount(2, $stripped); + self::assertNull($stripped->get(0)->getLine()); + self::assertNull($stripped->get(1)->getLine()); + self::assertEquals('App\Foo', $stripped->get(0)->getFqcn()); + self::assertEquals('App\Bar', $stripped->get(1)->getFqcn()); + } + + public function test_without_line_numbers_does_not_mutate_original(): void + { + $violations = new Violations(); + $violations->add(new Violation('App\Foo', 'some error', 42, '/src/Foo.php')); + + $violations->withoutLineNumbers(); + + self::assertEquals(42, $violations->get(0)->getLine()); + } + public function test_remove_violations_from_violations_ignore_linenumber(): void { $violation1 = new Violation(