Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 287713f

Browse files
committed
bug #19478 fixed Filesystem:makePathRelative and added 2 more testcases (muhammedeminakbulut)
This PR was submitted for the 2.8 branch but it was merged into the 2.7 branch instead (closes #19478). Discussion ---------- fixed Filesystem:makePathRelative and added 2 more testcases | Q | A | ------------- | --- | Branch? |2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT The 2 new test cases where broken on the old code. The old testcases and the new one succeed with the given code. Commits ------- cd3206c fixed Filesystem:makePathRelative and added 2 more testcases
2 parents 8d218e7 + cd3206c commit 287713f

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,14 @@ public function makePathRelative($endPath, $startPath)
369369
}
370370

371371
// Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
372-
$depth = count($startPathArr) - $index;
372+
if (count($startPathArr) === 1 && $startPathArr[0] === '') {
373+
$depth = 0;
374+
} else {
375+
$depth = count($startPathArr) - $index;
376+
}
373377

374378
// When we need to traverse from the start, and we are starting from a root path, don't add '../'
375-
if ('/' === $startPath[0] && 0 === $index && 1 === $depth) {
379+
if ('/' === $startPath[0] && 0 === $index && 0 === $depth) {
376380
$traverser = '';
377381
} else {
378382
// Repeated "../" for each level need to reach the common path

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,8 @@ public function providePathsForMakePathRelative()
840840
array('/a/aab/bb/', '/a/aa/', '../aab/bb/'),
841841
array('/a/aab/bb/', '/', 'a/aab/bb/'),
842842
array('/a/aab/bb/', '/b/aab', '../../a/aab/bb/'),
843+
array('/aab/bb', '/aa', '../aab/bb/'),
844+
array('/aab', '/aa', '../aab/'),
843845
);
844846

845847
if ('\\' === DIRECTORY_SEPARATOR) {

0 commit comments

Comments
 (0)