Skip to content

Commit de40ca3

Browse files
committed
Fix bugs in SimpleRelativePathHelper
This fixes and adds tests for the following: - If CWD ended in a directory separator path+1 was stripped ('/home' = 'ome') - Even if CWD partially matched it would strip the path ('/homeFolder' = 'Folder') - getRelativePath didn't work with Windows paths
1 parent ffaed50 commit de40ca3

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed

src/File/SimpleRelativePathHelper.php

+23-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,36 @@ class SimpleRelativePathHelper implements RelativePathHelper
66
{
77

88
/** @var string */
9-
private $currentWorkingDirectory;
9+
private $directorySeparator;
1010

11-
public function __construct(string $currentWorkingDirectory)
11+
/** @var string */
12+
private $currentWorkingDirectory = '';
13+
14+
public function __construct(string $currentWorkingDirectory, string $directorySeparator = DIRECTORY_SEPARATOR)
1215
{
16+
$this->directorySeparator = $directorySeparator;
17+
18+
if ($currentWorkingDirectory !== $directorySeparator) {
19+
$currentWorkingDirectory = rtrim($currentWorkingDirectory, $directorySeparator);
20+
}
1321
$this->currentWorkingDirectory = $currentWorkingDirectory;
1422
}
1523

1624
public function getRelativePath(string $filename): string
1725
{
18-
if ($this->currentWorkingDirectory !== '' && strpos($filename, $this->currentWorkingDirectory) === 0) {
26+
if ($this->currentWorkingDirectory === '') {
27+
return $filename;
28+
}
29+
30+
if ($this->currentWorkingDirectory === $this->directorySeparator) {
31+
if (strpos($filename, $this->currentWorkingDirectory) === 0) {
32+
return substr($filename, strlen($this->currentWorkingDirectory));
33+
}
34+
35+
return $filename;
36+
}
37+
38+
if (strpos($filename, $this->currentWorkingDirectory . $this->directorySeparator) === 0) {
1939
return substr($filename, strlen($this->currentWorkingDirectory) + 1);
2040
}
2141

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\File;
4+
5+
class SimpleRelativePathHelperTest extends \PHPUnit\Framework\TestCase
6+
{
7+
8+
public function dataGetRelativePath(): array
9+
{
10+
return [
11+
[
12+
'/usr',
13+
'/',
14+
'/usr/app/test.php',
15+
'app/test.php',
16+
],
17+
[
18+
'',
19+
'/',
20+
'/usr/app/test.php',
21+
'/usr/app/test.php',
22+
],
23+
[
24+
'/var',
25+
'/',
26+
'/usr/app/test.php',
27+
'/usr/app/test.php',
28+
],
29+
[
30+
'/usr/app',
31+
'/',
32+
'/usr/app/src/test.php',
33+
'src/test.php',
34+
],
35+
[
36+
'/',
37+
'/',
38+
'/usr/app/test.php',
39+
'usr/app/test.php',
40+
],
41+
[
42+
'/usr/',
43+
'/',
44+
'/usr/app/test.php',
45+
'app/test.php',
46+
],
47+
[
48+
'/usr/app',
49+
'/',
50+
'/usr/application/test.php',
51+
'/usr/application/test.php',
52+
],
53+
[
54+
'C:\\app',
55+
'\\',
56+
'C:\\app\\test.php',
57+
'test.php',
58+
],
59+
[
60+
'C:\\app\\',
61+
'\\',
62+
'C:\\app\\src\\test.php',
63+
'src\\test.php',
64+
],
65+
];
66+
}
67+
68+
/**
69+
* @dataProvider dataGetRelativePath
70+
* @param string $currentWorkingDirectory
71+
* @param string $directorySeparator
72+
* @param string $filenameToRelativize
73+
* @param string $expectedResult
74+
*/
75+
public function testGetRelativePathOnUnix(
76+
string $currentWorkingDirectory,
77+
string $directorySeparator,
78+
string $filenameToRelativize,
79+
string $expectedResult
80+
): void
81+
{
82+
$helper = new SimpleRelativePathHelper($currentWorkingDirectory, $directorySeparator);
83+
$this->assertSame(
84+
$expectedResult,
85+
$helper->getRelativePath($filenameToRelativize)
86+
);
87+
}
88+
89+
}

0 commit comments

Comments
 (0)