Skip to content

Commit e96af97

Browse files
committed
:octocat: added File::realpath()
1 parent ec55eac commit e96af97

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

src/Directory.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use function is_readable;
2020
use function is_writable;
2121
use function mkdir;
22-
use function realpath;
2322
use function rmdir;
2423
use function sprintf;
2524
use function trim;
@@ -78,16 +77,9 @@ public static function create(string $dir, int $permissions = 0o777, bool $recur
7877
throw new RuntimeException(sprintf('could not create directory: %s', $dir)); // @codeCoverageIgnore
7978
}
8079

81-
$dir = realpath($dir);
82-
83-
// reaplpath error
84-
if(!$dir){
85-
throw new RuntimeException('invalid directory (realpath)'); // @codeCoverageIgnore
86-
}
87-
8880
clearstatcache();
8981

90-
return $dir;
82+
return File::realpath($dir);
9183
}
9284

9385
/**

src/File.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace chillerlan\Utilities;
1313

14+
use InvalidArgumentException;
1415
use RuntimeException;
1516
use function clearstatcache;
1617
use function dirname;
@@ -55,11 +56,29 @@ public static function isWritable(string $file):bool{
5556
return self::exists($file) && is_writable($file);
5657
}
5758

59+
/**
60+
* Returns the absolute real path to the given file or directory
61+
*
62+
* @codeCoverageIgnore
63+
*/
64+
public static function realpath(string $path):string{
65+
$realpath = realpath($path);
66+
67+
if($realpath === false){
68+
throw new InvalidArgumentException('invalid file path');
69+
}
70+
71+
return $realpath;
72+
}
73+
74+
/**
75+
* Deletes a file
76+
*/
5877
public static function delete(string $file):bool{
59-
$file = realpath($file);
78+
$file = self::realpath($file);
6079

61-
if($file === false || !self::isWritable($file)){
62-
throw new RuntimeException('cannot read the given file');
80+
if(!self::isWritable($file)){
81+
throw new RuntimeException('cannot read the given file'); // @codeCoverageIgnore
6382
}
6483

6584
if(!unlink($file)){
@@ -79,10 +98,10 @@ public static function delete(string $file):bool{
7998
* @throws \RuntimeException
8099
*/
81100
public static function load(string $file, int $offset = 0, int|null $length = null):string{
82-
$file = realpath($file);
101+
$file = self::realpath($file);
83102

84-
if($file === false || !self::isReadable($file)){
85-
throw new RuntimeException('cannot read the given file');
103+
if(!self::isReadable($file)){
104+
throw new RuntimeException('cannot read the given file'); // @codeCoverageIgnore
86105
}
87106

88107
$content = file_get_contents(filename: $file, offset: $offset, length: $length);

tests/DirAndFileTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public function loadFile():void{
5454

5555
#[Test]
5656
public function loadInvalidFileException():void{
57-
$this->expectException(RuntimeException::class);
58-
$this->expectExceptionMessage('cannot read the given file');
57+
$this->expectException(InvalidArgumentException::class);
58+
$this->expectExceptionMessage('invalid file path');
5959

6060
File::load(self::invalidFile);
6161
}
@@ -68,8 +68,8 @@ public function deleteFile():void{
6868

6969
#[Test]
7070
public function deleteInvalidFileException():void{
71-
$this->expectException(RuntimeException::class);
72-
$this->expectExceptionMessage('cannot read the given file');
71+
$this->expectException(InvalidArgumentException::class);
72+
$this->expectExceptionMessage('invalid file path');
7373

7474
File::delete(self::invalidFile);
7575
}

0 commit comments

Comments
 (0)