Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Commit 484d8d7

Browse files
committed
Merge pull request #37 from pscheit/fix-relative-paths
[File] Fix relative paths calculation for mixed slash paths
2 parents 205fe91 + 7e22f46 commit 484d8d7

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,12 @@ protected function formatExpectedPatch($patch)
121121
{
122122
if ('\\' === DIRECTORY_SEPARATOR) {
123123
$formatLine = function ($line) {
124-
if (0 === strpos($line, '---') || 0 === strpos($line, '+++')) {
125-
$line = preg_replace('~/(?<!(a|b)/)~', '\\', $line);
126-
}
127-
124+
// replace lines for diff-path-files starting with --- or +++
128125
$line = preg_replace_callback('~^((?:---|\+\+\+)\s*(?:a|b)/)(.*)~', function ($match) {
129126
list($all, $diff, $path) = $match;
130127

131-
if (0 === preg_match('~^[a-z]+://~i', $path)) {
128+
// dont replace wrapped path separators
129+
if (! preg_match('~^[a-z]+://~i', $path)) {
132130
$path = str_replace('/', '\\', $path);
133131
}
134132

src/main/QafooLabs/Refactoring/Domain/Model/File.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public static function createFromPath($path, $workingDirectory)
3737
$workingDirectory = rtrim($workingDirectory, '/\\');
3838
$relativePath = ltrim(str_replace($workingDirectory, "", $path), "/\\");
3939

40+
// converted mixed, wrapped, absolute paths on windows
41+
if (DIRECTORY_SEPARATOR === '\\' && strpos($relativePath, '://') !== FALSE) {
42+
$relativePath = str_replace('\\', '/', $relativePath);
43+
}
44+
4045
return new self($relativePath, $code);
4146
}
4247

@@ -94,11 +99,13 @@ private function parseFileForPsr0NamespaceName()
9499
{
95100
$file = ltrim($this->getRelativePath(), DIRECTORY_SEPARATOR);
96101

102+
$separator = DIRECTORY_SEPARATOR;
97103
if (preg_match('(^([a-z]+:\/\/))', $file, $matches)) {
98104
$file = substr($file, strlen($matches[1]));
105+
$separator = '/';
99106
}
100107

101-
$parts = explode(DIRECTORY_SEPARATOR, ltrim($this->getRelativePath(), DIRECTORY_SEPARATOR));
108+
$parts = explode($separator, $file);
102109
$namespace = array();
103110

104111
foreach ($parts as $part) {
@@ -120,4 +127,3 @@ private function startsWithLowerCase($string)
120127
return isset($string[0]) && strtolower($string[0]) === $string[0];
121128
}
122129
}
123-

src/test/QafooLabs/Refactoring/Domain/Model/FileTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,20 @@ public function testGetRelativePathRespectsMixedWindowsPathsAndWorkingDirectoryT
3434
$this->assertEquals("Foo\Bar.php", $file->getRelativePath());
3535
}
3636

37+
public function testRelativePathConstructionForAbsoluteVFSFiles()
38+
{
39+
$src = $this->createFileSystem()->getChild('src')->url();
40+
$bar = $src.DIRECTORY_SEPARATOR.'Foo'.DIRECTORY_SEPARATOR.'Bar.php';
41+
42+
$file = File::createFromPath($bar, $notRelatedWorkingDir = __DIR__);
43+
$this->assertEquals('vfs://project/src/Foo/Bar.php', $file->getRelativePath());
44+
}
45+
3746
static public function dataExtractPsr0ClassName()
3847
{
3948
return array(
40-
array(new PhpName('Foo', 'Foo'), 'src/Foo.php'),
41-
array(new PhpName('Foo\Bar', 'Bar'), 'src/Foo/Bar.php'),
49+
array(new PhpName('Foo', 'Foo'), 'src'.DIRECTORY_SEPARATOR.'Foo.php'),
50+
array(new PhpName('Foo\Bar', 'Bar'), 'src'.DIRECTORY_SEPARATOR.'Foo'.DIRECTORY_SEPARATOR.'Bar.php'),
4251
);
4352
}
4453

0 commit comments

Comments
 (0)