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

always make paths absolute before processing files #6293

Merged
merged 4 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions src/FileSystem/FilePathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 realpath($fileRealPath);
}
samsonasik marked this conversation as resolved.
Show resolved Hide resolved

samsonasik marked this conversation as resolved.
Show resolved Hide resolved
/**
* Used from
* https://github.com/phpstan/phpstan-src/blob/02425e61aa48f0668b4efb3e73d52ad544048f65/src/File/FileHelper.php#L40, with custom modifications
Expand Down
1 change: 1 addition & 0 deletions src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(realpath(...), $filteredFilePaths);
$filteredFilePaths = array_filter(
$filteredFilePaths,
fn (string $filePath): bool => ! $this->pathSkipper->shouldSkip($filePath)
Expand Down
20 changes: 20 additions & 0 deletions tests/FileSystem/FilesFinder/FilesFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public function test(): void
$this->assertCount(0, $foundFiles);
}

#[DataProvider('alwaysReturnsAbsolutePathDataProvider')]
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<array<string>>
*/
public static function alwaysReturnsAbsolutePathDataProvider(): 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']);
Expand Down
Loading