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
23 changes: 23 additions & 0 deletions src/RelativePath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace DocbookCS;

final class RelativePath
{
public static function fromWorkingDirectory(string $filePath): string
{
$workingDirectory = getcwd();
if ($workingDirectory === false) {
return $filePath; // @codeCoverageIgnore
}

$prefix = rtrim(str_replace('\\', '/', $workingDirectory), '/') . '/';
$normalisedPath = str_replace('\\', '/', $filePath);

return str_starts_with($normalisedPath, $prefix)
? substr($normalisedPath, strlen($prefix))
: $filePath;
}
}
3 changes: 2 additions & 1 deletion src/Report/Reporter/CheckstyleReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DocbookCS\Report\Reporter;

use DocbookCS\RelativePath;
use DocbookCS\Report\Report;

final class CheckstyleReporter implements ReporterInterface
Expand All @@ -28,7 +29,7 @@ public function generate(Report $report): string
}

$fileNode = $dom->createElement('file');
$fileNode->setAttribute('name', $fileReport->filePath);
$fileNode->setAttribute('name', RelativePath::fromWorkingDirectory($fileReport->filePath));

foreach ($fileReport->getViolations() as $violation) {
$errorNode = $dom->createElement('error');
Expand Down
9 changes: 6 additions & 3 deletions src/Report/Reporter/ConsoleReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DocbookCS\Report\Reporter;

use DocbookCS\RelativePath;
use DocbookCS\Report\Report;
use DocbookCS\Report\Severity;

Expand All @@ -27,9 +28,11 @@ public function generate(Report $report): string
continue;
}

$filePath = RelativePath::fromWorkingDirectory($fileReport->filePath);

$output .= PHP_EOL;
$output .= $this->bold('FILE: ' . $fileReport->filePath) . PHP_EOL;
$output .= str_repeat('-', min(80, 6 + strlen($fileReport->filePath))) . PHP_EOL;
$output .= $this->bold('FILE: ' . $filePath) . PHP_EOL;
$output .= str_repeat('-', min(80, 6 + strlen($filePath))) . PHP_EOL;

foreach ($fileReport->getViolations() as $violation) {
$output .= sprintf(
Expand All @@ -41,7 +44,7 @@ public function generate(Report $report): string
) . PHP_EOL;
}

$output .= str_repeat('-', min(80, 6 + strlen($fileReport->filePath))) . PHP_EOL;
$output .= str_repeat('-', min(80, 6 + strlen($filePath))) . PHP_EOL;
}

$output .= PHP_EOL;
Expand Down
3 changes: 2 additions & 1 deletion src/Report/Reporter/JsonReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DocbookCS\Report\Reporter;

use DocbookCS\RelativePath;
use DocbookCS\Report\Report;

final class JsonReporter implements ReporterInterface
Expand Down Expand Up @@ -38,7 +39,7 @@ public function generate(Report $report): string
];
}

$data['files'][$fileReport->filePath] = [
$data['files'][RelativePath::fromWorkingDirectory($fileReport->filePath)] = [
'violations' => count($violations),
'messages' => $violations,
];
Expand Down
19 changes: 1 addition & 18 deletions src/Runner/SniffRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function run(RunPlan $plan): Report
$fileReport = $processor->processFile(
$file,
$changedLines,
$this->makeRelative($file),
$file,
);

$violationCount = $fileReport->getViolationCount();
Expand Down Expand Up @@ -101,21 +101,4 @@ private function instantiateSniffs(array $entries): array

return $sniffs;
}

private function makeRelative(string $absolutePath): string
{
$cwd = getcwd();
if ($cwd === false) {
return $absolutePath; // @codeCoverageIgnore
}

$prefix = rtrim(str_replace('\\', '/', $cwd), '/') . '/';
$normalized = str_replace('\\', '/', $absolutePath);

if (str_starts_with($normalized, $prefix)) {
return substr($normalized, strlen($prefix));
}

return $absolutePath; // @codeCoverageIgnore
}
}
12 changes: 12 additions & 0 deletions tests/Unit/Report/ReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DocbookCS\Tests\Unit\Report;

use DocbookCS\RelativePath;
use DocbookCS\Report\FileReport;
use DocbookCS\Report\Report;
use DocbookCS\Report\Severity;
Expand All @@ -14,6 +15,7 @@

#[
CoversClass(FileReport::class),
CoversClass(RelativePath::class),
CoversClass(Report::class),
CoversClass(Violation::class),
]
Expand Down Expand Up @@ -94,6 +96,16 @@ public function itOverwritesFileReportWithSamePath(): void
self::assertSame($second, $report->getFileReports()['file.xml']);
}

#[Test]
public function itKeepsTheFileReportPathWhileRenderingItRelativeToWorkingDirectory(): void
{
$filePath = (getcwd() ?: '') . '/src/chapter.xml';
$fileReport = new FileReport($filePath);

self::assertSame($filePath, $fileReport->filePath);
self::assertSame('src/chapter.xml', RelativePath::fromWorkingDirectory($fileReport->filePath));
}

