From 4534676a0295c8f53c5ba30804250a9280e8b5cc Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 15 Feb 2024 20:23:31 +0700 Subject: [PATCH] Ensure always normalize file paths for multi OSs support (#7) * Ensure always normalze file paths for multi OSs support * fix phpstan --- src/Command/FindMultiClassesCommand.php | 3 ++- src/EntityClassResolver.php | 3 ++- src/FileSystem/PathHelper.php | 9 ++++++++- src/Testing/Command/DetectUnitTestsCommand.php | 3 +-- src/Testing/Printer/PHPUnitXmlPrinter.php | 9 +++------ tests/AbstractTestCase.php | 3 ++- tests/EntityClassResolver/EntityClassResolverTest.php | 2 +- 7 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/Command/FindMultiClassesCommand.php b/src/Command/FindMultiClassesCommand.php index 9b4bb33bd7..065816a7bf 100644 --- a/src/Command/FindMultiClassesCommand.php +++ b/src/Command/FindMultiClassesCommand.php @@ -4,6 +4,7 @@ namespace Rector\SwissKnife\Command; +use Rector\SwissKnife\FileSystem\PathHelper; use Rector\SwissKnife\Finder\MultipleClassInOneFileFinder; use Rector\SwissKnife\Finder\PhpFilesFinder; use Symfony\Component\Console\Command\Command; @@ -50,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int foreach ($multipleClassesByFile as $filePath => $classes) { // get relative path to getcwd() - $relativeFilePath = str_replace(getcwd() . '/', '', $filePath); + $relativeFilePath = PathHelper::relativeToCwd($filePath); $message = sprintf('File "%s" contains %d classes', $relativeFilePath, count($classes)); $this->symfonyStyle->section($message); diff --git a/src/EntityClassResolver.php b/src/EntityClassResolver.php index 1bdb4fb21c..530ce24525 100644 --- a/src/EntityClassResolver.php +++ b/src/EntityClassResolver.php @@ -4,6 +4,7 @@ namespace Rector\SwissKnife; +use Nette\Utils\Strings; use PhpParser\NodeTraverser; use Rector\SwissKnife\Finder\PhpFilesFinder; use Rector\SwissKnife\Finder\YamlFilesFinder; @@ -87,7 +88,7 @@ private function resolveYamlEntityClassNames(array $paths): array /** @var SplFileInfo $yamlFileInfo */ foreach ($yamlFileInfos as $yamlFileInfo) { - $matches = \Nette\Utils\Strings::matchAll($yamlFileInfo->getContents(), self::YAML_ENTITY_CLASS_NAME_REGEX); + $matches = Strings::matchAll($yamlFileInfo->getContents(), self::YAML_ENTITY_CLASS_NAME_REGEX); foreach ($matches as $match) { $yamlEntityClassNames[] = $match['class_name']; diff --git a/src/FileSystem/PathHelper.php b/src/FileSystem/PathHelper.php index 904fd5ac06..f1682ea4b4 100644 --- a/src/FileSystem/PathHelper.php +++ b/src/FileSystem/PathHelper.php @@ -6,9 +6,16 @@ final class PathHelper { + private static function normalize(string $filePath): string + { + return str_replace('\\', '/', $filePath); + } + public static function relativeToCwd(string $filePath): string { + $filePath = self::normalize($filePath); + // get relative path from getcwd() - return str_replace(getcwd() . '/', '', $filePath); + return str_replace(self::normalize((string) getcwd()) . '/', '', $filePath); } } diff --git a/src/Testing/Command/DetectUnitTestsCommand.php b/src/Testing/Command/DetectUnitTestsCommand.php index f65b30c12f..6fdd478bee 100644 --- a/src/Testing/Command/DetectUnitTestsCommand.php +++ b/src/Testing/Command/DetectUnitTestsCommand.php @@ -56,8 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $filesPHPUnitXmlContents = $this->phpunitXmlPrinter->printFiles( - $unitTestCasesClassesToFilePaths, - (string) getcwd() + $unitTestCasesClassesToFilePaths ); FileSystem::write(self::OUTPUT_FILENAME, $filesPHPUnitXmlContents); diff --git a/src/Testing/Printer/PHPUnitXmlPrinter.php b/src/Testing/Printer/PHPUnitXmlPrinter.php index 66bea715c7..8d593ac49e 100644 --- a/src/Testing/Printer/PHPUnitXmlPrinter.php +++ b/src/Testing/Printer/PHPUnitXmlPrinter.php @@ -4,7 +4,7 @@ namespace Rector\SwissKnife\Testing\Printer; -use Nette\Utils\Strings; +use Rector\SwissKnife\FileSystem\PathHelper; final class PHPUnitXmlPrinter { @@ -13,14 +13,11 @@ final class PHPUnitXmlPrinter * * @param string[] $filePaths */ - public function printFiles(array $filePaths, string $rootDirectory): string + public function printFiles(array $filePaths): string { - $rootDirectory = realpath($rootDirectory); - $fileContents = ''; foreach ($filePaths as $filePath) { - $relativeFilePath = Strings::after($filePath, $rootDirectory . '/'); - + $relativeFilePath = PathHelper::relativeToCwd($filePath); $fileContents .= '' . $relativeFilePath . '' . PHP_EOL; } diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 89b9a2eaf1..91652ab424 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -4,12 +4,13 @@ namespace Rector\SwissKnife\Tests; +use Illuminate\Container\Container; use PHPUnit\Framework\TestCase; use Rector\SwissKnife\DependencyInjection\ContainerFactory; abstract class AbstractTestCase extends TestCase { - private \Illuminate\Container\Container $container; + private Container $container; protected function setUp(): void { diff --git a/tests/EntityClassResolver/EntityClassResolverTest.php b/tests/EntityClassResolver/EntityClassResolverTest.php index 32071fc22d..18909dc69d 100644 --- a/tests/EntityClassResolver/EntityClassResolverTest.php +++ b/tests/EntityClassResolver/EntityClassResolverTest.php @@ -21,7 +21,7 @@ protected function setUp(): void public function test(): void { - $entityClasses = $this->entityClassResolver->resolve([__DIR__ . '/Fixture'], function () { + $entityClasses = $this->entityClassResolver->resolve([__DIR__ . '/Fixture'], static function () : void { }); $this->assertSame(