1111
1212namespace chillerlan \Utilities ;
1313
14+ use InvalidArgumentException ;
1415use RuntimeException ;
1516use function clearstatcache ;
1617use 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 );
0 commit comments