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

Conversation

Simperfit
Copy link
Contributor

Q A
Branch? master
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #25023
License MIT
Doc PR none

I needed to open my windows in order to test this ;).
pr

@nicolas-grekas
Copy link
Member

For 2.7?

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Nov 27, 2017
@@ -1094,44 +1094,46 @@ public function providePathsForMakePathRelative()
$paths = array(
array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/src/Symfony/Component', '../'),
array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/src/Symfony/Component/', '../'),
array('/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component', '../'),
array('/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component/', '../'),
array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/src/Symfony/Component', '../'),
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure why you change some tests...? Can you give more context? 🤔

Copy link
Member

@chalasr chalasr Nov 27, 2017

Choose a reason for hiding this comment

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

seems like he just removed a duplicated assertion :) missed the leading slash

Copy link
Contributor Author

@Simperfit Simperfit Nov 27, 2017

Choose a reason for hiding this comment

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

I've removed some dupplicate and the third point of the issue says that we should be able to dectect the trailing slash, that's why.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree. But we should not remove the tests without the trailing slash either, just add new ones as both ways should work :-)

@Simperfit Simperfit changed the base branch from master to 2.7 November 28, 2017 05:26
@Simperfit Simperfit force-pushed the feature/fix-make-relative-path branch from 04c1efa to a69279c Compare November 28, 2017 05:26
@Simperfit
Copy link
Contributor Author

rebased on 2.7

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

// Construct $endPath from traversing to the common path, then to the remaining $endPath
$relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.'/' : '');
if ('/' !== substr($endPath, -1)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sroze So I should remove this ?
because both ways are not possible IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ping @sroze WDYT?

@Simperfit Simperfit force-pushed the feature/fix-make-relative-path branch 2 times, most recently from 9225f6d to 53700b6 Compare January 5, 2018 12:43
@Simperfit Simperfit force-pushed the feature/fix-make-relative-path branch from 53700b6 to 7acfd17 Compare February 16, 2018 09:54
@Simperfit Simperfit changed the title [Filesystem] Several issues with Filesystem::makePathRelative [WIP][Filesystem] Several issues with Filesystem::makePathRelative Feb 16, 2018

if ($endPath[1] !== $startPath[1]) {
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?

@@ -849,56 +1100,65 @@ public function providePathsForMakePathRelative()
array('/usr/lib/symfony/', '/var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'),
array('usr/lib/symfony/', 'var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'),
array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/', 'src/Symfony/'),
array('/aa/bb', '/aa/bb', './'),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you changing these? Some changes you have done were already tested. E.g. you change here /aa/bb to /aa/bb/, but that one already exists. So much changes in tests is reason why everybody is hesitant to review this. Please go through your changes in tests and only change those that are wrong, otherwise just add new ones.

@@ -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

}
$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);


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.

@fabpot
Copy link
Member

fabpot commented May 28, 2018

Closing as there is no more activity here. Feel free to reopen though (related issue is kept open).

@fabpot fabpot closed this May 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants