File tree 2 files changed +112
-3
lines changed
2 files changed +112
-3
lines changed Original file line number Diff line number Diff line change @@ -6,16 +6,36 @@ class SimpleRelativePathHelper implements RelativePathHelper
6
6
{
7
7
8
8
/** @var string */
9
- private $ currentWorkingDirectory ;
9
+ private $ directorySeparator ;
10
10
11
- public function __construct (string $ currentWorkingDirectory )
11
+ /** @var string */
12
+ private $ currentWorkingDirectory = '' ;
13
+
14
+ public function __construct (string $ currentWorkingDirectory , string $ directorySeparator = DIRECTORY_SEPARATOR )
12
15
{
16
+ $ this ->directorySeparator = $ directorySeparator ;
17
+
18
+ if ($ currentWorkingDirectory !== $ directorySeparator ) {
19
+ $ currentWorkingDirectory = rtrim ($ currentWorkingDirectory , $ directorySeparator );
20
+ }
13
21
$ this ->currentWorkingDirectory = $ currentWorkingDirectory ;
14
22
}
15
23
16
24
public function getRelativePath (string $ filename ): string
17
25
{
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 ) {
19
39
return substr ($ filename , strlen ($ this ->currentWorkingDirectory ) + 1 );
20
40
}
21
41
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments