Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove directory class #287

Merged
merged 1 commit into from
Jul 26, 2024
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
7 changes: 7 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ The `Token::NAME_TYPE` has been split in four:
- $token->getPosition();
+ $token->getLinePosition();
```

### Directory

```diff
- (new Directory($dir))->getRelativePathTo($file);
+ FileHelper::getRelativePath($file, $dir);
```
31 changes: 0 additions & 31 deletions src/Cache/Directory.php

This file was deleted.

10 changes: 5 additions & 5 deletions src/Cache/Manager/FileCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace TwigCsFixer\Cache\Manager;

use TwigCsFixer\Cache\Cache;
use TwigCsFixer\Cache\Directory;
use TwigCsFixer\Cache\FileHandler\CacheFileHandlerInterface;
use TwigCsFixer\Cache\Signature;
use TwigCsFixer\Exception\CannotWriteCacheException;
use TwigCsFixer\File\FileHelper;

/**
* Class supports caching information about state of fixing files.
Expand All @@ -26,13 +26,13 @@ final class FileCacheManager implements CacheManagerInterface
{
private Cache $cache;

private Directory $cacheDirectory;
private string $cacheDirectory;

public function __construct(
private CacheFileHandlerInterface $handler,
private Signature $signature
) {
$this->cacheDirectory = new Directory(\dirname($handler->getFile()));
$this->cacheDirectory = \dirname($handler->getFile());

$this->readCache();
}
Expand Down Expand Up @@ -67,14 +67,14 @@ public function __wakeup(): void

public function needFixing(string $file, string $fileContent): bool
{
$file = $this->cacheDirectory->getRelativePathTo($file);
$file = FileHelper::getRelativePath($file, $this->cacheDirectory);

return !$this->cache->has($file) || $this->cache->get($file) !== $this->calcHash($fileContent);
}

public function setFile(string $file, string $fileContent): void
{
$file = $this->cacheDirectory->getRelativePathTo($file);
$file = FileHelper::getRelativePath($file, $this->cacheDirectory);

$hash = $this->calcHash($fileContent);

Expand Down
19 changes: 19 additions & 0 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ public static function detectEOL(string $content): string
return $matches[0] ?? \PHP_EOL;
}

public static function normalizePath(string $path): string
{
return str_replace(['\\', '/'], \DIRECTORY_SEPARATOR, $path);
}

public static function getRelativePath(string $path, string $baseDir): string
{
$path = static::normalizePath($path);

if (
'' === $baseDir
|| 0 !== stripos($path, $baseDir.\DIRECTORY_SEPARATOR)
) {
return $path;
}

return substr($path, \strlen($baseDir) + 1);
}

public static function getAbsolutePath(string $path, ?string $workingDir = null): string
{
if (Path::isAbsolute($path)) {
Expand Down
40 changes: 0 additions & 40 deletions tests/Cache/DirectoryTest.php

This file was deleted.

28 changes: 28 additions & 0 deletions tests/File/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ public static function detectEOLDataProvider(): iterable
yield ["foo\r\n", "\r\n"];
}

/**
* @dataProvider getRelativePathToDataProvider
*/
public function testGetRelativePathTo(string $directoryName, string $file, string $expected): void
{
static::assertSame($expected, FileHelper::getRelativePath($file, $directoryName));
}

/**
* @return iterable<array-key, array{string, string, string}>
*/
public static function getRelativePathToDataProvider(): iterable
{
$slash = \DIRECTORY_SEPARATOR;

yield ['', 'foo.php', 'foo.php'];
yield ['', '/foo.php', $slash.'foo.php'];
yield ['', '\foo.php', $slash.'foo.php'];
yield ['directory', 'foo.php', 'foo.php'];
yield ['directory', 'directory.php', 'directory.php'];
yield ['directory', 'directory/foo.php', 'foo.php'];
yield ['directory', 'directory\foo.php', 'foo.php'];
yield ['directory', '/foo.php', $slash.'foo.php'];
yield ['directory', '\foo.php', $slash.'foo.php'];
yield ['directory', '/directory/foo.php', $slash.'directory'.$slash.'foo.php'];
yield ['directory', '\directory\foo.php', $slash.'directory'.$slash.'foo.php'];
}

/**
* @dataProvider removeDotDataProvider
*/
Expand Down
Loading