Skip to content

Commit dae9bec

Browse files
committed
Add slash normalization
Fixes #4 Includes tests.
1 parent 8bed61b commit dae9bec

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/Parser/SniffParser.php

+13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class SniffParser
2727
{
2828
public function parse(string $phpFilePath, SourceLocator $projectSourceLocator): Sniff
2929
{
30+
$phpFilePath = $this->normalizeSlashes($phpFilePath);
3031
$astLocator = (new BetterReflection())->astLocator();
3132
$reflector = new ClassReflector(
3233
new AggregateSourceLocator([
@@ -211,4 +212,16 @@ private function getUrls(ReflectionClass $classInfo, array $xmlUrls): UrlList
211212

212213
return new UrlList(array_merge($urls, $xmlUrls));
213214
}
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+
}
214227
}

tests/Parser/SniffParserTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class SniffParserTest extends TestCase
1919
{
2020
private const PHP_FILE_PATH = 'var/tests/src/Standard/Sniffs/Category/MySniff.php';
2121
private const XML_FILE_PATH = 'var/tests/src/Standard/Docs/Category/MyStandard.xml';
22+
private const PHP_FILE_PATH_MIXED_SLASHES = 'var\tests\src\Standard/Sniffs/Category\MySniff.php';
2223

2324
private SniffParser $parser;
2425
private Locator $astLocator;
@@ -246,6 +247,53 @@ class MySniff {}
246247
);
247248
}
248249

250+
/** @test */
251+
public function parse_WithWindowsSlashesInPhpPath()
252+
{
253+
$content = '<?php
254+
namespace Standard\Sniffs\Category;
255+
/**
256+
* Summary
257+
*
258+
* Description
259+
*
260+
* @since 1.0.0
261+
*/
262+
class MySniff {}
263+
';
264+
265+
(new Filesystem())->dumpFile(self::PHP_FILE_PATH, $content);
266+
$doc = $this->parser->parse(str_replace('/', '\\', self::PHP_FILE_PATH), new StringSourceLocator($content, $this->astLocator));
267+
self::assertEquals(
268+
'Standard.Category.My',
269+
$doc->getCode()
270+
);
271+
}
272+
273+
/** @test */
274+
public function parse_WithMixedSlashesInPhpPath()
275+
{
276+
$content = '<?php
277+
namespace Standard\Sniffs\Category;
278+
/**
279+
* Summary
280+
*
281+
* Description
282+
*
283+
* @since 1.0.0
284+
*/
285+
class MySniff {}
286+
';
287+
288+
(new Filesystem())->dumpFile(self::PHP_FILE_PATH, $content);
289+
290+
$doc = $this->parser->parse(self::PHP_FILE_PATH_MIXED_SLASHES, new StringSourceLocator($content, $this->astLocator));
291+
self::assertEquals(
292+
'Standard.Category.My',
293+
$doc->getCode()
294+
);
295+
}
296+
249297
/** @test */
250298
public function parse_WithInvalidPhpPath_ThrowException()
251299
{

0 commit comments

Comments
 (0)