Skip to content

Commit 4ff6332

Browse files
authored
Merge pull request #9 from PHPCSStandards/feature/further-slash-normalization
More slash normalization
2 parents b25ee48 + 3933e7c commit 4ff6332

File tree

4 files changed

+74
-13
lines changed

4 files changed

+74
-13
lines changed

src/Parser/SniffParser.php

+2-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace App\Parser;
55

66
use App\Parser\Exception\NotASniffPath;
7+
use App\Util\Functions;
78
use App\Value\Diff;
89
use App\Value\Property;
910
use App\Value\Sniff;
@@ -27,7 +28,7 @@ class SniffParser
2728
{
2829
public function parse(string $phpFilePath, SourceLocator $projectSourceLocator): Sniff
2930
{
30-
$phpFilePath = $this->normalizeSlashes($phpFilePath);
31+
$phpFilePath = Functions::normalizeSlashes($phpFilePath);
3132
$astLocator = (new BetterReflection())->astLocator();
3233
$reflector = new ClassReflector(
3334
new AggregateSourceLocator([
@@ -212,16 +213,4 @@ private function getUrls(ReflectionClass $classInfo, array $xmlUrls): UrlList
212213

213214
return new UrlList(array_merge($urls, $xmlUrls));
214215
}
215-
216-
/**
217-
* Normalizes all slashes in a file path to forward slashes.
218-
*
219-
* @param string $path File path.
220-
*
221-
* @return string The file path with normalized slashes.
222-
*/
223-
private function normalizeSlashes($path)
224-
{
225-
return str_replace('\\', '/', $path);
226-
}
227216
}

src/Parser/ViolationParser.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace App\Parser;
55

66
use App\Parser\Exception\NotAViolationPath;
7+
use App\Util\Functions;
78
use App\Value\Diff;
89
use App\Value\Url;
910
use App\Value\UrlList;
@@ -27,6 +28,7 @@ public function parse(string $xmlFilePath): Violation
2728

2829
private function getErrorCode(string $xmlFilePath): string
2930
{
31+
$xmlFilePath = Functions::normalizeSlashes($xmlFilePath);
3032
$part = '([^/]*)';
3133
preg_match("`$part/Docs/$part/{$part}Standard/$part\.xml$`", $xmlFilePath, $matches);
3234
if ($matches === []) {

src/Util/Functions.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Util;
5+
6+
class Functions
7+
{
8+
9+
/**
10+
* Normalizes all slashes in a file path to forward slashes.
11+
*
12+
* @param string $path File path.
13+
*
14+
* @return string The file path with normalized slashes.
15+
*/
16+
public static function normalizeSlashes(string $path)
17+
{
18+
return str_replace('\\', '/', $path);
19+
}
20+
}

tests/Util/FunctionsTest.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Tests\Utils;
5+
6+
use App\Util\Functions;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/** @covers \App\Util\Functions */
10+
class FunctionsTest extends TestCase
11+
{
12+
13+
/**
14+
* Verify slash normalization.
15+
*
16+
* @dataProvider dataNormalizeSlashes
17+
*
18+
* @param string $input Path to normalize.
19+
* @param string $expected Expected normalized path.
20+
*
21+
* @return void
22+
*/
23+
public function testNormalizeSlashes($input, $expected)
24+
{
25+
self::assertSame($expected, Functions::normalizeSlashes($input));
26+
}
27+
28+
/**
29+
* Data provider.
30+
*
31+
* @return array
32+
*/
33+
public function dataNormalizeSlashes()
34+
{
35+
return [
36+
'unix_slashes' => [
37+
'path/to/folder/filename.php',
38+
'path/to/folder/filename.php',
39+
],
40+
'windows_slashes' => [
41+
'path\to\folder\filename.php',
42+
'path/to/folder/filename.php',
43+
],
44+
'mixed_slashes' => [
45+
'path\to/folder\filename.php',
46+
'path/to/folder/filename.php',
47+
],
48+
];
49+
}
50+
}

0 commit comments

Comments
 (0)