Skip to content

Commit

Permalink
feat(core): Added File::isFile() and File::isDirectory().
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Aug 5, 2024
1 parent 0f1db84 commit 6862a0a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/core/src/Utils/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Symfony\Component\Filesystem\Path as SymfonyPath;

use function dirname;
use function is_dir;
use function is_file;

class Path {
Expand All @@ -27,7 +27,15 @@ public static function getRelativePath(string $root, string $path): string {
}

public static function getDirname(string $path): string {
return static::normalize(is_file($path) ? dirname($path) : $path);
return static::normalize(static::isFile($path) ? SymfonyPath::getDirectory($path) : $path);
}

public static function isFile(string $path): bool {
return is_file(static::normalize($path));
}

public static function isDirectory(string $path): bool {
return is_dir(static::normalize($path));
}

public static function isRelative(string $path): bool {
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/Utils/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ public function testNormalize(): void {
self::assertEquals('any/path', Path::normalize('././any/path'));
self::assertEquals('../any/path', Path::normalize('./../any/path'));
self::assertEquals('path', Path::normalize('./any/../path'));
self::assertEquals('', Path::normalize('./'));
self::assertEquals('', Path::normalize('.'));
self::assertEquals('..', Path::normalize('../'));
self::assertEquals('..', Path::normalize('..'));
self::assertEquals('path', Path::normalize('./any/../path/.'));
self::assertEquals('/', Path::normalize('/..'));
self::assertEquals('../any/path', Path::normalize('.\\..\\any\\path'));
self::assertEquals('any/path', Path::normalize('any\\path'));
self::assertEquals('/any/path', Path::normalize('/any/path/'));
self::assertEquals('any/path', Path::normalize('any/path/'));
}

public function testIsNormalized(): void {
Expand All @@ -53,4 +63,16 @@ public function testIsNormalized(): void {
self::assertFalse(Path::isNormalized('././any/path'));
self::assertFalse(Path::isNormalized('./../any/path'));
}

public function testIsFile(): void {
self::assertTrue(Path::isFile(__FILE__));
self::assertFalse(Path::isFile(__DIR__));
self::assertFalse(Path::isFile('/path/to/file'));
}

public function testIsDirectory(): void {
self::assertTrue(Path::isDirectory(__DIR__));
self::assertFalse(Path::isDirectory(__FILE__));
self::assertFalse(Path::isDirectory('/path/to/file'));
}
}

0 comments on commit 6862a0a

Please sign in to comment.