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 5783547
Show file tree
Hide file tree
Showing 2 changed files with 10 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
42 changes: 9 additions & 33 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

namespace TwigCsFixer\File;

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

final class FileHelper
{
public static function getAbsolutePath(string $path, ?string $workingDir = null): string
{
if (Path::isAbsolute($path)) {
return $path;
}

$workingDir ??= @getcwd();
Assert::notFalse($workingDir, 'Cannot get the current working directory.');

return self::isAbsolutePath($path) ? $path : $workingDir.\DIRECTORY_SEPARATOR.$path;
return $workingDir.\DIRECTORY_SEPARATOR.$path;
}

public static function removeDot(string $fileName): string
Expand Down Expand Up @@ -70,49 +75,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);
}
}

0 comments on commit 5783547

Please sign in to comment.