Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Feb 18, 2024
1 parent 646c79b commit e74da52
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 34 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
38 changes: 5 additions & 33 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace TwigCsFixer\File;

use Symfony\Component\Filesystem\Path;
use Webmozart\Assert\Assert;

final class FileHelper
Expand All @@ -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
Expand Down Expand Up @@ -70,49 +71,20 @@ 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 [];
}
}

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);
}
}
2 changes: 2 additions & 0 deletions tests/File/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,6 @@ public static function getDirectoriesDataProvider(): iterable
[],
];
}


}

0 comments on commit e74da52

Please sign in to comment.