#[Test]
public function itReturnsTotalViolationsAcrossAllFiles(): void
{
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/Report/Reporter/CheckstyleReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

namespace DocbookCS\Tests\Unit\Report\Reporter;

use DocbookCS\RelativePath;
use DocbookCS\Report\FileReport;
use DocbookCS\Report\Report;
use DocbookCS\Report\Reporter\CheckstyleReporter;
use DocbookCS\Report\Severity;
use DocbookCS\Report\Violation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[
CoversClass(CheckstyleReporter::class),
CoversClass(FileReport::class),
CoversClass(Report::class),
CoversClass(Violation::class),
//
UsesClass(RelativePath::class),
]
final class CheckstyleReporterTest extends TestCase
{
Expand Down Expand Up @@ -113,6 +117,23 @@ public function itIncludesFileNodeWithNameAttribute(): void
self::assertSame('src/broken.xml', $fileNodes->item(0)?->getAttribute('name'));
}

#[Test]
public function itRendersAbsoluteFilePathRelativeToWorkingDirectory(): void
{
$fileReport = new FileReport((getcwd() ?: '') . '/src/broken.xml');
$fileReport->addViolation($this->createViolation());

$report = new Report();
$report->addFileReport($fileReport);

$dom = $this->parseOutput($this->reporter->generate($report));

self::assertSame(
'src/broken.xml',
$dom->getElementsByTagName('file')->item(0)?->getAttribute('name'),
);
}

#[Test]
public function itSetsLineAttribute(): void
{
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/Report/Reporter/ConsoleReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

namespace DocbookCS\Tests\Unit\Report\Reporter;

use DocbookCS\RelativePath;
use DocbookCS\Report\FileReport;
use DocbookCS\Report\Report;
use DocbookCS\Report\Reporter\ConsoleReporter;
use DocbookCS\Report\Severity;
use DocbookCS\Report\Violation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[
CoversClass(ConsoleReporter::class),
CoversClass(FileReport::class),
CoversClass(Report::class),
CoversClass(Violation::class),
//
UsesClass(RelativePath::class),
]
final class ConsoleReporterTest extends TestCase
{
Expand Down Expand Up @@ -90,6 +94,20 @@ public function itShowsFilePathInHeader(): void
self::assertStringContainsString('FILE: src/broken.xml', $output);
}

#[Test]
public function itRendersAbsoluteFilePathRelativeToWorkingDirectory(): void
{
$fileReport = new FileReport((getcwd() ?: '') . '/src/broken.xml');
$fileReport->addViolation($this->createViolation());

$report = new Report();
$report->addFileReport($fileReport);

$output = $this->reporter->generate($report);

self::assertStringContainsString('FILE: src/broken.xml', $output);
}

#[Test]
public function itShowsDashSeparatorAfterFileHeader(): void
{
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/Report/Reporter/JsonReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

namespace DocbookCS\Tests\Unit\Report\Reporter;

use DocbookCS\RelativePath;
use DocbookCS\Report\FileReport;
use DocbookCS\Report\Report;
use DocbookCS\Report\Reporter\JsonReporter;
use DocbookCS\Report\Severity;
use DocbookCS\Report\Violation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[
CoversClass(FileReport::class),
CoversClass(JsonReporter::class),
CoversClass(Report::class),
CoversClass(Violation::class),
//
UsesClass(RelativePath::class),
]
final class JsonReporterTest extends TestCase
{
Expand Down Expand Up @@ -312,6 +316,20 @@ public function itDoesNotEscapeSlashesInOutput(): void
self::assertStringNotContainsString('path\/to\/file.xml', $output);
}

#[Test]
public function itRendersAbsoluteFilePathRelativeToWorkingDirectory(): void
{
$fileReport = new FileReport((getcwd() ?: '') . '/path/to/file.xml');
$fileReport->addViolation($this->createViolation());

$report = new Report();
$report->addFileReport($fileReport);

$data = $this->parseOutput($this->reporter->generate($report));

self::assertArrayHasKey('path/to/file.xml', $data['files']);
}

#[Test]
public function itUsesPrettyPrintedJson(): void
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Runner/SniffRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function setProperty(string $name, string $value): void
}

#[Test]
public function itStoresRelativePathsInFileReports(): void
public function itStoresAbsolutePathsInFileReports(): void
{
$sniff = new class implements SniffInterface {
public function getCode(): string
Expand Down Expand Up @@ -192,9 +192,9 @@ public function setProperty(string $name, string $value): void
$report = $runner->run($this->planPaths($config));

foreach ($report->getFileReports() as $fileReport) {
self::assertFalse(
self::assertTrue(
str_starts_with($fileReport->filePath, '/'),
'Expected relative path, got: ' . $fileReport->filePath,
'Expected absolute path, got: ' . $fileReport->filePath,
);
}
}
Expand Down