Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SniffParser: add slash normalization #5

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Parser/SniffParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SniffParser
{
public function parse(string $phpFilePath, SourceLocator $projectSourceLocator): Sniff
{
$phpFilePath = $this->normalizeSlashes($phpFilePath);
$astLocator = (new BetterReflection())->astLocator();
$reflector = new ClassReflector(
new AggregateSourceLocator([
Expand Down Expand Up @@ -211,4 +212,16 @@ private function getUrls(ReflectionClass $classInfo, array $xmlUrls): UrlList

return new UrlList(array_merge($urls, $xmlUrls));
}

/**
* Normalizes all slashes in a file path to forward slashes.
*
* @param string $path File path.
*
* @return string The file path with normalized slashes.
*/
private function normalizeSlashes($path)
{
return str_replace('\\', '/', $path);
}
}
48 changes: 48 additions & 0 deletions tests/Parser/SniffParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SniffParserTest extends TestCase
{
private const PHP_FILE_PATH = 'var/tests/src/Standard/Sniffs/Category/MySniff.php';
private const XML_FILE_PATH = 'var/tests/src/Standard/Docs/Category/MyStandard.xml';
private const PHP_FILE_PATH_MIXED_SLASHES = 'var\tests\src\Standard/Sniffs/Category\MySniff.php';

private SniffParser $parser;
private Locator $astLocator;
Expand Down Expand Up @@ -246,6 +247,53 @@ class MySniff {}
);
}

/** @test */
public function parse_WithWindowsSlashesInPhpPath()
{
$content = '<?php
namespace Standard\Sniffs\Category;
/**
* Summary
*
* Description
*
* @since 1.0.0
*/
class MySniff {}
';

(new Filesystem())->dumpFile(self::PHP_FILE_PATH, $content);
$doc = $this->parser->parse(str_replace('/', '\\', self::PHP_FILE_PATH), new StringSourceLocator($content, $this->astLocator));
self::assertEquals(
'Standard.Category.My',
$doc->getCode()
);
}

/** @test */
public function parse_WithMixedSlashesInPhpPath()
{
$content = '<?php
namespace Standard\Sniffs\Category;
/**
* Summary
*
* Description
*
* @since 1.0.0
*/
class MySniff {}
';

(new Filesystem())->dumpFile(self::PHP_FILE_PATH, $content);

$doc = $this->parser->parse(self::PHP_FILE_PATH_MIXED_SLASHES, new StringSourceLocator($content, $this->astLocator));
self::assertEquals(
'Standard.Category.My',
$doc->getCode()
);
}

/** @test */
public function parse_WithInvalidPhpPath_ThrowException()
{
Expand Down