Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/CLI/Baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/CLI/Command/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!");

Expand Down
5 changes: 5 additions & 0 deletions src/Rules/Violation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 10 additions & 0 deletions src/Rules/Violations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Rules/ViolationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down