Skip to content

Commit a3a1187

Browse files
committed
fix: joinPath
joinPath("/", "abc") now returns "/abc"
1 parent 6bcbf6b commit a3a1187

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/PI.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,25 @@ public static function joinPath($path1, $path2) {
7373
$path2 = trim($path2, $win_delim);
7474
}
7575

76-
//preserve the root (Linux, Mac)
77-
$startChar = substr($path1Orig, 0, 1);
78-
if ($startChar == $root_delim) {
79-
$path1 = $root_delim . $path1;
80-
}
8176

77+
$finalPath;
8278
if (!$path1 && !$path2) {
83-
return "";
79+
$finalPath = "";
8480
} else if (!$path1) {
85-
return $path2;
81+
$finalPath = $path2;
8682
} else if (!$path2) {
87-
return $path1;
83+
$finalPath = $path1;
8884
} else {
89-
return $path1 . $final_delim . $path2;
85+
$finalPath = $path1 . $final_delim . $path2;
86+
}
87+
88+
//preserve the root (Linux, Mac)
89+
$startChar = substr($path1Orig, 0, 1);
90+
if ($startChar == $root_delim) {
91+
$finalPath = $root_delim . $finalPath;
9092
}
93+
94+
return $finalPath;
9195
}
9296

9397
}

test/PI/JoinPathTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public function test_preserve_root_delimiter_first_arg() {
6969
$this->assertEquals("/abc", PI::joinPath("/abc/", ""));
7070
$this->assertEquals("/abc", PI::joinPath("/abc/", null));
7171

72+
$this->assertEquals("/abc", PI::joinPath("/", "abc"));
73+
$this->assertEquals("/abc", PI::joinPath("/", "/abc"));
74+
7275
$this->assertEquals("/abc/d", PI::joinPath("/abc", "d"));
7376
$this->assertEquals("/abc/d", PI::joinPath("/abc", "/d"));
7477
}

0 commit comments

Comments
 (0)