From aaeefd8f7075902a89cfa2d3b01c44c21e6f939a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 18 Mar 2021 06:58:34 +0100 Subject: [PATCH 1/2] 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. --- src/Parser/SniffParser.php | 15 ++--------- src/Util/Functions.php | 20 +++++++++++++++ tests/Util/FunctionsTest.php | 50 ++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 src/Util/Functions.php create mode 100644 tests/Util/FunctionsTest.php diff --git a/src/Parser/SniffParser.php b/src/Parser/SniffParser.php index ddcdca3..afe51e7 100644 --- a/src/Parser/SniffParser.php +++ b/src/Parser/SniffParser.php @@ -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; @@ -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([ @@ -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); - } } diff --git a/src/Util/Functions.php b/src/Util/Functions.php new file mode 100644 index 0000000..70161e8 --- /dev/null +++ b/src/Util/Functions.php @@ -0,0 +1,20 @@ + [ + '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', + ], + ]; + } +} From 76a7d71baf96523552b2b370a2353bb554b30bd3 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 18 Mar 2021 07:03:33 +0100 Subject: [PATCH 2/2] ViolationParser: implement use of Functions::normalizeSlashes() Improves cross-platform compatibility of the code. With this additional change, the tests will pass on Windows as well, meaning that this part of the functionality should now also work on Windows. --- src/Parser/ViolationParser.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Parser/ViolationParser.php b/src/Parser/ViolationParser.php index cabab9c..4caf328 100644 --- a/src/Parser/ViolationParser.php +++ b/src/Parser/ViolationParser.php @@ -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; @@ -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 === []) {