diff --git a/composer.json b/composer.json index 850f21c9..1d3394e8 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "ext-json": "*", "composer-runtime-api": "^2.0.0", "symfony/console": "^5.4.9 || ^6.0 || ^7.0", + "symfony/filesystem": "^5.4 || ^6.0 || ^7.0", "symfony/finder": "^5.4 || ^6.0 || ^7.0", "symfony/string": "^5.4 || ^6.0 || ^7.0", "twig/twig": "^2.14.0 || ^3.0.5", @@ -36,7 +37,6 @@ "psalm/plugin-phpunit": "^0.18.4", "psalm/plugin-symfony": "^5.0.0", "rector/rector": "^1.0.0", - "symfony/filesystem": "^5.4 || ^6.0 || ^7.0", "symfony/process": "^5.4 || ^6.0 || ^7.0", "symfony/twig-bridge": "^5.4 || ^6.0 || ^7.0", "symfony/ux-twig-component": "^2.2.0", diff --git a/src/File/FileHelper.php b/src/File/FileHelper.php index 18df0a2d..86f918c0 100644 --- a/src/File/FileHelper.php +++ b/src/File/FileHelper.php @@ -4,6 +4,7 @@ namespace TwigCsFixer\File; +use Symfony\Component\Filesystem\Path; use Webmozart\Assert\Assert; final class FileHelper @@ -13,7 +14,7 @@ public static function getAbsolutePath(string $path, ?string $workingDir = null) $workingDir ??= @getcwd(); Assert::notFalse($workingDir, 'Cannot get the current working directory.'); - return self::isAbsolutePath($path) ? $path : $workingDir.\DIRECTORY_SEPARATOR.$path; + return Path::makeAbsolute($path, $workingDir); } public static function removeDot(string $fileName): string @@ -70,15 +71,15 @@ private static function splitPath( array $ignoredDir = [], ?string $workingDir = null, ): array { - $baseDir = self::simplifyPath(self::getAbsolutePath($baseDir ?? '', $workingDir)); - $path = self::simplifyPath(self::getAbsolutePath($path, $workingDir)); + $baseDir = Path::canonicalize(self::getAbsolutePath($baseDir ?? '', $workingDir)); + $path = Path::canonicalize(self::getAbsolutePath($path, $workingDir)); if (!str_starts_with($path, $baseDir.\DIRECTORY_SEPARATOR)) { return []; } foreach ($ignoredDir as $ignoredDirectory) { - $ignoredDirectory = self::simplifyPath(self::getAbsolutePath($ignoredDirectory, $baseDir)); + $ignoredDirectory = Path::canonicalize(self::getAbsolutePath($ignoredDirectory, $baseDir)); if (str_starts_with($path, $ignoredDirectory.\DIRECTORY_SEPARATOR)) { return []; } @@ -86,33 +87,4 @@ private static function splitPath( return explode(\DIRECTORY_SEPARATOR, substr($path, \strlen($baseDir) + 1)); } - - private static function isAbsolutePath(string $path): bool - { - return '' !== $path && ( - '/' === $path[0] - || '\\' === $path[0] - || 1 === preg_match('#^[a-zA-Z]:\\\\#', $path) - ); - } - - private static function simplifyPath(string $absolutePath): string - { - if (!self::isAbsolutePath($absolutePath)) { - throw new \InvalidArgumentException('The path must be absolute.'); - } - - $parts = explode(\DIRECTORY_SEPARATOR, $absolutePath); - - $result = []; - foreach ($parts as $part) { - if ('..' === $part) { - array_pop($result); - } elseif ('.' !== $part && '' !== $part) { - $result[] = $part; - } - } - - return \DIRECTORY_SEPARATOR.implode(\DIRECTORY_SEPARATOR, $result); - } } diff --git a/tests/File/FileHelperTest.php b/tests/File/FileHelperTest.php index ca443551..94e01499 100644 --- a/tests/File/FileHelperTest.php +++ b/tests/File/FileHelperTest.php @@ -180,4 +180,6 @@ public static function getDirectoriesDataProvider(): iterable [], ]; } + + }