From 4a95676655f29e74c3312ae16dcd8c9967941652 Mon Sep 17 00:00:00 2001 From: Matthias Vogel Date: Fri, 6 Sep 2024 13:53:05 +0200 Subject: [PATCH] always make paths absolute before processing files --- src/FileSystem/FilePathHelper.php | 9 +++++++++ src/FileSystem/FilesFinder.php | 1 + .../FilesFinder/FilesFinderTest.php | 20 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/FileSystem/FilePathHelper.php b/src/FileSystem/FilePathHelper.php index a2e9125254a..c93cc5615e6 100644 --- a/src/FileSystem/FilePathHelper.php +++ b/src/FileSystem/FilePathHelper.php @@ -45,6 +45,15 @@ public function relativePath(string $fileRealPath): string return $this->relativeFilePathFromDirectory($fileRealPath, getcwd()); } + public function absolutePath(string $fileRealPath): string + { + if ($this->filesystem->isAbsolutePath($fileRealPath)) { + return $fileRealPath; + } + + return getcwd() . '/' . $fileRealPath; + } + /** * Used from * https://github.com/phpstan/phpstan-src/blob/02425e61aa48f0668b4efb3e73d52ad544048f65/src/File/FileHelper.php#L40, with custom modifications diff --git a/src/FileSystem/FilesFinder.php b/src/FileSystem/FilesFinder.php index 7fc11662d5a..f2bccc01095 100644 --- a/src/FileSystem/FilesFinder.php +++ b/src/FileSystem/FilesFinder.php @@ -39,6 +39,7 @@ public function findInDirectoriesAndFiles(array $source, array $suffixes = [], b // filtering files in files collection $filteredFilePaths = $this->fileAndDirectoryFilter->filterFiles($filesAndDirectories); + $filteredFilePaths = array_map($this->filePathHelper->absolutePath(...), $filteredFilePaths); $filteredFilePaths = array_filter( $filteredFilePaths, fn (string $filePath): bool => ! $this->pathSkipper->shouldSkip($filePath) diff --git a/tests/FileSystem/FilesFinder/FilesFinderTest.php b/tests/FileSystem/FilesFinder/FilesFinderTest.php index 4457f7a91fb..850175c3326 100644 --- a/tests/FileSystem/FilesFinder/FilesFinderTest.php +++ b/tests/FileSystem/FilesFinder/FilesFinderTest.php @@ -29,6 +29,26 @@ public function test(): void $this->assertCount(0, $foundFiles); } + #[DataProvider('testAlwaysReturnsAbsolutePathDataProvider')] + public function testAlwaysReturnsAbsolutePath(string $relativePath): void + { + $absolutePath = str_replace('/', DIRECTORY_SEPARATOR, getcwd() . '/' . $relativePath); + $foundFiles = $this->filesFinder->findInDirectoriesAndFiles([$absolutePath], ['php']); + $this->assertStringStartsWith($absolutePath, $foundFiles[0], 'should return absolute path if absolute is given'); + + $foundFiles = $this->filesFinder->findInDirectoriesAndFiles([$relativePath], ['php']); + $this->assertStringStartsWith($absolutePath, $foundFiles[0], 'should return absolute path if relative is given'); + } + + /** + * @return Iterator> + */ + public static function testAlwaysReturnsAbsolutePathDataProvider(): Iterator + { + yield 'directory given' => ['tests/FileSystem/FilesFinder/Source/']; + yield 'file given' => ['tests/FileSystem/FilesFinder/Source/SomeFile.php']; + } + public function testWithFollowingBrokenSymlinks(): void { SimpleParameterProvider::setParameter(Option::SKIP, [__DIR__ . '/../SourceWithBrokenSymlinks/folder1']);