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

[File] Fix relative paths calculation for mixed slash paths #37

Merged
merged 2 commits into from
Oct 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,12 @@ protected function formatExpectedPatch($patch)
{
if ('\\' === DIRECTORY_SEPARATOR) {
$formatLine = function ($line) {
if (0 === strpos($line, '---') || 0 === strpos($line, '+++')) {
$line = preg_replace('~/(?<!(a|b)/)~', '\\', $line);
}

// replace lines for diff-path-files starting with --- or +++
$line = preg_replace_callback('~^((?:---|\+\+\+)\s*(?:a|b)/)(.*)~', function ($match) {
list($all, $diff, $path) = $match;

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

Expand Down
10 changes: 8 additions & 2 deletions src/main/QafooLabs/Refactoring/Domain/Model/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public static function createFromPath($path, $workingDirectory)
$workingDirectory = rtrim($workingDirectory, '/\\');
$relativePath = ltrim(str_replace($workingDirectory, "", $path), "/\\");

// converted mixed, wrapped, absolute paths on windows
if (DIRECTORY_SEPARATOR === '\\' && strpos($relativePath, '://') !== FALSE) {
$relativePath = str_replace('\\', '/', $relativePath);
}

return new self($relativePath, $code);
}

Expand Down Expand Up @@ -94,11 +99,13 @@ private function parseFileForPsr0NamespaceName()
{
$file = ltrim($this->getRelativePath(), DIRECTORY_SEPARATOR);

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

$parts = explode(DIRECTORY_SEPARATOR, ltrim($this->getRelativePath(), DIRECTORY_SEPARATOR));
$parts = explode($separator, $file);
$namespace = array();

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

13 changes: 11 additions & 2 deletions src/test/QafooLabs/Refactoring/Domain/Model/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,20 @@ public function testGetRelativePathRespectsMixedWindowsPathsAndWorkingDirectoryT
$this->assertEquals("Foo\Bar.php", $file->getRelativePath());
}

public function testRelativePathConstructionForAbsoluteVFSFiles()
{
$src = $this->createFileSystem()->getChild('src')->url();
$bar = $src.DIRECTORY_SEPARATOR.'Foo'.DIRECTORY_SEPARATOR.'Bar.php';

$file = File::createFromPath($bar, $notRelatedWorkingDir = __DIR__);
$this->assertEquals('vfs://project/src/Foo/Bar.php', $file->getRelativePath());
}

static public function dataExtractPsr0ClassName()
{
return array(
array(new PhpName('Foo', 'Foo'), 'src/Foo.php'),
array(new PhpName('Foo\Bar', 'Bar'), 'src/Foo/Bar.php'),
array(new PhpName('Foo', 'Foo'), 'src'.DIRECTORY_SEPARATOR.'Foo.php'),
array(new PhpName('Foo\Bar', 'Bar'), 'src'.DIRECTORY_SEPARATOR.'Foo'.DIRECTORY_SEPARATOR.'Bar.php'),
);
}

Expand Down