Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][Filesystem] Several issues with Filesystem::makePathRelative #25169

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,25 @@ public function makePathRelative($endPath, $startPath)
if ('\\' === DIRECTORY_SEPARATOR) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment above needs to be updated. This block no longer just normalizes separators

$endPath = str_replace('\\', '/', $endPath);
$startPath = str_replace('\\', '/', $startPath);
}

$stripDriveLetter = function ($path) {
if (strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) {
return substr($path, 2);
}
$stripDriveLetter = function ($path) {
if (strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) {
return array(substr($path, 2), $path[0].$path[1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't do this here, you changed what this function does - now name of function is wrong. I suggest to do something like

$isAbsolutePath = function($path) {
    return strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0]);
}

if ($isAbsolutePath($endPath) && $isAbsolutePath($startPath) && substr($endPath, 0, 2) !== substr($startPath, 0, 2)) {
    throw new \InvalidArgumentException("You can't make relative links across drives");
}

$stripDriveLetter = function ($path) (use $isAbsolutePath) {
    if ($isAbsolutePath) {
        return substr($path, 2);
    }

    return $path;
};

$endPath = $stripDriveLetter($endPath);
$startPath = $stripDriveLetter($startPath);

}

return $path;
};
return $path;
};

$endPath = $stripDriveLetter($endPath);
$startPath = $stripDriveLetter($startPath);
$endPath = $stripDriveLetter($endPath);
$startPath = $stripDriveLetter($startPath);

if ($endPath[1] !== $startPath[1]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will do unexpected things if paths don't contain :. Array is returned only when it does. So when it doesn't, you are getting here second string character of relative path.

return '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you return empty string here? It doesn't make sense. Maybe throw exception?

}

$endPath = $endPath[0];
$startPath = $startPath[0];
}
// Split the paths into arrays
$startPathArr = explode('/', trim($startPath, '/'));
$endPathArr = explode('/', trim($endPath, '/'));
Expand Down Expand Up @@ -419,7 +425,6 @@ public function makePathRelative($endPath, $startPath)

// Repeated "../" for each level need to reach the common path
$traverser = str_repeat('../', $depth);

$endPathRemainder = implode('/', array_slice($endPathArr, $index));

// Construct $endPath from traversing to the common path, then to the remaining $endPath
Expand Down