Skip to content

Commit

Permalink
make file path symfony independent
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 19, 2023
1 parent edfe440 commit b47f1e7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
9 changes: 3 additions & 6 deletions app/FileSystem/StaticRelativeFilePathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@

namespace TomasVotruba\ClassLeak\FileSystem;

use Symfony\Component\Filesystem\Filesystem;

final class StaticRelativeFilePathHelper
{
public static function resolveFromCwd(string $filePath): string
{
$filesystem = new Filesystem();
$relativeFilePathFromCwd = $filesystem->makePathRelative((string) realpath($filePath), getcwd());

return rtrim($relativeFilePathFromCwd, '/');
// make path relative with native PHP
$relativeFilePath = (string) realpath($filePath);
return str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $relativeFilePath);
}
}
5 changes: 5 additions & 0 deletions tests/FileSystem/Fixture/some-file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

// some content
17 changes: 17 additions & 0 deletions tests/FileSystem/StaticRelativeFilePathHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\ClassLeak\Tests\FileSystem;

use PHPUnit\Framework\TestCase;
use TomasVotruba\ClassLeak\FileSystem\StaticRelativeFilePathHelper;

final class StaticRelativeFilePathHelperTest extends TestCase
{
public function test(): void
{
$relativeFilePath = StaticRelativeFilePathHelper::resolveFromCwd(__DIR__ . '/Fixture/some-file.php');
$this->assertSame('tests/FileSystem/Fixture/some-file.php', $relativeFilePath);
}
}

0 comments on commit b47f1e7

Please sign in to comment.