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

More slash normalization #9

Merged
merged 3 commits into from
Mar 18, 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
15 changes: 2 additions & 13 deletions src/Parser/SniffParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace App\Parser;

use App\Parser\Exception\NotASniffPath;
use App\Util\Functions;
use App\Value\Diff;
use App\Value\Property;
use App\Value\Sniff;
Expand All @@ -27,7 +28,7 @@ class SniffParser
{
public function parse(string $phpFilePath, SourceLocator $projectSourceLocator): Sniff
{
$phpFilePath = $this->normalizeSlashes($phpFilePath);
$phpFilePath = Functions::normalizeSlashes($phpFilePath);
$astLocator = (new BetterReflection())->astLocator();
$reflector = new ClassReflector(
new AggregateSourceLocator([
Expand Down Expand Up @@ -212,16 +213,4 @@ 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);
}
}
2 changes: 2 additions & 0 deletions src/Parser/ViolationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace App\Parser;

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

private function getErrorCode(string $xmlFilePath): string
{
$xmlFilePath = Functions::normalizeSlashes($xmlFilePath);
$part = '([^/]*)';
preg_match("`$part/Docs/$part/{$part}Standard/$part\.xml$`", $xmlFilePath, $matches);
if ($matches === []) {
Expand Down
20 changes: 20 additions & 0 deletions src/Util/Functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace App\Util;

class Functions
{

/**
* Normalizes all slashes in a file path to forward slashes.
*
* @param string $path File path.
*
* @return string The file path with normalized slashes.
*/
public static function normalizeSlashes(string $path)
{
return str_replace('\\', '/', $path);
}
}
50 changes: 50 additions & 0 deletions tests/Util/FunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

namespace App\Tests\Utils;

use App\Util\Functions;
use PHPUnit\Framework\TestCase;

/** @covers \App\Util\Functions */
class FunctionsTest extends TestCase
{

/**
* Verify slash normalization.
*
* @dataProvider dataNormalizeSlashes
*
* @param string $input Path to normalize.
* @param string $expected Expected normalized path.
*
* @return void
*/
public function testNormalizeSlashes($input, $expected)
{
self::assertSame($expected, Functions::normalizeSlashes($input));
}

/**
* Data provider.
*
* @return array
*/
public function dataNormalizeSlashes()
{
return [
'unix_slashes' => [
'path/to/folder/filename.php',
'path/to/folder/filename.php',
],
'windows_slashes' => [
'path\to\folder\filename.php',
'path/to/folder/filename.php',
],
'mixed_slashes' => [
'path\to/folder\filename.php',
'path/to/folder/filename.php',
],
];
}
}