diff --git a/CHANGELOG.md b/CHANGELOG.md index adf525e..59c1e14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec to [Semantic Versioning] (http://semver.org/). For change log format, use [Keep a Changelog] (http://keepachangelog.com/). +## [1.6.2] - 2022-02-04 + +### Fixed + +- `FileHelper::resolveAbsolutePath()` with empty destination path + ## [1.6.1] - 2022-01-18 ### Fixed diff --git a/src/FileHelper.php b/src/FileHelper.php index 36cfd80..6ca8942 100644 --- a/src/FileHelper.php +++ b/src/FileHelper.php @@ -123,9 +123,9 @@ public static function resolveAbsolutePath(string $srcPath, string $dstPath): ?s { $srcPath = self::uniformizePathSeparator($srcPath); $dstPath = self::uniformizePathSeparator($dstPath); - $finalPath = $dstPath; + $finalPath = $dstPath ?: $srcPath; - if (substr($dstPath, 0, 1) !== '/') { + if (strlen($dstPath) > 0 && substr($dstPath, 0, 1) !== '/') { // Complete absolute link if (substr($dstPath, 0, 2) === './') { $dstPath = substr($dstPath, 2); diff --git a/tests/FileHelperTest.php b/tests/FileHelperTest.php index b898a04..b7890e1 100644 --- a/tests/FileHelperTest.php +++ b/tests/FileHelperTest.php @@ -65,19 +65,21 @@ public function absolutePathProvider(): array ['foo/bar/', '../', '/foo/'], ['foo/bar/', '..', '/foo/'], ['foo/bar/', '../qux', '/foo/qux'], + ['foo/bar/', '', '/foo/bar/'], + ['foo/bar', '', '/foo/bar'], ]; } /** * @param $src * @param $dst - * @param $excepted + * @param $expected * * @dataProvider absolutePathProvider */ - public function testResolveAbsolutePath($src, $dst, $excepted) + public function testResolveAbsolutePath($src, $dst, $expected) { - $this->assertEquals($excepted, FileHelper::resolveAbsolutePath($src, $dst)); + $this->assertEquals($expected, FileHelper::resolveAbsolutePath($src, $dst)); } public function relativePathProvider(): array @@ -106,14 +108,16 @@ public function relativePathProvider(): array ['/foo/bar/index.md', '../../', '../../'], ['/foo/bar/index.md', '..', '../'], ['/foo/bar/index.md', '../', '../'], + ['/foo/bar/', '', './'], + ['/foo/bar/index.md', '', './index.md'], ]; } /** * @dataProvider relativePathProvider */ - public function testResolveRelativePath($src, $dst, $excepted) + public function testResolveRelativePath($src, $dst, $expected) { - $this->assertEquals($excepted, FileHelper::resolveRelativePath($src, $dst)); + $this->assertEquals($expected, FileHelper::resolveRelativePath($src, $dst)); } }