Skip to content

Commit 245a58c

Browse files
committed
up: add some Path help methods, add some tests
1 parent 86fa1b1 commit 245a58c

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

src/FileSystem.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use function is_array;
2626
use function is_dir;
2727
use function is_file;
28-
use function is_string;
2928
use function preg_match;
3029
use function str_ends_with;
3130
use function str_ireplace;
@@ -51,12 +50,12 @@ abstract class FileSystem
5150
*/
5251
public static function isAbsPath(string $path): bool
5352
{
54-
if (!$path || !is_string($path)) {
53+
if (!$path) {
5554
return false;
5655
}
5756

5857
if (str_starts_with($path, '/') || // linux/mac
59-
1 === preg_match('#^[a-z]:[\/|\\\].+#i', $path) // windows
58+
1 === preg_match('#^[a-z]:[/|\\\].+#i', $path) // windows
6059
) {
6160
return true;
6261
}

src/Parser/IniParser.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class IniParser extends AbstractParser
3939
* @param string $fileDir When the second param is true, this param is valid.
4040
*
4141
* @return array
42-
* @throws InvalidArgumentException
4342
* @throws UnexpectedValueException
4443
*/
4544
protected static function doParse(

src/Path.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,33 @@
99
*/
1010
class Path extends FileSystem
1111
{
12+
/**
13+
* @param string $path
14+
*
15+
* @return bool
16+
*/
17+
public static function isAbs(string $path): bool
18+
{
19+
return self::isAbsPath($path);
20+
}
1221

22+
/**
23+
* @param string $path
24+
*
25+
* @return bool
26+
*/
27+
public static function isAbsolute(string $path): bool
28+
{
29+
return self::isAbsolutePath($path);
30+
}
31+
32+
/**
33+
* @param string $path
34+
*
35+
* @return string
36+
*/
37+
public static function format(string $path): string
38+
{
39+
return self::pathFormat($path);
40+
}
1341
}

test/PathTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\FsUtilTest;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Toolkit\FsUtil\Path;
7+
8+
/**
9+
* class PathTest
10+
*
11+
* @author inhere
12+
*/
13+
class PathTest extends TestCase
14+
{
15+
public function testPath_isAbs(): void
16+
{
17+
$tests = [
18+
'/tmp',
19+
'C:/tmp',
20+
'C:\\tmp',
21+
'C:\tmp',
22+
"C:\\tmp",
23+
];
24+
25+
foreach ($tests as $case) {
26+
$this->assertTrue(Path::isAbs($case));
27+
$this->assertTrue(Path::isAbsolute($case));
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)