Skip to content

Commit aaeefd8

Browse files
committed
Move normalizeSlashes() to Util\Functions class
Follow up on 5 When trying to run the tests on Windows, I ran into more issues with mixed slashes. As that means that the `normalizeSlashes()` functionality is needed in more places, I'm proposing to introduce a generic `Util\Functions` class for simple static methods which are needed in multiple places. Includes introducing dedicated tests for the `normalizeSlashes()` method.
1 parent 806997b commit aaeefd8

File tree

3 files changed

+72
-13
lines changed

3 files changed

+72
-13
lines changed

src/Parser/SniffParser.php

Lines changed: 2 additions & 13 deletions
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/Util/Functions.php

Lines changed: 20 additions & 0 deletions
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

Lines changed: 50 additions & 0 deletions
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)